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

相关文章
|
25天前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
118 26
|
1月前
|
XML Java 开发者
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
110 6
|
28天前
|
缓存 Java 数据库
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
183 89
|
2月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
184 73
|
15天前
|
监控 Java Spring
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
49 16
|
25天前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
93 25
|
25天前
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
78 24
|
2月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
64 21
|
1月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
91 8
|
2月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解