关于@Autowired注解和静态方法及new的关系
作者:徐浩刚 时间:2021-07-16 13:08:06
@Autowired注解和静态方法及new关系
一、@autowired 与new
new出来的对象无法调用@Autowired注入的Spring Bean,否则报空指针异常,
@Autowired注入Spring Bean,则当前类必须也是Spring Bean才能调用它,不能用new xxx()来获得对象,这种方式获得的对象无法调用@Autowired注入的Bean。
二、@autowired 与静态方法
1.spring框架应用中有些静态方法需要依赖被容器管理的类
像这样
public class CeErrorAlarm{undefined
@autowired
private static CeAlarmDao ceAlarmDao;
public static ceErrorAlarm(){undefined
ceAlarmDao.insert();
}
}
这样一定会报java.lang.NullPointerException: null异常。
2.原理剖析
静态变量、类变量不是对象属性,而是一个类的属性,所以静态方法是属于类(class)的,普通方法才是属于实体对象的(new出来的对象),spring注入是在容器中实例化对象,所以不能使用静态方法。
而使用静态变量、类变量扩大了静态方法的使用范围。
静态方法在spring中是不推荐使用的,依赖注入的主要目的是让容器去产生一个对象的实例,然后交给spring容器管理,在整个生命周期中使用他们,更加方便灵活
一旦你使用静态方法,就不再需要去产生这个类的实例,这会让testing变得更加困难,同时你也不能为一个给定的类,依靠注入方式去产生多个具有不同的依赖环境的实例,这种static field是隐含共享的,并且是一种global全局状态,spring同样不推荐这样去做。
3.解决办法
1.将@autowired注解加在构造方法上
public class CeErrorAlarm{undefined
private static CeAlarmDao ceAlarmDao;
@autowired
public CeErrorAlarm(CeAlarmDao ceAlarmDao){undefined
CeErrorAlarm.ceAlarmDao=ceAlarmDao;//将利用构造方法自动注入的对象赋值给static
}
public static ceErrorAlarm(){undefined
ceAlarmDao.insert();
}
}
2.用@PostConstruct注解
public class CeErrorAlarm{undefined
private static CeAlarmDao ceAlarmDao;
@Autowired
private CeAlarmDao ceAlarmDao2;
@PostConstruct
public void init() {undefined
ceAlarmDao=ceAlarmDao2; //原理类似
}
public static ceErrorAlarm(){undefined
ceAlarmDao.insert();
}
}
@PostConstruct:被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。(PreDestroy()方法在destroy()方法执行执行之后执行)
@Autowired和new对象有什么区别
为什么在new 对象里面使用自动注入对象会报空指针异常?
根本原因在于当Spring框架帮我们管理的时候就会自动的初始化接下来会用到的属性,而通过new对象的方式,在该new对象中使用到的一些实例就需要自己去做初始化,否则就会报空指针异常。
如下例子所示
TestService 通过@Autowired注入,那么Spring容器就会自动注入TestService 中会用到的TestDao。如例一所示。
例一:
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/print",method = RequestMethod.GET)
public void test() {
testService.test();
}
}
@Service
public class TestService {
@Autowired
private TestDao testDao;
public void test() {
testDao.test();
}
}
如果TestService 通过new对象方式新建的话,Spring容器就不会自动注入TestDao,此时testDao为null,会报空指针异常。此时就需要在TestService中自己new一个TestDao对象。如例二所示。
例二:
@RestController
@RequestMapping(value = "/test")
public class TestController {
private TestService testService = new TestService ();
@RequestMapping(value = "/print",method = RequestMethod.GET)
public void test() {
testService.test();
}
}
@Service
public class TestService {
@Autowired
private TestDao testDao;
public void test() {
TestDao testDao = new TestDao ();
testDao.test();
}
}
小结
在程序启动时,Spring会按照一定的加载链来加载并初始化Spring容器中的组件。
例如:在A中注入B,B中注入C。在A中调用B,来使用B中调用C的方法时,如果不采用自动注入,而是使用new对象方式的话,就会报空指针异常(因为B中的C并没有被初始化)。
来源:https://blog.csdn.net/xuhaogang3/article/details/81986665
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java泛型映射不同的值类型详解及实例代码
Java基础之二叉搜索树的基本操作
![](https://img.aspxhome.com/file/2023/9/61019_0s.png)
Android自定义View之组合控件实现类似电商app顶部栏
![](https://img.aspxhome.com/file/2023/3/84713_0s.png)
Java技能点之SimpleDateFormat进行日期格式化问题
![](https://img.aspxhome.com/file/2023/9/58339_0s.jpg)
SpringMVC的执行过程浅析
![](https://img.aspxhome.com/file/2023/4/62074_0s.png)
Java如何给Word文档添加多行文字水印
![](https://img.aspxhome.com/file/2023/2/58922_0s.png)
Java面向对象编程的三大特征
![](https://img.aspxhome.com/file/2023/8/58478_0s.png)
Java 守护线程_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/7/60177_0s.png)
MyBatis框架迭代器模式实现原理解析
java中javaBean与Bean的深入理解
浅谈Java(SpringBoot)基于zookeeper的分布式锁实现
浅谈MyBatis3 DynamicSql风格语法使用指南
IDEA报错:无效的源发行版解决方案
![](https://img.aspxhome.com/file/2023/4/62454_0s.png)
Java排序的那些事之sort方法的使用详解
![](https://img.aspxhome.com/file/2023/4/61014_0s.png)
SpringBoot项目中新增脱敏功能的实例代码
Jenkins的安装配置详解
![](https://img.aspxhome.com/file/2023/6/62166_0s.png)
Android实现人脸支付的示例代码
![](https://img.aspxhome.com/file/2023/0/86020_0s.gif)