Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别

来源:互联网 发布:淘宝联盟怎么查订单号 编辑:程序博客网 时间:2024/06/06 09:39

前段时间开发遇到一个这样的问题,使用<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容器中 */@Servicepublic class Cat implements Animal{     //给song赋值     @Value("CAT...SONG")     private String song;     @Override     public void arouse() {          System.out.println("Cat's song is ="+song);     }}


/** * 当前对象注入到spring容器中 */@Servicepublic 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)     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、具有自动检测功能
0 0
原创粉丝点击