spring—aop

1、导入 aop的约束

<?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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

2、创建需要被增强的类和方法

public class User {
    public void add(){
        System.out.print("UserMethod is Running!");
    }
}

3、创建增强的类和方法

public class strongUser {
    public void before(){
        System.out.print("strongUser before is running");
    }
    public void after(){
        System.out.print("strongUser after is running");
    }
}

4、spring的核心配置文件配置,实现类的增强

(1)定义切点

<aop:pointcut id="pointcut" expression="execution(* com.wm103.aop.User.*(..))"/>

execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?) 除了返回类型模式、方法名模式和参数模式外,其它项都是可选的。

举例几个主要使用: 
execution(public * *(..)):匹配所有类的public方法; 
 execution(* com.wm103.dao.*(..)):匹配指定包(这里指com.wm103.dao)下的所有类的方法,不包含子包; 
execution(* com.wm103.dao..*(..)):匹配指定包以及其下子孙包下的所有类的方法,值得注意的是,“..”出现在类名中时,后面必须跟“*”,表示包、子孙包下的所有类; 
 execution(* com.wm103.dao.UserDaoImpl.*(..)):匹配指定类下的所有方法; 
execution(* com.wm103.dao.UserDao+.*(..)):匹配实现指定接口(这里指com.wm103.dao.UserDao接口)的所有类的方法; 
execution(* save*(..)):匹配所有指定前缀(这里指的是save)开头的方法; 
execution(* *.*(..)):匹配所有类的所有方法。

(2)定义切面

 增强的顺序默认按照xml中配置的顺序运行。

<aop:aspect ref="logHandler" order="2">
    <!-- 2.3 针对切入点,配置通知/增强 -->
    <aop:before method="before" pointcut-ref="pointcut"/>
    <aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>

<aop:aspect ref="timeHandler" order="1">
    <aop:before method="before" pointcut-ref="pointcut"/>
    <aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>

(3)具体配置

<bean id="user" class="com.spring.aop.User"></bean>
<bean id="strongUser" class="com.spring.aop.StrongUser"></bean>
<!--AOP的配置-->
<aop:config>
    <!--切入点-->
    <aop:pointcut id="pointcut1" expression="execution(* com.spring.aop.User.*(..))"></aop:pointcut>
    <!--切面-->
    <aop:aspect ref="strongUser">
        <!--方法增强-->
        <aop:before method="before" pointcut-ref="pointcut1"></aop:before>
        <aop:after method="after" pointcut-ref="pointcut1"></aop:after>
    </aop:aspect>
</aop:config>

5、解决问题spring加载配置文件报错: Cannot create inner bean '(inner bean)' of type

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.2</version>

</dependency>

6、注解方式实现

(1)核心配置文件

<!--注解方式开启注解扫描-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

(2)切面---要增强的方法

@Aspect
public class TimeCounter {
    @Before(value="execution(* com.spring.aop.User.*(..))")
    public void before(){
        long time = new Date().getTime();
        System.out.println("现在时间"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
    }
    @After(value="execution(* com.spring.aop.User.*(..))")
    public void after(){
        long time = new Date().getTime();
        System.out.println("现在时间"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值