@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<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 Pool

public 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

参考:

http://blog.sina.com.cn/s/blog_6151984a0100oy98.html

https://segmentfault.com/q/1010000008200816

http://www.cnblogs.com/chyu/p/4655475.html

new出来的对象无法调用@Autowired注入的Spring Bean的更多相关文章

  1. 关于工具类静态方法调用@Autowired注入的service类问题

    @Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...

  2. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. IDEA下Maven项目搭建踩坑记----3.最长的bug,最简单的错误。同一类中,部分函数的@AutoWired注入的对象失效

    这个错误绝对是我写到现在为止最傻X的一个错误,先上图 问题: 出了一个特别长的错误,大致的意思就是mapper.xml文件注入Dao层的时候失败. 解决: 查看一下错误的位置→ 找到Dao层 找到错误 ...

  4. 欲哭无泪的@Autowired注入对象为NULL

    欲哭无泪啊...一下午的时间就这么被浪费了...一个基于spring mvc和spring data jpa的小项目,当我写完一个controller的测试用例后,一运行却报空指针,跟了下是一个dao ...

  5. 实现Callable的对象中,用@Autowired注入别的对象失败

    场景是这样: 我需要在一个实现类A中写一个拿到返回值的多线程,于是用的Callable,在这个实现类A外我又写了一个专门实现Callable的实现类B,在B中用spring注解@Autowired注入 ...

  6. intellij idea中去除@Autowired注入对象的红色波浪线提示

    idea中通过@Autowired注入的对象一直有下划线提示. 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspections检索

  7. intellij idea中去除@Autowired注入对象带来的下划线提示

    场景: idea中通过@Autowired注入的对象一直有下划线提示,虽然不影响运行 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspe ...

  8. Quartz定时器+Spring + @Autowired注入 空指针异常

    在Quartz的定时方法里引用@Autowired注入Bean,会报空指针错误 解决办法: 第一种方法:(推荐,简单,亲测可行) 使用@Resource(name="指定要注入的Bean&q ...

  9. Netty handler处理类无法使用@Autowired注入bean的解决方法

    问题由来: 公司有个项目用到netty作为websocket的实现,最近打算部署双机,这使得原来在内存中的保存Channel信息的方案不再可行,需要转移到redis中,改造过程中发现通过@Autowi ...

随机推荐

  1. 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement

    题目传送门 /* 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 */ #include ...

  2. ACM_求f(n)

    求f(n) Time Limit: 2000/1000ms (Java/Others) Problem Description: 设函数f(n)=1*1*1+2*2*2+3*3*3+...+n*n*n ...

  3. 数据传递-------@ModelAttribute

    package com.wh.handler; /** * @ModelAttribute绑定请求参数到命令对象 * @ModelAttribute一个具有如下三个作用: * * ①绑定请求参数到命令 ...

  4. WinForm ListBox 控件用法

    下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...

  5. 转:python中使用txt文本保存和读取变量

    问题: 在python中需要经常有需要提前生成复杂的计算结果变量的需求减少程序计算运行时间的需求,因此这里把变量存在txt文本文件中. 解决方法: 使用两个函数解决问题,一个函数把变量保存到文本文件中 ...

  6. Http协议对格式、请求头、方法

    ######### #概览 ######### 超文本传输协议(Http: Hyper Text Transfer Protocol) :用于发送WWW方式的数据.采用TCP/IP协议,是一个无状态协 ...

  7. SQL server中的T-SQL语句

    首先点击新建查询 如下图所示 创建数据库:create database 数据库名称 使用数据库:use 数据库名称 创建表:create table 表名 ( 代码 ) 输入完成执行时需选中 如果需 ...

  8. 【转】Java实现将文件或者文件夹压缩成zip

    转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...

  9. Unity笔记(3)自学第三天

    学习记录: 脚本使用:

  10. STM32的串口DMA收发以及双缓冲区的实现

    在使用STM32的UART的DMA功能总结如下: 首先上代码,这里采用STM32 的USART1作为Demo,RX的DMA为DMA1_Channel5,TX的DMA为DMA1_Channel4.初始化 ...