Spring Framework(1):Bean的自动装配检测 & AOP简述
2015-05-07 19:25
399 查看
Bean的自动装配
注解可以充分利用Java的反射机制获取类结构信息,从而有效减少配置工作;注解与java代码位于同一个文件,便于对变动的统一维护;
1) Spring容器默认禁用注解装配,因此使用注解装配之前需要在配置文件中显示打开注解装配:
View Code
#1<aop:aspct表示定义一个切面,并且与名为audience的bean关联;
#2<aop:pointcut>表示定义一个名为performance的切点,并与调用perform方法触发绑定;
#3<aop:before>表示定义调用perform方法触发之前的动作;
#4<aop:after-returning>表示定义调用perform方法触发正常结束之后的动作;
#5<aop:after-throwing>表示定义调用perform方法抛出异常后的动作;
#6<aop:around>合并before和after-returning,PreceedingJoinPoint.proceed()为目标动作;
#7如果目标动作有参数,可以借助arg/arg-names在切面方法中获取;
3) Spring AOP也可以通过注解实现
#1首先要在配置文件中添加<aop:aspectj-autoproxy />;
#2然后通过@Aspect,@Pointcut,@Before,@AfterReturning,@Around等实现
注解可以充分利用Java的反射机制获取类结构信息,从而有效减少配置工作;注解与java代码位于同一个文件,便于对变动的统一维护;
1) Spring容器默认禁用注解装配,因此使用注解装配之前需要在配置文件中显示打开注解装配:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="audience" class="com.active.leo.helloworld.Audience" /> <bean id="instrumentAlist" class="com.active.leo.helloworld.InstrumentAlist" /> <bean id="instrument2" class="com.active.leo.helloworld.InstrumentImpl2"/> <aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression="execution(* com.active.leo.helloworld.Performer.perform(..))" /> <aop:before pointcut-ref="performance" method="takeSeats" /> <aop:before pointcut-ref="performance" method="turnOffCellPhones" /> <aop:after-returning pointcut-ref="performance" method="applaud" /> <aop:after-throwing pointcut-ref="performance" method="demandRefund" /> </aop:aspect> </aop:config> </beans>
View Code
#1<aop:aspct表示定义一个切面,并且与名为audience的bean关联;
#2<aop:pointcut>表示定义一个名为performance的切点,并与调用perform方法触发绑定;
#3<aop:before>表示定义调用perform方法触发之前的动作;
#4<aop:after-returning>表示定义调用perform方法触发正常结束之后的动作;
#5<aop:after-throwing>表示定义调用perform方法抛出异常后的动作;
#6<aop:around>合并before和after-returning,PreceedingJoinPoint.proceed()为目标动作;
#7如果目标动作有参数,可以借助arg/arg-names在切面方法中获取;
3) Spring AOP也可以通过注解实现
#1首先要在配置文件中添加<aop:aspectj-autoproxy />;
#2然后通过@Aspect,@Pointcut,@Before,@AfterReturning,@Around等实现
相关文章推荐
- Spring <bean>标签属性 Autowire自动装配
- 从头认识Spring-2.7 自动检测Bean(2)-过滤器<context:include-filter/>
- spring学习总结(五):IOC & DI 配置 Bean 之自动装配及bean之间的关系
- spring的自动装配Bean与自动检测Bean
- 从头认识Spring-2.7 自动检测Bean(3)-过滤器<context:exclude-filter/>
- spring的自动装配Bean与自动检测Bean
- Spring自动检测并装配bean
- Spring -- Bean自动装配&Bean之间关系&Bean的作用域
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用
- spring initializingBean vs init-method && 自动装配
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
- Quartz SchedulerFactoryBean异常--Spring自动装配惹的祸
- spring自动装配与spring_bean之间的关系(二)
- 11级_Java_曹建波 05.06 Spring的autowire自动装配bean的四种方式
- spring自动扫描装配bean
- Spring 自动装配 Bean
- 简化Spring的XML配置(一)——自动装配bean属性
- java Spring常用注解,自动扫描装配Bean
- Spring_ Spring_教程9_Spring中Bean的自动装配六种模式