new 出的对象无法使用 @Autowired装配进来的属性,会报null
来源:互联网 发布:淘宝卖百货用什么类目 编辑:程序博客网 时间:2024/06/05 01:54
new 出的对象,无法调用@Autowired进入的spring bean
(2011-02-11 17:55:54)转载▼
标签:
autowired
new
null
pointer
杂谈
分类: java和数据库@Autowired来的spring 下的bean,则当前类必须也是spring bean才能调用它,不能用new Xxx()来获得对象,这种方式获得的对象无法调用其内的@autowired的bean
1. 类1
加入spring poolpublic class PersonServiceImpl implements PersonService{
public void save(){
System.out.println("This is save for test spring");
}
public List<String> findAll(){
List<String> retList = new ArrayList<String>();
for(int i=1;i<10;i++){
retList.add("test"+i);
}
return retList;
}
}
加入spring pool
<bean id="personServiceImpl" class="com.machome.testtip.impl.PersonServiceImpl" >
</bean>2.类2
autowired类1, 并且也加入spring poolpublic class ProxyPServiceImpl implements ProxyPService {
public void save(){
System.out.print("this is proxy say:");
personService.save();
}
public List<String> findAll(){
System.out.print("this is proxy say:");
return personService.findAll();
}
@Autowired
PersonService personService;
}3.直接new类2,则执行其方法时出null pointer错误ProxyPService proxyPService = new ProxyPServiceImpl();
proxyPService.save();
执行报错:
java.lang.NullPointerException
at com.machome.testtip.impl.ProxyPServiceImpl.save(ProxyPServiceImpl.java:18)
at com.machome.testtip.TestSpring2.testSave(TestSpring2.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)4.解决:用spring 方式获取类2的bean,再执行其方法,没问题ProxyPService proxyPService = null;
try {
String[] confFile = {"spring.xml"};
ctx = new ClassPathXmlApplicationContext(confFile);
proxyPService = (ProxyPService)ctx.getBean("ProxyPServiceImpl");
} catch (Exception e) {
e.printStackTrace();
}
proxyPService.save();
执行:
this is proxy say:This is save for test spring
1. 类1
加入spring poolpublic class PersonServiceImpl implements PersonService{
}
加入spring pool
<bean id="personServiceImpl" class="com.machome.testtip.impl.PersonServiceImpl" >
</bean>2.类2
autowired类1, 并且也加入spring poolpublic class ProxyPServiceImpl implements ProxyPService {
}3.直接new类2,则执行其方法时出null pointer错误ProxyPService
proxyPService.save();
执行报错:
java.lang.NullPointerException
try {
proxyPService.save();
执行:
this is proxy say:This is save for test spring
0 0
- new 出的对象无法使用 @Autowired装配进来的属性,会报null
- 使用@Autowired自动装配对象和new对象的区别
- new 出的对象,无法调用@Autowired进入的spring bean
- new 出的对象,无法调用@Autowired进入的spring bean
- new 出一个对象 和 = null的区别
- 被@Autowired的对象不能再new
- new出来的对象无法调用@Autowired注入的Spring Bean
- Spring的《注解装配》- @autowired
- java创建对象时,new 出一个对象 和 = null的区别?
- java中id为封装类时整形new出的对象为null
- @autowired自动装配的几种方式
- Aop拦截到的Action 使得@Autowired 无法自动装配问题解决
- Aop拦截到的Action 使得@Autowired 无法自动装配问题解决
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- @Autowired 对象为null
- 为什么Oracle数据库安装成功后,经常会报出无法识别SID的错误
- SCRIPT5007: 无法获取属性“getElementsByTagName”的值: 对象为 null 或未定义 的解决方案
- JavaEE Spring框架学习笔记(理论学习之通过@Autowired 自动装配注解的使用)
- div布局-子容器设置浮动对于父容器高度自适应产生影响的解决办法
- 第三方登录获取头像和昵称
- javaWeb开发细节总结(3)
- C++实际上有两种不同的常数
- java 动态绑定
- new 出的对象无法使用 @Autowired装配进来的属性,会报null
- 【a901】滑雪
- 你编译你的代码
- tabbaritem设置红点大小、自定义宽高
- 训练计划
- 客户端往服务端定时发送消息
- java is bad
- React-Native 基础(五) Height and Width
- 再谈魔镜VR第一人称