经过这段时间的学习,不能讲完全掌握了Spring,实际上就算是Spring框架常用的知识点,我也有很多没有学会。但还是想做个总结。
1、AOP,自己随便写一段配置文件
<bean id="aspect" class="......Aspect"/>//这是切面类的bean
<bean id="service" class="......Service"/>//这是目标类的bean
//配置AOP
<aop:config>
//配置切点,包括切点名和哪些方法
<aop:pointcut id="pointcut" expression="excution(* cn.zzh.service.*.*(..))" />
//配置切面,主要以何种方式增强,增强的方法名,增强的切点名
<aop:aspect ref="aspect">
<aop:before method="addBeforeLog" pointcut-ref="pointcut" />
</aop:sapect>
</aop:config>
2、注解
@Autowired
自动装配对象,容器会根据代码自动寻找对象之间的依赖关系,xml文件中只需要配置bean的基本信息就好。注意:xml中需要使用<context:annotation-config/>来开启自动装配,因为spring默认是关闭的。
@Qualifier
属性如果是接口,@Autowired不能判断具体要注入哪一个实现类,此时用这个注解。案例:
@Autowired
@Qualifier(dao1)
private UserDao dao;
dao1是配置文件中的bean的id,实现了UserDao接口。
最后还有一个标签<context:component-scan/>,和<context:annotation-config/>一样开启自动装配,并且能自动检测bean和定义bean,不过需要指定包。这个标签已经不需要再xml中使用bean标签,不过需要在bean中使用注解,以下是常用组件声明注解。
@component:标识该组件为Spring组件,不利于bean的分类管理;
@Controller:Spring MVC组件中使用,标识控制器;
@Service:声明业务组件;
@Repository:标识持久层组件。
Fighting!