Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
2017-03-18 14:49
701 查看
前段时间开发遇到一个这样的问题,使用<context:annotation-config/>开启注解服务后,发现@Controller,@Component,@Service等注解失效了,这里用代码演示一下问题出现的现象 |
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/tx/spring-util-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <!-- 开启注解服务 --> <context:annotation-config/> <beanid="animals"class="org.spring.test2.Animals"></bean> </beans> |
public class Animals { //@Autowired与@Resource功能相似,都是从spring获取实例 @Resource @Qualifier("cat") private Animalanimal; public void arouse(){ animal.arouse(); } } |
public interface Animal{ public void arouse(); } |
/** * 当前对象注入到spring容器中 */ @Service public class Cat implements Animal{ //给song赋值 @Value("CAT...SONG") private String song; @Override public void arouse() { System.out.println("Cat's song is ="+song); } } |
/** * 当前对象注入到spring容器中 */ @Service public class Dog implements Animal{ //给song赋值 @Value("DOG...SONG") private Stringsong; @Override public void arouse() { System.out.println("Dog's song is ="+song); } } |
发现会报一个这样的问题:Exception in thread "main"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'animals': Injection of resource dependencies failed; nested exception isorg.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.spring.test2.Animal] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=), @org.springframework.beans.factory.annotation.Qualifier(value=cat)} at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306) ba56 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117) at org.spring.test2.Test.main(Test.java:10) |
后来把<context:annotation-config/>替换成为<context:component-scanbase-package="org.spring.test2"/>发现所有问题解决了。 |
<context:annotation-config/>作用:开启注解服务。 <context:component-scan base-package="org.spring.test2"/>作用:1、开启注解服务,2、具有自动检测功能 |
相关文章推荐
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
- Spring <context:component-scan base-package="">和<context:annotation-config>
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.xx
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotati...
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.x
- <context:component-scan base-package="">与 <context:annotation-config/>的区别
- Spring配置文件详解一:<:<context:annotation-config/>与<context:component-scan base-package="com.xx
- Spring:<context:annotation-config/>与<context:component-scan base-package="com.xx />
- spring 中<context:annotation-config />和<context:component-scan base-package="com.*" >
- <context:component-scan base-package=""> 与 <context:annotation-config 区别
- Spring_<context:component-scan base-package="">和<context:annotation-config>学习
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <context:component-scan base-package=""> <context:annotation-config>
- spring mvc <mvc:annotation-driven/> 和<context:component-scan base-package=""/>解释
- [spring]Spring <context:component-scan base-package="">和<context:annotation-config>
- 合并注解配置 <context:component-scan base-package="com.**.controller" annotation-config="true"/>
- 关于使用注解的Spring配置文件的配置<context:annotation-config/>&&<context:component-scan base-package=”XX.XX”/>
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别