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<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的更多相关文章
- 关于工具类静态方法调用@Autowired注入的service类问题
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- IDEA下Maven项目搭建踩坑记----3.最长的bug,最简单的错误。同一类中,部分函数的@AutoWired注入的对象失效
这个错误绝对是我写到现在为止最傻X的一个错误,先上图 问题: 出了一个特别长的错误,大致的意思就是mapper.xml文件注入Dao层的时候失败. 解决: 查看一下错误的位置→ 找到Dao层 找到错误 ...
- 欲哭无泪的@Autowired注入对象为NULL
欲哭无泪啊...一下午的时间就这么被浪费了...一个基于spring mvc和spring data jpa的小项目,当我写完一个controller的测试用例后,一运行却报空指针,跟了下是一个dao ...
- 实现Callable的对象中,用@Autowired注入别的对象失败
场景是这样: 我需要在一个实现类A中写一个拿到返回值的多线程,于是用的Callable,在这个实现类A外我又写了一个专门实现Callable的实现类B,在B中用spring注解@Autowired注入 ...
- intellij idea中去除@Autowired注入对象的红色波浪线提示
idea中通过@Autowired注入的对象一直有下划线提示. 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspections检索
- intellij idea中去除@Autowired注入对象带来的下划线提示
场景: idea中通过@Autowired注入的对象一直有下划线提示,虽然不影响运行 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspe ...
- Quartz定时器+Spring + @Autowired注入 空指针异常
在Quartz的定时方法里引用@Autowired注入Bean,会报空指针错误 解决办法: 第一种方法:(推荐,简单,亲测可行) 使用@Resource(name="指定要注入的Bean&q ...
- Netty handler处理类无法使用@Autowired注入bean的解决方法
问题由来: 公司有个项目用到netty作为websocket的实现,最近打算部署双机,这使得原来在内存中的保存Channel信息的方案不再可行,需要转移到redis中,改造过程中发现通过@Autowi ...
随机推荐
- 题解报告:hdu 2612 Find a way(双bfs)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- Android 性能优化(14)网络优化( 10)Determining and Monitoring the Connectivity Status
Determining and Monitoring the Connectivity Status This lesson teaches you to Determine if you Have ...
- 1682. [HAOI2014]贴海报
1682. [HAOI2014]贴海报 ★★☆ 输入文件:ha14d.in 输出文件:ha14d.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] Byteto ...
- firefox浏览器中 bootstrap 静态弹出框中select下拉框不能弹出(解决方案)
问题出现场景1: 在firefox浏览器中在bootstrap弹出的modal静态框中再次弹出一个静态框时 select下拉框不能弹出选项 解决方案:去掉最外层静态框的 tabindex=" ...
- select count(1) 和 select count(*)的区别
统计一个表T有多少行数据,通常写法是: 查询A:select count(*) from T 但也可以采用下面语句来查: 查询B:select count(1) from T 结果通常是一样的.那么二 ...
- mysql 性能优化索引、缓存、分表、分布式实现方式。
系统针对5000台终端测试结果 索引 目标:优化查询速度3秒以内 需要优化.尽量避免使用select * 来查询对象.使用到哪些属性值就查询出哪些使用即可 首页页面: 设备-组织查询 优化 避免使用s ...
- codeforces_456C_dp
链接:http://codeforces.com/problemset/problem/456/C C. Boredom time limit per test 1 second memory lim ...
- Shell输入/输出重定向
输出重定向 重定向一般通过在命令间插入特定的符号来实现.特别的,这些符号的语法如下所示 command1 >file1 上面这个命令执行command1然后将输出的内容存入file1. 注意任何 ...
- Docker私有仓库的构建
[root@localhost ~]# vim /etc/sysconfig/docker #INSECURE_REGISTRY='--insecure-registry' INSECURE_REGI ...
- returnValue of Chrome
说实话,我一看到这个returnValue就有点反感,感觉这个就是IE式的老套的用法,因为项目中有用到就了解了下,以下主要是一些我的理解和发现吧. PS:returnValue是window的属性,s ...