package com.ssm.chapter11.aop.service; import com.ssm.chapter11.game.pojo.Role; public interface RoleService { // public void printRole(Role role); public void printRole(Role role, int sort); }
代碼清單:RoleService實現類
package com.ssm.chapter11.aop.service.impl; import org.springframework.stereotype.Component; import com.ssm.chapter11.aop.service.RoleService; import com.ssm.chapter11.game.pojo.Role; @Component public class RoleServiceImpl implements RoleService { // @Override // public void printRole(Role role) { // System.out.println("{id: " + role.getId() + ", " + "role_name : " + role.getRoleName() + ", " + "note : " + role.getNote() + "}"); // } public void printRole(Role role, int sort) { System.out.println("{id: " + role.getId() + ", " + "role_name : " + role.getRoleName() + ", " + "note : " + role.getNote() + "}"); System.out.println(sort); } }
這個類沒什麼特別的,只是這個時候若是把printRole做爲AOP的切點,那麼用動態代理的語言就是要爲類RoleServi-ceImpl生成代理對象,而後攔截printRole方法,因而能夠產生各類AOP通知方法。
package com.ssm.chapter11.aop.aspect; import com.ssm.chapter11.aop.verifier.RoleVerifier; import com.ssm.chapter11.aop.verifier.impl.RoleVerifierImpl; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; @Aspect public class RoleAspect { @DeclareParents(value = "com.ssm.chapter11.aop.service.impl.RoleServiceImpl+", defaultImpl = RoleVerifierImpl.class) public RoleVerifier roleVerifier; @Pointcut("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..))") public void print() { } // @Before("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..))") // @Before("execution(* com.ssm.chapter11.*.*.*.*.printRole(..)) && within(com.ssm.chapter11.aop.service.impl.*)") @Before("print()") // @Before("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..)) && args(role, sort)") public void before() { System.out.println("before ...."); } @After("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..))") public void after() { System.out.println("after ...."); } @AfterReturning("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..))") public void afterReturning() { System.out.println("afterReturning ...."); } @AfterThrowing("execution(* com.ssm.chapter11.aop.service.impl.RoleServiceImpl.printRole(..))") public void afterThrowing() { System.out.println("afterThrowing ...."); } @Around("print()") public void around(ProceedingJoinPoint jp) { System.out.println("around before ...."); try { jp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("around after ...."); } }
package com.ssm.chapter11.aop.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.ssm.chapter11.aop.aspect.RoleAspect; @Configuration @EnableAspectJAutoProxy @ComponentScan("com.ssm.chapter11.aop") public class AopConfig { @Bean public RoleAspect getRoleAspect() { return new RoleAspect(); } }
Spring還提供了XML的方式,這裏就須要使用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: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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <aop:aspectj-autoproxy/> <bean id="roleAspect" class="com.ssm.chapter11.aop.aspect.RoleAspect"/> <bean id="roleService" class="com.ssm.chapter11.aop.service.impl.RoleServiceImpl"/> </beans>
不管用XML仍是用Java的配置,都能使Spring產生動態代理對象,從而組織切面,把各種通知織入到流程當中
package com.ssm.chapter11.aop.main; import com.ssm.chapter11.aop.verifier.RoleVerifier; import com.ssm.chapter11.aop.verifier.impl.RoleVerifierImpl; import org.aspectj.lang.annotation.DeclareParents; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.ssm.chapter11.aop.config.AopConfig; import com.ssm.chapter11.aop.service.RoleService; import com.ssm.chapter11.game.pojo.Role; public class Main { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AopConfig.class); // 使用XML使用ClassPathXmlApplicationContext做爲IoC容器 // ApplicationContext ctx = new ClassPathXmlApplicationContext("ssm/chapter11/spring-cfg3.xml"); RoleService roleService = ctx.getBean(RoleService.class); Role role = new Role(); role.setId(1L); role.setRoleName("role_name_1"); role.setNote("note_1"); RoleVerifier roleVerifier = (RoleVerifier) roleService; if (roleVerifier.verify(role)) { roleService.printRole(role, 1); } System.out.println("####################"); //測試異常通知 // role = null; // roleService.printRole(role); } }