spring注入为null的问题
2015-08-28 00:00
323 查看
一,新建一个util类,TestUtil:
public class TestUtil {
private MyService myService;
public MyService getMyService() {
return myService;
}
public void setMyService(MyService myService) {
this.myService = myService;
}
public void test(){
System.out.println("myService = " + myService);
}
}
二,spring xml配置如下:
<bean name="testUtil" class="com.rgm.util.testUtil">
<property name="myService" ref="myService" />
</bean>
三,action使用:
TestUtil testUtil = new TestUtil();
testUtil.test();
四,输出结果:myService = null
五,原因分析:
由于testUtil是在new出来的,testUtil不受spring管理,因此myService并没有注入到testUtil实例中。
六,解决办法:
1)在action增加TestUtil属性,并设置get/set方法,在xml里面注入action的testUtil属性
2)新建spring工具类获得相应的bean
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
<bean id="springContextUtil" class="com.rgm.util.SpringContextUtil" scope="singleton"/>
public class TestUtil {
private MyService myService = (MyService)SpringContextUtil.getApplicationContext().getBean("myService");
public void test(){
System.out.println(myService.toString());
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
public class TestUtil {
private MyService myService;
public MyService getMyService() {
return myService;
}
public void setMyService(MyService myService) {
this.myService = myService;
}
public void test(){
System.out.println("myService = " + myService);
}
}
二,spring xml配置如下:
<bean name="testUtil" class="com.rgm.util.testUtil">
<property name="myService" ref="myService" />
</bean>
三,action使用:
TestUtil testUtil = new TestUtil();
testUtil.test();
四,输出结果:myService = null
五,原因分析:
由于testUtil是在new出来的,testUtil不受spring管理,因此myService并没有注入到testUtil实例中。
六,解决办法:
1)在action增加TestUtil属性,并设置get/set方法,在xml里面注入action的testUtil属性
2)新建spring工具类获得相应的bean
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
<bean id="springContextUtil" class="com.rgm.util.SpringContextUtil" scope="singleton"/>
public class TestUtil {
private MyService myService = (MyService)SpringContextUtil.getApplicationContext().getBean("myService");
public void test(){
System.out.println(myService.toString());
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android Native 绘图方法
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- XML 与 JSON 优劣对比
- ActionScript 3.0 编程体验
- As3.0 xml + Loader应用代码
- 再谈Jquery Ajax方法传递到action(补充)
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#中struct和class的区别详解
- C#针对xml基本操作及保存配置文件应用实例
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法
- mysql 中存在null和空时创建唯一索引的方法
- C#中的Linq to Xml详解
- C#操作XML文件实例汇总