第一步:定義業務層接口與實現java
1.定義接口spring
package group.esperanto.service; public interface IMessageService { public boolean doRemove(String mid); }
2.定義實現類express
package group.esperanto.service.impl; import org.apache.log4j.Logger; import group.esperanto.service.IMessageService; public class MessageServiceImpl implements IMessageService { @Override public boolean doRemove(String mid) { Logger.getLogger(IMessageService.class).info("[業務層]---執行刪除ID = "+mid); return false; } }
第二步:定義AOP程序處理類apache
package group.esperanto.proxy; import org.apache.log4j.Logger; public class ServiceProxy { public void BeforeInvoke(){ Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before]"); } public void AfterInvoke(){ Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before]"); } }
第三步:在applicationContext中進行配置spring-mvc
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd "> <bean id="serviceProxy" class="group.esperanto.proxy.ServiceProxy" /> <bean id="messageServiceImpl" class="group.esperanto.service.impl.MessageServiceImpl"/> <!-- 針對AOP的處理進行配置 --> <aop:config> <!-- 定義業務操做切入點 --> <aop:pointcut expression="execution(* group.esperanto.service..*.*(..))" id="messagePoint"/> <!-- 定義具體操做到切入點的方法上 --> <aop:aspect ref="serviceProxy"> <!-- 引用外部定義好的切入點 --> <aop:before method="beforeInvoke" pointcut-ref="messagePoint"/> <!-- 內部定義切入點 --> <aop:after method="afterInvoke" pointcut="execution(* group.esperanto.service..*.*(..))"/> </aop:aspect> </aop:config> </beans>
第四步:測試調用mvc
package group.esperanto.test; import org.apache.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import group.esperanto.service.IMessageService; public class Task { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); IMessageService messageService = ctx.getBean("messageServiceImpl",IMessageService.class); Logger.getLogger(Task.class).info(messageService.doRemove("110")); } }