new不能执行autowired_new出来的对象无法调用@Autowired注入的Spring Bean

@Autowired注入Spring Bean,则当前类必须也是Spring Bean才能调用它,不能用new xxx()来获得对象,这种方式获得的对象无法调用@Autowired注入的Bean。

1、类1,加入Spring Pool

public class PersonServiceImpl implements PersonService{

public void save(){

System.out.println("This is save for test spring");}

public List findAll(){

List retList = new ArrayList();

for(int i=1;i<10;i++){

retList.add("test"+i);}return retList;}

}//加入Spring Pool

2、类2,@Autowired类1,并且也加入Spring Pool

public class ProxyPServiceImpl implements ProxyPService {

public void save(){

System.out.print("this is proxy say:");personService.save();}

public List 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 proxysay:This is save for test spring

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用@Autowired注解注入对象时,Spring容器会自动查找对应的Bean,并将其注入到需要的位置。而@Bean注解则用于声明一个Bean,告诉Spring容器如何创建该对象。因此,@Autowired与@Bean可以联合使用,实现依赖注入Bean创建的功能。 下面是一个简单的例子: ``` @Configuration public class AppConfig { @Bean public ExampleDao exampleDao() { return new ExampleDao(); } @Bean public ExampleService exampleService() { return new ExampleService(exampleDao()); } } ``` 在上面的例子中,@Configuration注解表示该是一个配置,里面声明了两个Bean:ExampleDao和ExampleService。其中,ExampleService依赖于ExampleDao,因此在@Bean注解中调用了exampleDao()方法来创建ExampleDao对象,并将其传入到ExampleService的构造方法中。 在ExampleService中,使用@Autowired注解注入ExampleDao对象: ``` public class ExampleService { private ExampleDao exampleDao; public ExampleService(@Autowired ExampleDao exampleDao) { this.exampleDao = exampleDao; } // ... } ``` 因为ExampleService中的构造方法被注解为@Autowired,所以Spring容器会自动查找ExampleDao的Bean,并将其注入到ExampleService的构造方法中。这样,在创建ExampleService对象时,ExampleDao对象会被自动注入。 在实际开发中,一般会将Bean的声明和依赖注入的声明分别放在不同的配置中,以提高代码的可读性和可维护性。例如,将Bean的声明放在一个中,如下所示: ``` @Configuration public class DaoConfig { @Bean public ExampleDao exampleDao() { return new ExampleDao(); } } ``` 将依赖注入的声明放在另一个中,如下所示: ``` @Configuration public class ServiceConfig { @Autowired private ExampleDao exampleDao; @Bean public ExampleService exampleService() { return new ExampleService(exampleDao); } } ``` 这样,DaoConfig和ServiceConfig分别声明了ExampleDao和ExampleService的Bean,并将ExampleDao注入到ExampleService中。在实际运行时,Spring容器会自动将这两个配置中的Bean合并,并创建ExampleDao和ExampleService对象,并将ExampleDao对象注入到ExampleService对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值