Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别
<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过packagesanning的方式)上面的注解。(激活@Resource和@Autowired注解)
<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。(激活@Resource和@Autowired注解,同时可以配置扫描的包以激活@Service、@Controller等注解)
1.<context:component-scan>的作用:(开发中用这一个足以)
一方面可以配置扫描注解的包的路径,另一方面具有<context:annotation-config>的作用,也就是可以实现注解注入(Autowired与Resource等注解)。
例如:
目录结构:

ApplicationContext-test.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描注解的包-->
<context:component-scan base-package="cn.qlq.test" ></context:component-scan>
</beans>
package cn.qlq.test.dao; import org.springframework.stereotype.Repository; /**
* @Author: qlq
* @Description
* @Date: 22:49 2018/9/28
*/
@Repository
public class UserDao {
public void saveUser() {
System.out.println("save user");
}
}
package cn.qlq.test.service; import cn.qlq.test.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @Author: qlq
* @Description
* @Date: 22:52 2018/9/28
*/
@Service
public class UserService {
@Autowired
private UserDao userDao; public void saveUser() {
userDao.saveUser();
}
}
测试类:
package cn.qlq.test; import cn.qlq.test.service.UserService;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; /**
* @Author: qlq
* @Description
* @Date: 22:54 2018/9/28
*/
public class TestApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext-test.xml");
//第一种:用beanId获取bean
// UserService userService = (UserService) applicationContext.getBean(UserService.class);
//第二种:用bean的class获取bean
UserService userService = (UserService) applicationContext.getBean(UserService.class);
userService.saveUser();
}
}
结果:
save user
补充:
<context:component-scan....可以扫描多个包,逗号隔开就行,而且扫描包的时候会自动扫描子包。如下也是正确的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描注解的包-->
<context:component-scan base-package="cn.qlq.test.dao,cn.qlq.test.service" ></context:component-scan>
</beans>
2.<context:annotation-config>的作用与测试
<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。也就是激活@Autowired和@Resource注解。但是不会扫描@Service等注解配置的bean。
如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/> <bean id="userDao" class="cn.qlq.test.dao.UserDao"/>
<bean id="userService" class="cn.qlq.test.service.UserService"/>
</beans>
package cn.qlq.test.dao; import org.springframework.stereotype.Repository; /**
* @Author: qlq
* @Description
* @Date: 22:49 2018/9/28
*/
public class UserDao {
public void saveUser() {
System.out.println("save user");
}
}
package cn.qlq.test.service; import cn.qlq.test.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @Author: qlq
* @Description
* @Date: 22:52 2018/9/28
*/
public class UserService {
@Autowired
private UserDao userDao; public void saveUser() {
userDao.saveUser();
}
}
测试代码同上,可以正常运行。
如果我们修改xml为下面:(去掉bean定义)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描注解的包-->
<context:annotation-config/>
</beans>
结果报错没有找到bean:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.qlq.test.service.UserService] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:972)
at cn.qlq.test.TestApp.main(TestApp.java:17)
修改xml为如下:(去掉annotation-config标签)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userDao" class="cn.qlq.test.dao.UserDao"/>
<bean id="userService" class="cn.qlq.test.service.UserService"/>
</beans>
结果会报空指针异常,service中没有注入dao:
Exception in thread "main" java.lang.NullPointerException
at cn.qlq.test.service.UserService.saveUser(UserService.java:17)
at cn.qlq.test.TestApp.main(TestApp.java:18)
Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别的更多相关文章
- Spring注解详解@Repository、@Component、@Service 和 @Constroller
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- 学会使用Spring注解
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 ...
- Spring注解详解
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- spring注解和xml方式区别详解
一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 ...
- 【转】Spring注解详解
http://blog.csdn.net/xyh820/article/details/7303330/ 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类 ...
- Spring 注解详细分析解释有实例
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- Spring注解_详解
@Autowired 注释 将 @Autowired 注释标注在成员变量上 import org.springframework.beans.factory.annotation.Autowire ...
- Spring学习(五)——Spring注解(一)
---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...
- Spring @注解详解(转)
1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...
- Spring注解详解(转)
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
随机推荐
- json_decode()相关报错
错误描述 PHP Warning: json_decode() expects parameter 1 to be string, array given in xxx.php on line 29 ...
- MT【240】6*6放黑白子
$6*6$的方格中放三个完全相同的黑子和三个完全相同的白子,要求每行每列都有一个棋子,且每一格只有一个棋子.问有多少不同放法? 解:$\dfrac{36*25*16*9*4*1}{3!*3!}=144 ...
- 洛谷 P1691 有重复元素的排列问题 解题报告
P1691 有重复元素的排列问题 题目描述 设\(R={r_1,r_2,--,r_n}\)是要进行排列的\(n\)个元素.其中元素\(r_1,r_2,--,r_n\)可能相同.使设计一个算法,列出\( ...
- CF438E The Child and Binary Tree(生成函数,NTT)
题目链接:洛谷 CF原网 题目大意:有 $n$ 个互不相同的正整数 $c_i$.问对于每一个 $1\le i\le m$,有多少个不同形态(考虑结构和点权)的二叉树满足每个点权都在 $c$ 中出现过, ...
- CAN总线疑惑与解答
1 CAN总线2根数据线是怎么表示数据信息1和0的? Can总线采用差分数据表示方法,平时2个数据线为2.5V,表示隐性(1).当用数据0(显性)需要发送时1跟数据线上升到3.5V另一个下降到1 ...
- 右值引用&&
以下内容参考https://blog.csdn.net/china_jeffery/article/details/78520237 右值引用若不作为函数参数使用,基本等于滥用 右值引用 (Rvalu ...
- echarts 取消图例上的点击事件和图表上鼠标滑过点击事件
备注:标黄的代码是起作用的代码,其他的不需要借鉴!!! //取消 鼠标滑过的提示框 tooltip : { trigger: 'item', show:false, ...
- Jenkins插件获取git分支的方法
1.旧版本的Jenkins可以使用Dynamic Choice Parameter插件: 使用方法: Jenkins--->dev-h5-server--->配置--->参数化构建过 ...
- Logstash配置文件介绍
Logstash配置文件介绍 Logstash配置文件有两种,分别是pipeline配置文件和setting配置文件. Pipeline配置文件主要定义logstash使用的插件以及每个插件的设置,定 ...
- 网络编程基础【day09】:实现简单地ssh(四)
本节内容 概述 简单ssh socket接收大数据的困惑 一.概述 我们用过linux的就知道什么是ssh,它是一种客户端和服务端交互返回的一个解决,输入一个命令,给我返回什么,接下来我们说一说,如何 ...