最近在学习spring,就写了一个小程序,来验证spring的自动加载类的功能,主要测试下通过spring来自获取POJO类的实例,闲话少说,上代码:
先写一个POJO类 :
package com.jeecms.common.util;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void test(){
System.out.println("name:"+getName()+" age:"+getAge());
}
}
然后在写一个bean.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="person" class="com.jeecms.common.util.Person">
<property name="name" value="xingoo"/>
<property name="age" value="12"/>
</bean>
</beans>
最后在写一个测试类:
package com.jeecms.common.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Tom
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
Person p = app.getBean("person",Person.class);//创建bean的引用对象
p.test();
}
}
这样基本完成了一个简单的装载注入,不过在使用ClassPathXmlApplicationContext时候发现, ClassPathXmlApplicationContext只能获取编译路径下的src,即你的.class文件目录下,不能获取其他路径的xml文件,不过我们可以使用FileSystemXmlApplicationContext,通过绝对的路径来获取XML文件,
ApplicationContext app = new FileSystemXmlApplicationContext("/WebContent/WEB-INF/config/bean.xml");
也或者通过配置 contextConfigLocation 来初始化所有相关的spring
<context-param>
<!-- Context Configuration locations for Spring XML files -->
<param-name>contextConfigLocation</param-name>
<param-value>
./WEB-INF/applicationContext.xml
</param-value>
</context-param>
然后就可以通过实现ApplicationContextAware 接口,可获取ApplicationContext 实例
/**
* 以静态变量保存Spring ApplicationContext,可在任意代码中取出ApplicaitonContext.
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext =applicationContext;
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}
private static void checkApplicationContext() {
if (applicationContext == null)
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil");
}
}