对Spring <context:annotation-config/>的理解
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
例如:
如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
传统声明方式如下
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。
同样,传统的声明方式如下:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,我们使用注解一般都会配置扫描包路径选项
<context:component-scan base-package=”XX.XX”/>
(用来扫描哪些包有注释,并开启注入)
该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
注意:
当Spring注入中还包涵其他Spring注入,则<context:annotation-config/>不能省略,<context:annotation-config/>还起到连线的作用,会优先把包涵在最里面的注入优先注入。
因为Spring注入是没有先后顺序的,当最外层的Spring注入完成,而被包涵的注入没有注入,则会报java.lang.NullPointerException
如
这是一个Conrtoller
@Controller
@RequestMapping("/front/user/view.shtml")
public class ContrlUser {
@Autowired
private IServiceUser serviceUser; @RequestMapping(params = "act=view")
public void view(int id, HttpServletRequest request, HttpServletResponse response) throws IOException{
User user = serviceUser.getUser(id);
response.getWriter().write(user.getName());
}
}
这是Serviece
public class ServiceUserImpl implements IServiceUser{
@Autowired
private UserDao userDao;
public User getUser(int id) {
System.out.println("ServiceUserImpl"+id);
User user = userDao.getUser(id);
return user;
}
public List<User> listUser() {
return null;
}
public void editUser(User user) {
}
public void deleteUser(int id) {
}
}
Controller中有Spring注入,需注入Serviece 而Service中有Dao的Spring注入
当省略<context:annotation-config/>后 由于Controller中要注入serviceUser,当serviceUser优先注入后,而Dao还没有注入,此时就会报java.lang.NullPointerException。
对Spring <context:annotation-config/>的理解的更多相关文章
- spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
在运行一个第三方公司交付的项目的时候, 出现: Caused by: java.lang.IllegalStateException: Context namespace element 'annot ...
- Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别
Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...
- Spring配置之context:annotation与、component-scan以及annotation-driven
spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...
- 实现运行在独立线程池的调度功能,基于Spring和Annotation
使用Spring的注解(@Scheduled)声明多个调度的时候,由于其默认实现机制,将导致多个调度方法之间相互干扰(简单理解就是调度不按配置的时间点执行). 为了解决该问题尝试了修改线程池大小,但是 ...
- Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...
- 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...
- Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser]
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- Spring Security Java Config Preview--官方
原文地址:[1]https://spring.io/blog/2013/07/02/spring-security-java-config-preview-introduction/ [2]https ...
- Alibaba Nacos 学习(二):Spring Cloud Nacos Config
Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...
- class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
在搭建SSM项目时报了以下的错误: 06-Oct-2019 11:55:52.109 信息 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina. ...
随机推荐
- OpenGL在什么样的领域才是主角?
从OpenGL入门到现在掌握OpenGL开发(仅仅是掌握而已).随着对OpenGL理解的加深,也一点点的了解OpenGL所涉及的行业,有些行业OpenGL是主角,有些行业OpenGL是配角 ...
- 三、Java基础---------关于继承、构造函数、静态代码块执行顺序示例讲解
在上节博客中曾提到过类的继承,这篇文章主要是介绍类的继承.构造函数以及静态代码块的执行顺序. 首先接着分析在黑马基础测试中的一个关于继承的题目,题目描述如下: 声明类Person,包含2个成员变量:n ...
- MessageDigest
转: 我们知道,编程中数据的传输,保存,为了考虑安全性的问题,需要将数据进行加密.我们拿数据库做例子.如果一个用户注册系统的数据库,没有对用户的信息进 行保存,如,我去页面注册,输入"Vic ...
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml)
我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为json,xml等),但是如果Controller的自动序列化后的结果不是我们想要的该 ...
- iOS - 代码查看控制台打印内存使用情况:
1.先导入: #import <mach/mach.h> 2.写此方法.单位为兆(M). void report_memory(void) { struct task_basic_info ...
- 161017、SQL必备知识点
经典SQL语句大全 基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql ser ...
- jquery选择器中两个class是什么意思?
jquery选择器中两个class是什么意思? $(".class1 .class2") 选择class1元素下class2的元素(中间有空格)$(".class1.cl ...
- TI BLE CC2541的I2C主模式
由于要写TM1680, 写命令跟写数据, 所以需要使用CC2541的I2C, 2541是有硬件I2C的. tm1680.c: #include "tm1680.h" //TM168 ...
- TM1680的I2C的51例程
搞到一个例程,虽然是51的, 但是我的ST版本也是用的模拟I2C, 分析一下吧: unsigned char i=0;TM1680start(); //I2C起始信号 TM1680SendByte( ...
- Best Time to Buy and Sell Stock
class Solution { public: int maxProfit(vector<int>& prices) { //eg: 5 6 2 3 1 4: // 记录i之前最 ...