关于Spring中的<context:annotation-config/>配置
来源:互联网 发布:ubuntu 升级python 编辑:程序博客网 时间:2024/06/16 11:10
使用隐式地向 Spring容器注册
当然我们很多时候使用注解都会配置扫描路径选项:
<context:component-scan base-package="pack.pack"/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用componet-scan后,即可将annotation-config省去。
注意在使用context命名空间之前,必须在中声明context命名空间:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" ... xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ... <context:annotation-config/> </beans>
注意:
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@component 泛指组件(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class>
)
需要注入类的组件使用@Autowired或@Resource
@Autowired与@Resource的区别
1、@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:
@Autowired() @Qualifier("baseDao") private BaseDao baseDao;
3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定,
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
@Resource(name="baseDao") private BaseDao baseDao;
注:@Resource注解在字段上,且这个注解是属于J2EE的,减少了与spring的耦合。
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- 关于Spring中的<context:annotation-config/>配置
- SQL数据库面试题以及答案(50例题)
- Web--聊聊Web缓存机制
- 关于蓝牙mac地址规则
- 深入理解java中的组合和继承
- 【双11福利】文末评论免费送《零起点TensorFlow快速入门》
- 关于Spring中的<context:annotation-config/>配置
- Please change caller according to com.intellij.openapi.project.IndexNotReadyException documentation
- SourceTree 使用手册(第一篇)
- JAVA中ListIterator和Iterator详解与辨析
- 下三角矩阵的压缩存储
- Vijos 3736 监狱重排
- 微信小程序模板中使用循环
- 小demo
- 熔断