spring学习笔记二 注解及AOP
注解:
使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值。 扫描某个包下的所有类中的注解.
复制代码
<? version="1.0" encoding="UTF-8"?>
ns:context="http://www.spring work.org/schema/context" ns:aop="http://www.spring work.org/schema/aop"
xsi:schemaLocation="http://www.spring work.org/schema/beans
http://www.spring work.org/schema/beans/spring-beans-4.2.xsd http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-4.2.xsd http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop-4.2.xsd "><bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean><aop:config> <!-- 配置切入点 public void cn.itcast.service.UserServiceImpl.save() 这样配置太啰嗦 void cn.itcast.service.UserServiceImpl.save() public可以省 * cn.itcast.service.UserServiceImpl.save() 对返回值不做任何要求 * cn.itcast.service.UserServiceImpl.*() 所有空参方法 * cn.itcast.service.*ServiceImpl.*(..) 找service包下的所有ServiceImpl结尾的方法 * cn.itcast.service..*ServiceImpl.*(..) 找Service包下(及其子孙包)的所有的ServiceImpl结尾的方法 --> <aop:pointcut ="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice" > <!-- 指定名为before方法作为前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 后置 --> <aop:after-returning method="afterReturning" pointcut-ref="pc" /> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 异常拦截通知 --> <aop:after-throwing method="afterException" pointcut-ref="pc"/> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect></aop:config>
复制代码
复制代码
5、测试代码
复制代码
复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext. ")
public class Demo {
@Autowiredprivate UserService u;@Testpublic void fun1() { u.save();}}
复制代码
复制代码
使用注解演示AOP
配置文件:
复制代码
复制代码
<? version="1.0" encoding="UTF-8"?>
http://www.spring work.org/schema/beans/spring-beans-4.2.xsd http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-4.2.xsd http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop-4.2.xsd ">
<bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
复制代码
复制代码
通知类
复制代码
复制代码
package cn.itcast.e_annotationaop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {
@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void pc(){}//抽取相同的execution,方便管理。//前置通知//指定该方法是前置通知,并制定切入点@Before("MyAdvice.pc()")public void before(){ System.out.println("这是前置通知!!");}//后置通知@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterReturning(){ System.out.println("这是后置通知(如果出现异常不会调用)!!");}//环绕通知@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")public around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("这是环绕通知之前的部分!!"); proceed = pjp.proceed();//调用目标方法 System.out.println("这是环绕通知之后的部分!!"); return proceed;}//异常通知@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterException(){ System.out.println("出事啦!出现异常了!!");}//后置通知@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void after(){ System.out.println("这是后置通知(出现异常也会调用)!!");} 继续阅读与本文标签相同的文章
Java开发手册 | 免费资料库
2017阿里技术年度精选| 免费资料库
-
《Java EE互联网轻量级框架整合开发》| 每日读本书
2026-05-24栏目: 教程
-
端点科技白冰:全渠道、大会员、新技术是企业变革的三大关键词 | 数字中国行·武汉城市峰会
2026-05-24栏目: 教程
-
重拾C++经典笔试30题(11-20)
2026-05-24栏目: 教程
-
阿里云成都服务器怎么样好不好?价格及如何选择?
2026-05-24栏目: 教程
-
重拾C++经典笔试30题(1-10)
2026-05-24栏目: 教程
