spring学习之---Annotation
来源:互联网 发布:卡巴斯基 知乎 编辑:程序博客网 时间:2024/05/20 14:39
1.搜索Bean类
- @Component:标注 一个普通的spring Bean类。
- @Controller:标注一个控制器组件类。
- @Service:标注一个业务逻辑组件类。
- @Repository:标注一个DAO组件类。
指定了某些类可作为spring Bean类使用后,最后还需要让spring搜索指定路径,在配置文件中指定搜索路径,需要在spring配置文件中导入context Schema:
<context:component-scan base-package="">
在基于Annotation的方式下,spring采用约定的方式来为这些Bean实例指定名称,这些Bean实例的名称默认是Bean类的首字母小写,其他部分不变。也允许在使用@Component标注时指定Bean实例的名称:@Component(“axe”)。
还可通过为<component-scan.../>
元素添加<include-filter.../>或<exclude-filter.../>
子元素来指定spring Bean类,只要位于指定路径下的Java类满足这种规则,即使这些Java类没有使用任何Annotation标注,spring一样会将它们当成Bean类来处理。使用这两个元素要求指定如下属性:
- type:指定过滤器类型。
- expression:指定过滤器所需要的表达式。
<context:component-scan base-package=""> <context:include-filter type="regex" expression=".*Chinese"/> <context:exclude-filter type="regex/annotation/assignable/aspectJ" expression=""/> </context:component-scan>
2.指定Bean的作用域
@Scope Annotation来指定Bean实例的作用域。
@Scope(“prototype”),默认是singleton
3.使用@Resource配置依赖
@Resource位于javax.annotation包下,通过使用该Annotation为目标Bean指定协作者Bean。
@Resource有一个name属性,在默认情况下,spring将这个值解释为需要被注入的Bean实例的id。不仅可以修饰setter方法,也可以直接修饰实例变量。
spring允许使用@Resource时省略name属性,当使用省略name属性的@Resource修饰setter方法时,name属性值默认为该setter方法去掉前面的set子串、首字母小写后得到的子串。
4.使用@PostConstruct和@PreDestroy定制生命周期行为
public class Chinese{ @PostConstruct public void init(){ } @PreDestroy public void close(){ } }
5.spring3.0新增的注解
- @DependsOn可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean。@DependsOn({“steelAxe”,”abc”})
- @Lazy修饰spring Bean类用于指定该Bean的预初始化行为,使用该注解时可指定一个boolean的value属性,该属性决定是否取消预初始化。
6.spring4.0增强的自动装配和精确装配
@Autowired注解来指定自动装配,可以修饰setter方法、普通方法、实例变量和构造器等。使用@Autowire标注setter方法时,默认采用byType自动装配策略。
@Autowire注解还可以根据泛型进行自动装配。
public class BaseDaoImpl<T> implements BaseDao<T>{ public void save(T e){ System.out.println("..."); } } @Component("userDao") public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{ } @Component("itemDao") public class ItemDaoImpl extends BaseDaoImpl<Item> implements ItemDao{ } public class BaseServiceImpl<T> implements BaseService<T>{ @Autowire private BaseDao<T> dao; public void addEntity(T entity){ System.out.println("调用" + dao + "保存实体:" + entity); } } @Component("userService") public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{ }
UserServiceImpl继承了BaseServiceImpl<User>
,这就相当于指定了上面BaseDao<T>
类型中T的类型为User,因此spring会在容器中寻找类型为BaseDao<User>
的Bean—此时会找到UserDaoImpl组件,这就实现了将UserDaoImpl注入UserServiceImpl组件。
为了实现精确地自动装配,spring提供了@Qualifier注解,通过使用该注解,允许根据Bean的id来执行自动装备。
- spring学习之---Annotation
- spring之annotation
- Spring学习笔记三之IOC(annotation实现)
- Spring学习笔记(三)-----IOC之Annotation
- java学习之annotation
- Java之Annotation学习
- Spring框架学习【Annotation 介绍】
- Spring之AOP Annotation方式
- spring annotation 之 自动装配
- Spring 之 Annotation-->>@Autowired & @Qualifier
- Spring之AOP Annotation方式
- Spring实例解析之Annotation
- spring 学习之四(spring的注入方式(xml和annotation))
- spring零配置(Annotation)学习笔记
- spring学习笔记(8)--xml annotation
- spring学习笔记(13)--aop annotation
- spring学习笔记(14)--AOP Annotation
- Spring IOC Annotation 注入 学习实例
- Android OKhttp3缓存处理
- 10:大整数加法
- 通过jQuery Ajax使用FormData对象上传文件
- mysql-2-事务控制
- 使用LoadBalancerClient就行服务消费
- spring学习之---Annotation
- 前端试题大综合练习(三)
- adc mda 的一些配置,,,非常感谢分享
- 无锁编程:lock-free原理;CAS;ABA问题
- POJ 3616 Milking Time(DP)
- jquery中关于属性的学习
- SlidingPaneLayout
- 前后分离--异常回调
- Codeforces 340E 错排问题dp