spring学习笔记二 注解及AOP

简介: spring学习笔记二 注解及AOP注解:使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值。 扫描某个包下的所有类中的注解.复制代码<?xml version="1.0" encoding="UTF-8"?>xmlns:context="http://www.

spring学习笔记二 注解及AOP
注解:

使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值。 扫描某个包下的所有类中的注解.

复制代码
<?xml version="1.0" encoding="UTF-8"?>
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.2.xsd http://www.springframework.org/schema/context 
                                       http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop 
                                       http://www.springframework.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 expression="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.xml")
public class Demo {

@Autowired
private UserService u;

@Test
public void fun1() {
  u.save();
}

}
复制代码
复制代码
使用注解演示AOP
配置文件:

复制代码
复制代码
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.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 Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("这是环绕通知之前的部分!!");
    Object 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("这是后置通知(出现异常也会调用)!!");
}

}
复制代码
原文地址link

相关文章
|
2天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
23 6
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
28 1
|
26天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
31 1
|
10天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
25 2
|
10天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
28 2
从入门到精通:Spring基础注解的全面解析
|
13天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
14天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
13 0
|
19天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
25天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
Spring4 学习教程
http://jinnianshilongnian.iteye.com/category/301336 Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
669 0