AOP(注解)
beans.xml配置文件。下面的三个配置是自动的.
<?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:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 开启注解 -->
<context:annotation-config/>
<!-- 开启组键扫描 -->
<context:component-scan base-package="com.mi"></context:component-scan>
<!-- 开启aop配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
1.以下是实现类中的方法,相当于server中,我只是使用一个实现类来代替:
其中也需要@component注释
package com.mi.dao.impl;
import org.springframework.stereotype.Component;
@Component
public class UserImpl {
public void add(){
System.out.println("add");
}
public void add02(int i ){
System.out.println("参数int");
}
public int add03(){
System.out.println("返回值int");
return 2;
}
public void add04 (){
System.out.println("add04");
}
public void add05(){
System.out.println("add05");
}
}
2.测试类中的代码
需要使用BeanFactory来创建,(兵工厂)
package com.mi.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mi.beans.User;
import com.mi.dao.impl.UserImpl;
public class Test {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
UserImpl impl = beanFactory.getBean(UserImpl.class);
impl.add();
impl.add02(2);
impl.add03();
impl.add04();
impl.add05();
}
}
3.AOP.java代码
其中
1.@component的作用是:将该class实例化到Spring容器中,相当 于xml配置中的< bean>标签
2.@Aspect为自动配置切面的的注释
3.@Pointcut为切入的方法
4.@Before表示为该方法之前序执行的方法,这里引用前面的method().
package com.mi.AOP;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Aop {
@Pointcut("execution(void add02(int))")
public void method() {
}
@Before("method()")
public void before() {
System.out.println("前置通知**");
}
// @AfterReturning("")
// public void returning() {
// System.out.println("后置通知");
// }
}
在上面的方法部分,可以有下面的几种方法:
/**
* 参数部分:
* void test() 切入没有参数,没有返回值的test方法
* void test(int) 切入参数为一个的int类型,没有返回值的test方法
* void test(int,java.lang.String) 切入参数为一个的int类型,一个为String类型,没有返回值的test方法
* void test(*) 切入任意的有一个参数的,没有返回值的test方法
* void test(..) 切入任意参数列表,没有返回值的test方法
*
* 方法名字:
* void test*(..) 切入以test开头的任意类型的参数,没有返回值的test方法
* void *(..) 切入任意方法 任意类型的参数,没有返回值的方法
*
* 返回值类型:
* int *(..)
* * *(..)
*
* 包的部分
* execution(public * com.sram.service..(..) 切入service包下面的任何类中的任何方法,不包括字包中的方法
* execution(public * com.sram.service...(..) 切入service包下面的任何类中的任何方法,包括字包中的任意类的任意方法
*/