我们认识完动态代理之后,就可以正式使用spring里的AOP了,实现spring的AOP有两种方式:
1)基于xml文件
2)基于注解
1. 通过配置XML文件,实现AOP.
用到的接口及API:
前置通知: MethodBeforeAdvice
public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable{}
后置通知: AfterReturningAdvice
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable
MethodInterceptor
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object result = methodInvocation.proceed();
return result;
}
用于处理事务: ThrowsAdvice
public void afterThrowing([Method method, Object[] args, Object target, Exception ex)
配置XML文件
<bean id="userTarget" class="code.bean.UserA"></bean> //真实角色
<bean id="methodbeforeTAdvice" class="code.bean.BeforTest"></bean>
<bean id="afterTest" class="code.bean.AfterTest"></bean> //后置通知
<bean id="user" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"> //真实角色的接口s
<value>code.bean.User</value>
</property>
<property name="target" ref="userTarget"></property>//代理目标
<property name="interceptorNames"> //嵌入通知
<list>
<value>afterTest</value>
<value>methodbeforeTAdvice</value>
</list>
</property>
</bean>
如果在org.springframework.aop.framework.ProxyFactoryBean 的<bean>中未配置proxyInterfaces那么就会自动通过CGglib实现
2. 用注解的方式实现AOP
1)@component基本注解,标识了一个受Spring管理的组件
2)@Reponsitory 标识的是持久层的组件
3)@service标识的是服务层的组件
4)@Controller 控制层的组件
5)@aspect 表示切面
依赖关系的注解(组件的装配)
1)@AutoWired -----常用
2) @Resource -----常用
3) @Inject
用法示栗~
@Aspect
@Order(2)
@Component
public class Myaspect {
@Pointcut("execution(public * login(..)) && target(com.neu.controller.UserController)")
public void pointCut(){}
@Before("execution(public * login(..)) && target(com.neu.controller.UserController)")
public void before(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName());
System.out.println("before");
}
@After("pointCut()")
public void after(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName());
System.out.println("after");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable{
System.out.println("before..");
Object object=point.proceed();
System.out.println("after..");
return object;
}
}
注意:切面类一定要添加@Component这样才会被XML扫描到
这样写需要在xml里面配置:
1) 注意 添加命名空间
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
2) 设置扫描路径
context:component-scan:自动扫描注解的配置文件
base-package:制定一个需要的包,如果多个包之间用,隔开
resource-pattern:更加精确的去确定扫描那个包下的哪个类
<context:component-scan base-package="com.neu" [resource-pattern="" use-default-filters="true"]>
[<context:exclude-filter type="annotation" expression=""/>] --按注释排除
[<context:include-filter type="assignable" expression=""/>]--按接口单一包含
</context:component-scan>
3)设置切面自动代理(在设置@Aspect和@Before等的前提下)
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
如果不设置@Aspect和@Before的话,也可以通过只配置xml的方式实现AOP
<bean id="aspect" class="com.neu.main.Myaspect"></bean>
<aop:config>
<aop:pointcut expression="execution(public * login(..)) and target(com.neu.controller.UserController)" id="pointcut"/>
<aop:aspect ref="aspect" order="1">
<aop:after method="before" pointcut-ref="pointcut"/>
<aop:before method="after" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
5. Spring提供了两种类型的IOC容器实现
1)BeanFactory :IOC容器最基本的实现
2) ApplicationContext:Web项目常用的获取bean的方式,特性比较高级,ClassPathXmlApplicationContext这个是用的最多的一种实现方式
4. Spring的注入数值问题:
1)字面值String:value
2) 基本数据类型:字面值
3)引用的是引用类型 Ref这个属性
4)如果字面值包含了特殊的字符CDATA节进行包裹,<![CDATA[]]>
附录:
Spring AOP切入点的表达式
1)execution(修饰符? 返回值类型 声明类型? 方法名称(方法的参数) 异常类型? )
1 执行任意的public方法
execution(public * * (..))
2 执行任意的get开头的方法
execution(* get*(..))
3 执行Service包下的所有的类中的任何方法
execution(* com.neusoft.service.*(..))
4 执行至少有一个参数,并且第一个参数的类型为int类型的方法
execution( * * (java.lang.Integer,..))
5执行Service包下及其子包下的所有的方法
execution(* com.neusoft.service..(..))
6 执行可能会发生异常的所有的方法
execution(* * (..) throws Exception)