sping AOP 注解形式与xml格式

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/>

<!--    &lt;!&ndash; 导入目标类 &ndash;&gt;-->
<!--    <bean id="personService" class="com.zhou.proxy2.PersonServiceImpl"></bean>-->
<!--    &lt;!&ndash; 导入切面 &ndash;&gt;-->
<!--    <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>
<!--        定义切点&#45;&#45;》要被插入的类-->
        <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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值