new 出的对象,无法调用@Autowired进入的spring bean
(2011-02-11 17:55:54)
@Autowired来的spring 下的bean,则当前类必须也是spring bean才能调用它,不能用new Xxx()来获得对象,这种方式获得的对象无法调用其内的@autowired的bean
1. 类1 加入spring pool | public class PersonServiceImpl implements PersonService{ } 加入spring pool <bean id="personServiceImpl" class="com.machome.testtip.impl.PersonServiceImpl" > </bean> |
2.类2 autowired类1, 并且也加入spring pool | public class ProxyPServiceImpl implements ProxyPService { } |
3.直接new类2,则执行其方法时出null pointer错误 | ProxyPService proxyPService.save(); 执行报错: java.lang.NullPointerException |
4.解决:用spring 方式获取类2的bean,再执行其方法,没问题 | ProxyPService try { proxyPService.save(); 执行: this is proxy say:This is save for test spring |