SpringAop切面的简单使用

配置和注解

版本spring4.2.4

                   ## 配置 ##

第一步:导入jar包
aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
commons-logging-1.1.3.jar
javax.annotation-3.1.2.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-context-support-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
第二步:建立实体类

package com.entity;

public class Log {
      public void loger(){
          System.out.println("loger....");
      }
      public void show(){
          System.out.println("方法结束...");
      }
}
package com.entity;

public class Test {
   public void abc(){
       System.out.println("abc..");
   }
}
package com.entity;

public class UserDao {
    public void save(){
        System.out.println("save");
    }
    public void add(){
        System.out.println("add");
    }
}

第三步:配置bean.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:p="http://www.springframework.org/schema/p" 
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-4.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="aa" class="com.entity.Log"></bean>
<bean id="userdao" class="com.entity.UserDao"></bean>
<bean id="test" class="com.entity.Test"></bean>

<aop:config>
    <aop:aspect id="loga" ref="aa">
        <aop:before method="loger" pointcut="execution(public * com.entity..*.*(..))"/>
        <aop:pointcut expression="execution(public * com.entity..*.a*(..))" id="yyy"/>
        <aop:pointcut expression="execution(public * com.entity..UserDao.*(..))" id="aaa"/>
        <aop:after method="show" pointcut-ref="aaa"/>        
    </aop:aspect>
</aop:config>

</beans>

第四步:建立测试类

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.entity.Test;
import com.entity.UserDao;

public class Demo {

public static void main(String[] args) {
      ApplicationContext     at= new ClassPathXmlApplicationContext("bean.xml");
         UserDao u=(UserDao) at.getBean("userdao");
                        u.add();    
                        u.save();
                        Test t=(Test) at.getBean("test");
                        t.abc();

}   
}
          ## 注解 ##

注意:jar包与上面相同

package com.entity;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Log {
    @Before("execution(public * com.entity..*.*(..))")
      public void loger(){
          System.out.println("loger....");
      }
    @After("execution(public * com.entity..*.a*(..))")
      public void show(){
         System.out.println("方法结束...");
      }
}
package com.entity;

import org.springframework.stereotype.Component;

@Component
public class Test {
   public void abc(){
       System.out.println("abc..");
   }
}
package com.entity;

import org.springframework.stereotype.Component;

@Component
public class UserDao {
    public void save(){
        System.out.println("save");
    }
    public void add(){
        System.out.println("add");
    }
}
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.entity.Test;
import com.entity.UserDao;

public class Demo {
    public static void main(String[] args) {
        ApplicationContext at = new ClassPathXmlApplicationContext("bean.xml");

        UserDao u = (UserDao)at.getBean("userDao");
        u.add();

        u.save();

        Test t = (Test)at.getBean("test");
        t.abc();

    }

}

bean.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:p="http://www.springframework.org/schema/p" 
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-4.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">


<context:annotation-config/>
<context:component-scan base-package="com"></context:component-scan>
<aop:aspectj-autoproxy/>
</beans>

测试类

<?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:p="http://www.springframework.org/schema/p" 
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-4.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">


<context:annotation-config/>
<context:component-scan base-package="com"></context:component-scan>
<aop:aspectj-autoproxy/>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值