@Autowired注解和静态方法
@Autowired注解入static属性时,出现NullPointerException异常。
使用构造方法可解决:
@Component
public class Test { private static UserService userService; @Autowired
public Test(UserService userService) {
Test.userService = userService;
} public static void test() {
userService.test();
}
}
使用@PostConstruct注解解决:
@Component
public class Test { private static UserService userService; @Autowired
private UserService userServiceAdd; @PostConstruct
public void beforeInit() {
userService = userServiceAdd;
}
}
@Autowired注解和静态方法的更多相关文章
- @Autowired注解和静态方法 NoClassDefFoundError could not initialize class 静态类
NoClassDefFoundError could not initialize class 静态类 spring boot 静态类 java.lang.ExceptionInInitializer ...
- @Autowired 注解详解
前言 我们平时使用 Spring 时,想要 依赖注入 时使用最多的是 @Autowired 注解了,本文主要讲解 Spring 是如何处理该注解并实现 依赖注入 的功能的. 正文 首先我们看一个测试用 ...
- Spring IoC @Autowired 注解详解
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 我们平时使用 Spring 时,想要 依赖 ...
- Spring5:@Autowired注解、@Resource注解和@Service注解
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- @Autowired注解的使用
使用Spring时,通过Spring注入的Bean一般都被定义成private,并且要有getter和setter方法,显得比较繁琐,增加了代码量,而且有时会搞忘造成错误. 可以使用@Autowire ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- 转:Spring中@Autowired注解、@Resource注解的区别
Pay attention: When using these annotations, the object itself has to be created by Spring context. ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- @Resource 和 @Autowired注解的异同
@Resource 和 @Autowired注解的异同 @Autowired 默认按类型装配,默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false 例 ...
随机推荐
- 机器学习笔记之二-win10+cuda9.1+CUDNN7+Anaconda3+VS2017+tensorflow1.5+opencv3.4
[Tensorflow]环境搭建vs2017+win10+py3.6+cuda9.1+cudnn7+tf1.5 一.安装cuda 9.1+VS2017 一路下一步即可,环境变量cuda会自动配好 ...
- 微信小程序tab栏切换
Wxml代码:<view class="body"> <view class="nav bc_white"> <view clas ...
- java序列化和反序列化中的serialVersionUID有啥用
1.什么是序列化和反序列化 序列化就是将java对象转成字节序列的过程:反序列化就是将字节序列转成java对象的过程. java中,序列化的目的一种是需要将对象保存到硬盘上,一种是对象需要在网络中传 ...
- SSM的搭建
1.首先是工具的准备. eclipse jdk1.7 maven 3.5.4 tomcat 8.5 2.工具环境的搭建 首先,new建立选择maven project工程,勾选simple proje ...
- C罗转会尤文图斯
皇家马德里头号球星C罗转会意甲尤文图斯,结束了9年的皇马生涯,已获得5座金球奖.
- HTML5 使用 JS 生成二维码,带头像
一般在项目开发中,前端显示给用户扫描的二维码基本都是由后端代码生成的,那么这个高大上的功能能不能用 JS 来绘制呢? 答案是肯定的 首先我们需要一个插件 jquery.qrcode.js,该插件基于 ...
- 在aspx中,如果要引用一个ID号,需要引用外层的ID号(内层的不行)
- python读取excel,返回dic列表
def get_xls_sheets_as_dic(pro_name, xls_name): dic_list = [] xls_path = os.path.join(BASE_PATH, &quo ...
- Python module ---- argparse
argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行参数,程序只需定义好它要求的参数,然后argpars ...
- __proto__、prototype和原型对象
一.__proto__ 对象内部存在一个指针,用来指向上一层函数的原型对象.ECMA-262第五版中关这个指针叫[[prototype]],但Firefox.Safari和Chrome在每个对象上都支 ...