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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zhou.proxy2"/>
<context:annotation-config/>
<!-- <!– 导入目标类 –>-->
<!-- <bean id="personService" class="com.zhou.proxy2.PersonServiceImpl"></bean>-->
<!-- <!– 导入切面 –>-->
<!-- <bean id="myTransaction" class="com.zhou.proxy2.MyTransaction"></bean>-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="perform" expression="execution(* com.zhou.proxy2.PersonServiceImpl.*(..))"/>-->
<!-- <aop:aspect ref="myTransaction">-->
<!-- <aop:before method="beginTransaction" pointcut-ref="perform"></aop:before>-->
<!-- </aop:aspect>-->
<!-- <aop:aspect ref="myTransaction">-->
<!-- <aop:after method="commit" pointcut-ref="perform"></aop:after>-->
<!-- </aop:aspect>-->
<!-- </aop:config>-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
申明切面类
@Component("myTransaction")
@Aspect
//申明这是一个切面类
public class MyTransaction {
//标明切入表达式
@Pointcut("execution(* com.zhou.proxy2.PersonServiceImpl.*(..))")
public void aaa(){
}
//切面里的通知方法
// @Before("execution(* com.zhou.proxy2.PersonServiceImpl.*(..))")
//如果不用申明表达式也可以直接把切点的表达式给扔进去
//在拦截方法前
@Before("aaa()")
public void beginTransaction(){
System.out.println("开启事务 ");
}
//切面里的通知方法 在拦截方法后
@After("aaa()")
//切面里的通知方法
public void commit(){
System.out.println("提交事务");
}
}
切点接口以及实现类(jdk动态代理)
public interface PersonService {
public String savePerson();
public void updatePerson();
public void deletePerson();
}
//切点类
@Repository("personService")
public class PersonServiceImpl implements PersonService{
//要进行增强的方法
public String savePerson() {
System.out.println("添加");
return "保存成功!";
}
//要进行增强的方法
public void updatePerson() {
System.out.println("修改");
}
//要进行增强的方法
public void deletePerson() {
System.out.println("删除");
}
}
测试
public class Text {
@Test
public void test1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Pig pig = context.getBean("pig", Pig.class);
pig.hello();
PersonService personServiceImpl = context.getBean("personService", PersonService.class);
personServiceImpl.savePerson();
// String returnValue = proxyPersonService.savePerson();
// System.out.println(returnValue);
}
}
xml aop织入
把前面的注解该删的删了
xml aop更加直观 代码可读性更加高 解耦性更加的墙
注解的形式灵活度比较差 xml 可以任意搭配!!!!!1
<aop:config>
<!-- 定义切点--》要被插入的类-->
<aop:pointcut id="perform" expression="execution(* com.zhou.proxy2.PersonServiceImpl.*(..))"/>
<!-- 定义切面》插入切点的面-->
<aop:aspect ref="myTransaction">
<!-- 定义通知-->
<aop:before method="beginTransaction" pointcut-ref="perform"></aop:before>
</aop:aspect>
<aop:aspect ref="myTransaction">
<aop:after method="commit" pointcut-ref="perform"></aop:after>
</aop:aspect>
</aop:config>