Spring AOP
AOP实现方式:1.annotation方式,2.xml方式
方式一:annotation
1.配置xml文件(spring-annotation.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.fq.*"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="test" class="com.fq.server.serverImpl.TestServerImpl"></bean> </beans>
2.设置TestServerImpl.java和TestServer.java
package com.fq.server; public interface TestServer { void sayHello(); }
package com.fq.server.serverImpl; import com.fq.server.TestServer; public class TestServerImpl implements TestServer { @Override public void sayHello() { // TODO Auto-generated method stub System.out.println("hello!"); } }
3. AOP实例(AOPTest.java)
package com.fq.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class AopTest { //commonPointCut()目的只是代表execution表达式,方便使用。 @Pointcut("execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())") public void commonPointCut(){} //写法一 @Before(value = "execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())") public void doBefore(){ System.out.println("method before "); } //写法二 @AfterReturning(value="commonPointCut()") public void doAfter(){ System.out.println("method after"); } @Around(value="commonPointCut()") public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("menthod before !"); //必须有 Object relval = joinPoint.proceed(); System.out.println("method after !"); } }
4.编写测试代码(Test.java)
package com.fq.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fq.server.TestServer; public class Test { public static void main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("spring/spring-annotation.xml"); TestServer test =(TestServer) context.getBean("test"); test.sayHello(); } }
5.运行Test查看结果
方式二:xml配置
使用annotation方式中的TestServer.java , TestServerImpl.java
1.xml配置文件(spring-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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 定义的切面 --> <bean id="aopTestXml" class="com.fq.aop.AopTestXml"></bean> <aop:config> <!--此处定义的pointcut具有全局作用 --> <aop:pointcut expression="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())" id="commonPointCut" /> <aop:aspect id="aopName" ref="aopTestXml"> <!-- 此处pointcut具有局部作用,只在切名叫aopName下有效 --> <aop:pointcut expression="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())" id="myPointCut" /> <aop:before method="doBefore" pointcut-ref="myPointCut" /> <aop:after-returning method="doAfter" pointcut-ref="commonPointCut" /> <aop:around method="doAround" pointcut="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())" /> </aop:aspect> </aop:config> <!-- 对象bean --> <bean id="testXml" class="com.fq.server.serverImpl.TestServerImpl"></bean> </beans>
2.xml文件中定义的AopTestXml.java
package com.fq.aop; import org.aspectj.lang.ProceedingJoinPoint; public class AopTestXml { public void doBefore(){ System.out.println("method before "); } public void doAfter(){ System.out.println("method after"); } public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("menthod before !"); //必须有 Object relval = joinPoint.proceed(); System.out.println("method after !"); } }
3.测试类TestXml.java
package com.fq.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fq.server.TestServer; public class TestXml { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-xml.xml"); TestServer testXml = (TestServer) context.getBean("testXml"); testXml.sayHello(); } }
参考来源: