81,092
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>spring-test</display-name>
<!-- spring config begin -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/spring/local/appcontext-*.xml
</param-value>
</context-param>
<!-- end -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<bean id="springTest" class="com.fcc.spring.test.SpringTest" />
<bean id="helloWorld" class="com.fcc.spring.test.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
package com.fcc.spring.test;
import java.io.Serializable;
public class HelloWorld implements Serializable{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.fcc.spring.test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.annotation.Resource;
public class SpringTest implements ApplicationContextAware{
private static ApplicationContext applicationContext;
public static void main(String[] args){
HelloWorld obj = (HelloWorld)getBean("helloWorld");
System.out.println("obj = " + obj);
System.out.println("The message value is " + obj.getMessage());
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("applicationContext before : " + applicationContext);
SpringTest.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static Object getBean(String name) throws BeansException{
System.out.println("applicationContext = " + applicationContext);
return applicationContext.getBean(name);
}
}