项目结构:
切面类:
package edu.nf.ch12.service.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * @author wangl * @date 2018/10/24 */ @Aspect //这个注解标识当前的类为一个切面 @Component //标识容器受管的Bean对象 public class UserServiceAspect { /** * 声明一个切入点,并编写切入点表达式 */ @Pointcut("execution(* edu.nf.ch12.service.*.*(..))") public void pointcut(){ } /** * 前置通知,指定切入点函数 * 也可在注解中自定义不同的切入点表达式 * @Before("execution(...)") * */ @Before("pointcut()") public void before(JoinPoint joinPoint){ System.out.println("前置通知..."+joinPoint.getArgs()[0]); } /** * 后置通知 */ @AfterReturning(value = "pointcut()", returning = "returnVal") public void afterReturn(String returnVal){ System.out.println("后置通知..." + returnVal); } /** * 环绕通知 */ @Around("pointcut()") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕通知前..."); Object returnVal = pjp.proceed(); System.out.println("环绕通知后..."); return returnVal; } /** * 异常通知 * 通过pointcut指定切入点 */ @AfterThrowing(pointcut = "pointcut()", throwing = "e") public void throwableAdvice(Throwable e){ System.out.println("异常通知..." + e.getMessage()); } /** * 最终通知 */ @After("pointcut()") public void after(){ System.out.println("最终通知..."); } }
配置类AppConfig:
package edu.nf.ch12.service.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author wangl * @date 2018/10/24 */ @ComponentScan("edu.nf.ch12") //启用包扫描 @EnableAspectJAutoProxy //启用AspectJ注解自动配置,proxyTargetClass用于指定是否强制使用cglib代理 public class AppConfig { }
接口类:
package edu.nf.ch12.service; /** * @author wangl * @date 2018/10/24 */ public interface UserService { /** * 查询用户 * @param uid * @return */ String getUserNameById(String uid); }
接口实现类:
package edu.nf.ch12.service.impl; import edu.nf.ch12.service.UserService; import org.springframework.stereotype.Service; /** * @author wangl * @date 2018/10/24 */ @Service("userService") public class UserServiceImpl implements UserService { @Override public String getUserNameById(String uid) { System.out.println("查询用户..."+uid); return "user1"; } }
程序测试类:
package edu.nf.ch12.test; import edu.nf.ch12.service.UserService; import edu.nf.ch12.service.config.AppConfig; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author wangl * @date 2018/10/24 */ public class UserServiceTest { @Test public void testGetUser(){ //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService service = context.getBean("userService", UserService.class); service.getUserNameById("1001"); } }
如果半注解半配置文件实现的话, new ClassPathXmlApplicationContext("applicationContext.xml");实例 然后再配置一个xml
applicationContext:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 启用包扫描 --> <context:component-scan base-package="edu.nf.ch12"/> <!-- 启用AspectJ注解自动配置,proxy-target-class是否强制使用cglib动态代理,true表示强制--> <aop:aspectj-autoproxy/> </beans>
运行结果: