1.spring的切面编程
概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115
2.所需要的jar包
maven引入jar包(aspectj+aopalliance+cglib+spring):另外还有log日志jar包
<properties> <spring.version>4.1.6.RELEASE</spring.version> <aspectj.version>1.8.4</aspectj.version> <aopalliance.version>1.0</aopalliance.version> <cglib.version>3.1</cglib.version> </properties> <dependencies> <!--Spring 系列--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <!--log4j 2--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.4.1</version> </dependency> <!--aspect,织入库,Spring依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <!--aspect库,Spring依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <!-- aop联盟定义的一组关于AOP的公共接口,Spring Aop依赖它 --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${aopalliance.version}</version> </dependency> <!--Cglib代理,Spring依赖--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> </dependencies>
3.自己写的hello World(不知道这里写的@Before前置通知并没有运行,还请懂得指点一下)
编写一个切面bean用于管理切面(基于注解) HelloWorldAspact :
package com.raipeng.work.spring.aspact; import org.apache.logging.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * Created by 111 on 2015/11/21. */ @Aspect @Component public class HelloWorldAspact { //使用的是log4j2 private Logger logger = org.apache.logging.log4j.LogManager.getLogger(HelloWorldAspact.class); @Pointcut("execution(* com.raipeng.work.spring.model.HelloWorldServiceImp.*(..))") private void anyMethod(){ } @Before("anyMethod()") public void Advice(JoinPoint joinPoint){ System.out.println("this is before Advice"); } @Around("anyMethod()") public void aroundAdvice(ProceedingJoinPoint joinPoint){ System.out.println("this is around Advice"); logger.info("the method {}.{}() run", joinPoint.getTarget().getClass().getSimpleName(), joinPoint.getSignature().getName()); } @After("anyMethod()") public void afterAdvice(){ System.out.println("this is after Advice"); } @AfterReturning("anyMethod()") public void afterReturningAdvice(){ System.out.println("this is afterReturning Advice"); } }
在spring的配置文件中配置如下(必须加上<aop:aspectj-autoproxy/>):
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <aop:aspectj-autoproxy/> <context:component-scan base-package="com.raipeng.work.spring.aspact"/> <bean id="helloWorld" class="com.raipeng.work.spring.model.HelloWorldServiceImp"> </bean> <!--<bean id="helloWorldAspact" class="com.raipeng.work.spring.aspact.HelloWorldAspact"></bean>--扫描包加入了@Component就不需> </beans>
需要aop监控的类的接口 HelloWorldService
package com.raipeng.work.spring.model; /** * Created by 111 on 2015/11/21. */ public interface HelloWorldService { void hello(String name); }
上面接口的实现类 HelloWorldServiceImp:
package com.raipeng.work.spring.model; /** * Created by 111 on 2015/10/26. */ public class HelloWorldServiceImp implements HelloWorldService { @Override public void hello(String name){ System.out.println(name+",hello!"); } }
测试方法如下:
package com.raipeng.work.spring.test; import com.raipeng.work.spring.model.HelloWorldService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by 111 on 2015/11/19. */ public class MainTest { public static void main(String[]args){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorldService helloWorld = (HelloWorldService)applicationContext.getBean("helloWorld"); helloWorld.hello("xl"); } }
控制台信息:
"D:\Program Files\Java\jdk1.7.0_13\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:60621,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.7.0_13\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jce.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jfxrt.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\resources.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\rt.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\zipfs.jar;D:\cxlwork\STSworkspace\work-test\target\classes;D:\app\repository\org\springframework\spring-core\4.1.6.RELEASE\spring-core-4.1.6.RELEASE.jar;D:\app\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\app\repository\org\springframework\spring-context\4.1.6.RELEASE\spring-context-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-context-support\4.1.6.RELEASE\spring-context-support-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-beans\4.1.6.RELEASE\spring-beans-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-expression\4.1.6.RELEASE\spring-expression-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-aspects\4.1.6.RELEASE\spring-aspects-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-aop\4.1.6.RELEASE\spring-aop-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-jdbc\4.1.6.RELEASE\spring-jdbc-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-orm\4.1.6.RELEASE\spring-orm-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-tx\4.1.6.RELEASE\spring-tx-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-web\4.1.6.RELEASE\spring-web-4.1.6.RELEASE.jar;D:\app\repository\org\springframework\spring-webmvc\4.1.6.RELEASE\spring-webmvc-4.1.6.RELEASE.jar;D:\app\repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar;D:\app\repository\org\apache\logging\log4j\log4j-api\2.4.1\log4j-api-2.4.1.jar;D:\app\repository\org\apache\logging\log4j\log4j-core\2.4.1\log4j-core-2.4.1.jar;D:\app\repository\org\aspectj\aspectjweaver\1.8.4\aspectjweaver-1.8.4.jar;D:\app\repository\org\aspectj\aspectjrt\1.8.4\aspectjrt-1.8.4.jar;D:\app\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\app\repository\cglib\cglib\3.1\cglib-3.1.jar;D:\app\repository\org\ow2\asm\asm\4.2\asm-4.2.jar;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\lib\idea_rt.jar" com.raipeng.work.spring.test.MainTest Connected to the target VM, address: '127.0.0.1:60621', transport: 'socket' 十一月 21, 2015 3:02:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@648ee94b: startup date [Sat Nov 21 15:02:45 CST 2015]; root of context hierarchy 十一月 21, 2015 3:02:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] this is around Advice 2015-11-21 15:02:47.479 INFO [main][HelloWorldAspact.java:33] - the method HelloWorldServiceImp.hello() run this is after Advice this is afterReturning Advice Disconnected from the target VM, address: '127.0.0.1:60621', transport: 'socket' Process finished with exit code 0