@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. Linux环境下卸载、安装及配置MySQL5.1

    Linux环境下卸载原有MySQL5.1数据库,并重新安装MySQL数据库的示例记录. 一.卸载MySQL 查看主机中是否安装了MySQL数据库: [root@RD-viPORTAL- ~]# rpm ...

  2. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

  3. 390 Elimination Game 淘汰游戏

    详见:https://leetcode.com/problems/elimination-game/description/ C++: 方法一: class Solution { public: in ...

  4. 转】 Spark SQL UDF使用

    原博文出自于: http://blog.csdn.net/oopsoom/article/details/39401391 感谢! Spark1.1推出了Uer Define Function功能,用 ...

  5. 发生在升级OS X Yosemite后:修复各种开发环境

    本博文最初发布于我的个人博客<Jerry的乐园> 终于还是忍不住升级了,促使我升级的原动力居然是Alfred的Yosemite theme居然比初始theme好看很多!在升级前就预想到我的 ...

  6. SQL Split函数,将一串字符串返回成table

    写法一: CREATE FUNCTION [dbo].[Split] ( @str VARCHAR(MAX), --传进来的字符串 ) --分割符 ) RETURNS @t TABLE --定义一个虚 ...

  7. Spark学习之编程进阶——累加器与广播(5)

    Spark学习之编程进阶--累加器与广播(5) 1. Spark中两种类型的共享变量:累加器(accumulator)与广播变量(broadcast variable).累加器对信息进行聚合,而广播变 ...

  8. EditPlus里面自带有更改文件编码的功能

  9. OKHTTP 简单分析

    内部使用了OKIO库, 此库中Source表示输入流(相当于InputStream),Sink表示输出流(相当于OutputStream) 特点: ·既支持同步请求,也支持异步请求,同步请求会阻塞当前 ...

  10. 由DB2分页想到的,关于JDBC ResultSet 处理大数据量

    最近在处理DB2 ,查询中,发现如下问题.如果一个查询 count(*),有几十万行,分页如何实现 select row_number() over (order by fid desc ) as r ...