Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
来源:互联网 发布:淘宝联盟怎么查订单号 编辑:程序博客网 时间:2024/06/06 09:39
<?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-scan base-package="org.spring.test2"/>作用:1、开启注解服务,2、具有自动检测功能
0 0
- Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
- <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.xx
- <context:component-scan base-package=""> <context:annotation-config>
- [spring]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/>的用法
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- Spring <context:component-scan base-package="">和<context:annotation-config>
- spring 中<context:annotation-config />和<context:component-scan base-package="com.*" >
- <context:component-scan base-package="com.a" /> 与<context:annotation-config />
- Spring_<context:component-scan base-package="">和<context:annotation-config>学习
- spring的<context:component-scan base-package=""/>用法
- spring中的<context:component-scan base-package="" />的作用
- Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
- Spring <context:annotation-config>和<context:component-scan>区别
- Hadoop实践(四)---Hadoop集群测试(MR样例)
- _dl_start_user源码分析(一)
- 剑指offer: 二叉树中和为某一值的路径
- ansbile 创建 主机之间的信任关系
- 求n个数最小公倍数
- Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
- ScrollView嵌套RecyclerView滑动冲突,禁止RecycleView滑动
- jqeury验证表单
- HTML+CSS+JavaScript网络相册【有缩略图】
- [ACM_HDU_1297递归推导]Children’s Queue
- linux驱动学习(字符型设备驱动)
- ios App启动加载广告页面思路
- viewPager无限轮播效果(留待以后填坑)
- 数据库和数据仓库