02_Spring_注解装配bean&AOP编程

Spring_Day02

一、IoC容器装配Bean_基于注解配置方式

1. 基于xml配置的方式装配Bean

1. 导包,4个核心包:cores,context,expression,beans. 2个日志包:loging,log4j
2. 编写service层与dao层
3. 配置applicationContext.xml,中service层与dao层的两个bean。
4. web层(这里junit模拟测试)调用业务方法

==========================================================================

CustomerServiceImpl:
    声明CustomerDAO对象用于接收属性注入的CustomerDAOImpl对象的引用。
    必须提供UserDAO的setter方法,用于Spring框架根据配置进行依赖注入。

public class CustomerServiceImpl implements CustomerService {

    // 声明CustomerDAO
    private CustomerDAO customerDAO;

    // 提供setter方法Spring框架属性注入
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    // 模拟用户注册
    public void save() {
        // 业务逻辑
        System.out.println("业务层......CustomerServiceImpl....save...");
        // 调用dao层
        customerDAO.save();
    }
}


---------------------------------------------------------------------------

CustomerDAOImpl:
    操作数据库的代码。模拟用户注册。
    public class CustomerDAOImpl implements CustomerDAO {
    
        @Override
        //模拟客户注册
        public void save() {
            //数据库操作
            System.out.println("dao层...CustomerDAOImpl....save");
        }
    
    }

---------------------------------------------------------------------------

配置核心文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注册bean对象UserDAOImpl -->
    <bean id="customerDAO" class="com.itdream.service.CustomerDAOImpl"/>
    <!-- 注册bean对象UserServiceImpl -->
    <bean id="customerService" class="com.itdream.service.CustomerServiceImpl">
        <property name="customerDAO" ref="customerDAO"/>
    </bean>
</beans>

---------------------------------------------------------------------------

web层:(Junit模拟调用业务层)

@Test
public void test() {
    //创建Spring的ApplicationContext工厂对象
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
    //获取CustomerServiceImpl的Bean对象
    CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
    //调用save方法
    customerService.save();
}

2. 基于Spring注解装配Bean

1. 导包,4个核心包:cores,context,expression,beans. 2个日志包:loging,log4j
    1. 额外添加一个aop的jar包
2. 编写service层与dao层,并完成注解注册
3. 核心配置文件applicationContext.xml中开启注解扫描 
4. web层(这里junit模拟测试)调用业务方法

============================================================================

2.1. 导jar包

注解装配Bean与XML方式装配的jar包相同,但还需要额外导入一个aop的jar包。

img12.png

2.2. 编写service层与dao层的注册

xml的做法: <bean id="" class="" ...></bean>

注解做法 : spring2.5引入 @Component 注解

CustomerServiceImpl:
    //注册bean,表明这是被Spring管理的bean.括号内相当于xml配置中的id/name
    //@Componet //如果不写value,默认类名第一个字母小写.习惯写接口
    @Component("customerService")
    //@Service("customerService")
    public class CustomerServiceImpl implements CustomerService {
    
        @AutoWired// 声明CustomerDAO,用于接收属性注入的CustomerDAOImpl对象的引用
        @Qualifier("customerDAO")
        private CustomerDAO customerDAO;
    
        @Override
        // 模拟用户注册
        public void save() {
            // 业务逻辑
            System.out.println("业务层......CustomerServiceImpl....save...");
            // 调用dao层
            customerDAO.save();
        }
    }

--------------------------------------------------------------------------

//注册bean,表明这是被Spring管理的bean,相当于xml配置中的<bean id="customerDAO" class="..."/>
//如果不写value,默认是第一个字母小写的类名.
//@Repository是dao层的bean类注册的注解,是Component的子注解
@Component("customerDAO")
//@Repository("customerDAO")
public class CustomerDAOImpl implements CustomerDAO {

    @Override
    //模拟客户注册
    public void save() {
        //数据库操作
        System.out.println("dao层...CustomerDAOImpl....save");
    }

}   

2.3. applicationContext.xml中开启注解扫描

1. 引入名称空间:
    找到xsd-configuration.html中contextschema的头约束引入。
    <?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" 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"> 
    </beans>
    配置本地提示:XML Catalog

注意:所有的名称空间都要基于Beans的名称空间。
img13.png
2. 编写applicationContext.xml开启注解

    <!-- 开启注解 -->
    <!-- <context:annotation-config/> -->
    <!-- 配置注解Bean组件扫描:将注解的bean扫描到容器
        作用:会自动扫描某包及其子包下所有含有@Component的类或子类,将其作为bean被Spring管理
    -->
    <context:component-scan base-package="com.itdream.service"/>


优化:

1. 注解扫描配置
    在配置组件扫描时,Spring会自动开启注解功能,所以开启注解功能可以不配置。
    即:<context:componet-scan> 具有 <context:annotation-config> 作用!

2. 衍生注解
    实际开发中,使用的是@Component三个衍生注解(“子注解”)
    子注解的作用:有分层的意义(分层注解)。功能与Component一致,以后会增强

    @Repository用于对DAO实现类进行标注
    @Service用于对Service实现类进行标注
    @Controller用于对Controller实现类进行标注
img14.png

3. Bean属性的依赖注入

3.1 简单数据类型依赖注入(了解)

Spring3.0后,提供 @Value注解,可以完成简单数据的注入

img15.png
3.3 复杂类型数据依赖注入【掌握】

注解实现属性依赖注入,将注解加在setXxx方法上 或者 属性定义上。

四种方式:

1. 使用@Value结合pElL表达式
    @Value("#{customerDAO}") // 找到设置id/name为customer的bean
    private CustomerDAO customerDAO;

2. 使用@Autowired 结合 @Qualifier
    // 声明CustomerDAO,用于接收属性注入的CustomerDAOImpl对象的引用
     @Autowired
     @Qualifier("customerDAO")
    private CustomerDAO customerDao;

    单独使用@Autowired 按照类型注入

3. JSR-250标准(jdk) 提供@Resource
    @Resource(name="customerDAO")
    private CustomerDAO customerDao;

4. SR-330标准(jdk) 提供 @Inject 
    需要先导入 javax.inject 的 jar
    @Inject
    @Named("customerDAO")
    private CustomerDAO customerDao;     

总结:

img16.png

4. Bean的初始化与销毁(注解)

img17.png
使用 @PostConstruct 注解, 标明初始化方法 ---相当于 init-method 指定初始化方法
使用 @PreDestroy 注解, 标明销毁方法  ----相当于 destroy-method 指定对象销毁方法 

==========================================================================

//不写value默认类名第一个字母小写作为该bean的id/name
@Component
public class InitAndDestroyDemo {

    //执行的方法
    public void save() {
        System.out.println("InitAndDestroyDemo.....save..........");
    }
    
    @PostConstruct
    public void init() {
        System.out.println("InitAndDestroyDemo.......初始化 .......");
    }
    
    //销毁前调用的方法
    @PreDestroy
    public void destroy() {
        System.out.println("InitAndDestroyDemo......销毁..........");
    }
}

测试:
    @Test
    public void test() {
        //加载配置文件获取Spring工厂对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取Bean对象
        InitAndDestroyDemo demo =(InitAndDestroyDemo) applicationContext.getBean("initAndDestroyDemo");
        //调用业务逻辑
        demo.save();
        //手动关闭Spring容器,使demo对象销毁
        ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext)applicationContext;
        context.close();
    }   

注意:
    要执行对象的销毁方法的条件:
    1. 必须是单例bean(Spring容器close时,单例bean执行销毁方法),多例bean不归Spring容器管理
    2. 必须调用Spring容器的close方法

结果:

img18.png

5. Bean的作用域

通过@Scope注解,指定Bean的作用域(默认是 singleton 单例)

@Component  //默认注册bean的id/name为第一个字母小写的类名
@Scope("prototype")//多例
public class ScopeDemo {
}

测试:

img19.png

另外,XML和注解也可以混合配置

二、Spring的junit测试集成

Spring提供 test.jar 可以整合junit。

好处:可以简化测试代码(不需要手动创建上下文)

1. 导入jar包,Spring的基本包.Junit包.spring-test.jar
2. 通过@RunWith注解,使用junit整合spring
    在测试类运行前的初始化的时候,会自动创建ApplicationContext对象
3. 通过@Autowired注解,注入需要测试的对象
4. 测试

=====================================================================================

//Junit和Spring整合,会自动开启注解功能,自动构建Spring容器,将当前类注册为bean
@RunWith(SpringJUnit4ClassRunner.class)
//为Spring容器指定配置文件
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SaveAndDestoryTest {
    
    //提供接收属性注入的需要测试的bean对象的引用
    @Autowired
    @Qualifier("initAndDestroyDemo")
    private InitAndDestroyDemo demo;

    @Test
    public void test() {
        demo.save();
    }
}   

Spring整合Junit测试后,无需手动销毁,最后会closeSpring容器。

三、 AOP面向切面编程的相关概念

3.1 什么是AOP?

AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想。

AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码的编写方式(性能监视、事务管理、安全检查、缓存,日志记录等)。


传统的纵向体系代码复用:

img20.png

横向抽取机制(AOP思想):

img21.png

开闭原则:

img22.png

AOP 思想: 基于代理思想,对原来目标对象,创建代理对象,在不修改原对象代码情况下,通过代理对象,调用增强功能的代码,从而对原有业务方法进行增强 !

切面:需要代理一些方法和增强代码 。

3.2 AOP的应用场景

场景一: 记录日志 
场景二: 监控方法运行时间 (监控性能)
场景三: 权限控制 
场景四: 缓存优化 (第一次调用查询数据库,将查询结果放入内存对象, 第二次调用, 直接从内存对象返回,不需要查询数据库 )
场景五: 事务管理 (调用方法前开启事务, 调用方法后提交关闭事务 )

3.3 Spring AOP编程两种方式

Spring内部支持两套AOP编程的方案:

  • Spring 1.2 开始支持AOP编程 (传统SpringAOP 编程),编程非常复杂
  • Spring 2.0 之后支持第三方 AOP框架(AspectJ ),实现另一种 AOP编程 -- 推荐

3.4 AOP编程相关术语

img23.png
JointPoint(连接点) : 在Spring中即指 拦截到的所有方法。
PointCut(切入点) : 指对哪些JointPoint进行拦截的定义。即对哪些方法进行增强。
Advice(通知/增强) : 就是对切入点(要增强的方法)做怎样的增强。
Target(目标对象) : 代理的目标对象
Weaving(织入) : 将增强应用到目标对象来创建代理对象的过程
Proxy(代理) : 一个类被AOP织入增强后,返回的就是一个代理对象
Aspect : 切入点和通知的结合,就是定义对哪个方法进行怎么样的增强
img24.png

4. AOP编程底层实现机制

AOP 就是要对目标进行代理对象的创建。
Spring AOP是基于动态代理的,基于两种动态代理机制: JDK动态代理和CGLIB动态代理

动态代理和静态代理区别? 
动态代理:在虚拟机内部,运行的时候,动态生成代理类(运行时生成,runtime生成) ,并不是真正存在的类, Proxy$$ (Proxy$$Customer)
静态代理:存在代理类 (例如:struts2 Action的代理类 ActionProxy)  

4.1. JDK动态代理

JDK动态代理,针对目标对象的接口进行代理 ,动态生成接口的实现类(必须有接口)

过程:

1. 必须对接口生成代理 
2. 用Proxy对象,通过newProxyInstance方法为目标创建代理对象.该方法接收三个参数 :
    1. 目标对象类加载器
    2. 目标对象实现的接口
    3. 代理后的处理程序InvocationHandler
3. 实现InvocationHandler接口中invoke方法,在目标对象每个方法调用时,都会执行invoke

例:

1. 编写UserService接口与UserServiceImpl实现类.(JDK动态代理必须有接口)
2. 编写JDK动态代理工厂生成动态代理,对目标对象进行增强
3. 测试


UserService:
public interface UserService {
    //模拟用户注册
    public void save();
}

UserServiceImpl:
public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        // 目标:使用jdk动态代理,增强该方法
        System.out.println("用户注册.........");
    }
}

--------------------------------------------------------------------------

JDK动态代理工厂类:
public class JdkProxyFactory implements InvocationHandler {
    
    //用于接收目标对象
    private Object taget;

    //构造方法传入目标对象
    public JdkProxyFactory(Object taget) {
        this.taget = taget;
    }

    // 获取目标的代理子对象
    public Object getJdkProxy() {

        // 参数1:目标对象的类加载器
        // 参数2:目标对象实现的所有接口(jdk通过目标接口生成代理子对象)
        // 参数3: 方法调用处理程序InvocationHandler,目标对象调用方法时会执行该处理器的invoke方法(可以写增强的代码)
        Object proxy = Proxy.newProxyInstance(taget.getClass().getClassLoader(), //
                taget.getClass().getInterfaces(), //
                this);
        return proxy;
    }

    @Override
    // 参数1:代理对象
    // 参数2: 目标对象调用的方法
    // 参数3:目标对象调用的方法的参数
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 增强的业务
        writeLog();
        // 执行目标对象原来的方法
        Object object = method.invoke(taget, args);
        // 还可以进行后置增强
        return object;// 返回目标对象
    }

    // 通知/增强的代码
    public void writeLog() {
        System.out.println("开始记录日志........Jdk动态代理.....");
    }
}

----------------------------------------------------------------------------

测试:
@Test
// 使用jdk动态代理增强目标对象
public void jdkProxyTest() {
    // 1.获取目标对象
    UserService userService = new UserServiceImpl();
    // 2. 通过代理工厂获取代理对象
    // 2.1 获取Jdk代理对象的工厂
    JdkProxyFactory jdkProxyFactory = new JdkProxyFactory(userService);
    // 将通知/增强应用到目标对象,生成代理对象的过程(织入)
    // 代理对象是目标对象的接口的子对象
    UserService jdkProxy = (UserService) jdkProxyFactory.getJdkProxy();
    // 3. 调用方法
    jdkProxy.save();
}   
img25.png

jdk动态代理的缺点: 只能面向接口代理,不能直接对目标类进行代理 (如果没有接口,则不能使用)。

4.2. cglib动态代理

Cglib的引入为了解决类的直接代理问题(生成代理子类),不需要接口也可以代理。

该代理方式需要相应的jar包,但不需要导入。因为Spring core包已经包含cglib ,而且同时包含了cglib 依赖的asm的包(动态字节码的操作类库)

示例:

1. 编写ProductService(没有接口)
2. 编写cglib代理工厂用于生成代理对象,并对目标对象进行增强
3. 测试

-----------------------------------------------------------------------------

ProductService:
public class ProductService {
    // 模拟用户登录
    public void login() {
        System.out.println("ProductService.....用户要登陆了............");
    }
}

-----------------------------------------------------------------------------

cglib代理工厂类:
public class CglibProxyFactory implements MethodInterceptor {

    // 用于接收存储目标对象
    private Object taget;

    // 构造方法传入目标对象
    public CglibProxyFactory(Object taget) {
        this.taget = taget;
    }

    // 获得cglib动态代理对象
    public Object getCglibProxy() {
        // 1. 获得代理对象增强器
        Enhancer enhancer = new Enhancer();
        // 2. 设置代理对象的父类为目标对象
        enhancer.setSuperclass(taget.getClass());
        // 3. 设置每次代理对象调用方法时,都会执行的回调函数
        enhancer.setCallback(this);

        // 返回代理对象
        return enhancer.create();
    }

    @Override
    // 代理对象执行目标对象方法时,调用该方法
    // 参数1:代理对象 , 参数2 : 调用的目标对象的方法, 参数3:调用的目标对象的方法的参数 , 参数4:代理对象的方法(已经被增强过的,目标对象的子类)
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        // 通知/增强,对目标对象进行增强
        writeLog();
        // 执行原来的方法,获得目标对象
        Object object = method.invoke(taget, args);
        // 返回目标对象
        return object;
    }

    // 通知/增强.的方法
    public void writeLog() {
        System.out.println("开始写日志了......cglib......");
    }
}

--------------------------------------------------------------------------

测试:
@Test
// 使用cglib动态代理增强目标对象(不需要有接口)
public void cglibProxyTest() {
    // 1. 获取目标对象
    ProductService productService = new ProductService();
    // 2. 通过cglib代理对象工厂获得代理对象
    CglibProxyFactory cglibProxyFactory = new CglibProxyFactory(productService);
    // 织入:将增强应用到目标对象,获得代理对象
    // cglib代理对象是目标对象的子类
    ProductService cglibProxy = (ProductService) cglibProxyFactory.getCglibProxy();
    //3. 调用方法
    cglibProxy.login();
}
img26.png

4.3.代理知识小结

区别 :

  • jdk动态代理:基于接口的代理,会生成目标对象的接口类型的子对象。
  • cglib动态代理:基于类的代理,会生成目标对象类型的子对象。
img27.png

1、Spring AOP 优先对接口进行代理 (使用Jdk动态代理)

2、如果目标对象没有实现任何接口,才会对类进行代理 (使用cglib动态代理)


5. 基于AspectJ的SpringAOP编程(xml配置)

Spring的aop有两套编程方案:传统的aop和aspectj的aop。

5.1 快速入门

使用AspectJ的方式进行Spring的AOP开发的三个步骤:

1. 确定目标对象(bean),注册bean
2. 编写通知/增强(advice),注册bean
3. 配置切入点(Pointcut)和切面(aspect),切面即为建立切入点与通知的关联
4. 测试

------------------------------------------------------------------------
在测试用例时,可以不用配置 开启注解,当然配置也可以。
<context:component-scan base-package="com.itdream.spring.aop"/>


1. 确定目标对象UserServiceImpl(有接口),即增强UserServiceImpl中的所有方法

    public class UserServiceImpl implements UserService {
    
        @Override
        // 使用Spring的AOP编程增强
        public void save() {
            System.out.println("用户注册.........");
        }
    
    }


目标对象ProductService(没有接口):

    public class ProductService {
        //模拟添加商品..对该方法进行增强
        public void save() {
            System.out.println("ProductService.......添加商品...........");
        }
    }


为目标对象注册bean:
    <!-- 有接口的目标对象Bean -->
    <bean id="userService" class="com.itdream.spring.aop.UserServiceImpl"/>
    <!-- 没有接口的目标对象Bean -->
    <bean id="productService" class="com.itdream.spring.aop.ProductService"/>


2. 编写通知/增强类,并注册bean
    
    //通知/增强类,对切入点进行增强
    public class MyAspect {
    
        public void writeLog() {
            System.out.println("开始写日志了......Spring..aop开发");
        }
    
    }

为通知注册bean:
    <!-- 注册通知/增强myAspect-->
    <bean id="myAspect" class="com.itdream.spring.aop.MyAspect"/>


3. 配置切面.建立切入点 与 通知的关联。确定哪些方法要怎样进行增强

    <!-- 注册切面Aspect,即确定哪些方法要进行怎样的增强 -->
    <aop:config>
        <!-- 注册切入点,即哪些对象的哪些方法要被增强
            id:切入点的名字,随便取
            expression:切入点表达式,bean(*Service):所有以Service结尾的bean的所有方法都是切入点
         -->
        <aop:pointcut expression="bean(*Service)" id="myPointcut"/>
        
        <!-- 切面,建立切入点与通知之间的联系 
            ref : 通知的id/name
            method : 通知/增强类中的方法
            pointcut-ref : 切入点的引用id/name
        -->
        <aop:aspect ref="myAspect">
            <aop:before method="writeLog" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>


4. 测试
    1.一定要导入Junit包!! 以及spring的test包
    2. 注意ContextConfiguration中的写法(locations="classpath:appliactionContext.xml")
    3. 测试必须写在一个test中,写两个test分开测试会报错
     
    @RunWith(SpringJUnit4ClassRunner.class)//Spring框架整合Junit
    @ContextConfiguration(locations="classpath:applicationContext.xml")//Spring容器加载配置文件
    public class SpringAopTest {
        
        //注入的测试bean
        @Autowired
        @Qualifier("userService")//指定id/名称的bean注入
        private UserService userService;
        
        @Autowired//以默认的类型ProductService注入
        private ProductService productService;
        
        @Test
        public void test() {
            //有接口
            userService.save();
            //没有接口
            productService.save();
        }
    }

结果:

img29.png

小结:

1. 有接口的使用的是jdk动态代理,没有接口的使用的是cglib动态代理
2. 这里使用的都是全局pointcut切入点,也可以将它们写在aop-aspect内部,那么这些pointcut就只针对这个aspect切面有效
3. 一个通知可以增强多个连接点,一个连接点可以被多次增强。

5.2 配置文件applicationConext.xml的细节

  • 因为用到了注解,因此在导入头约束时要有context的名称空间
  • 因为使用了aop开发,因此导入头约束时也要有aop的名称空间
  • 所有的名称空间都要基于beans

头约束从官方提供的docs/spring-framework-reference/html/xsd-configuration.html中查找

<?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="com.itdream.spring.aop"/>
    
    <!-- Spring-AOP开发 -->
    <!-- 注册目标对象(bean),即要增强的对象 -->
    <!-- 有接口的目标对象Bean -->
    <bean id="userService" class="com.itdream.spring.aop.UserServiceImpl"/>
    <!-- 没有接口的目标对象Bean -->
    <bean id="productService" class="com.itdream.spring.aop.ProductService"/>
    
    <!-- 注册通知/增强myAspect-->
    <bean id="myAspect" class="com.itdream.spring.aop.MyAspect"/>
    
    <!-- 配置切面Aspect,即确定什么对象要进行怎样的增强 -->
    <aop:config>
        <!-- 注册切入点,即哪些对象的哪些方法要被增强
            id:切入点的名字,随便取
            expression:切入点表达式,bean(*Service):所有以Service结尾的bean的所有方法都是切入点
         -->
        <aop:pointcut expression="bean(*Service)" id="myPointcut"/>
        
        <!-- 切面,建立切入点与通知之间的联系 
            ref : 通知的id/name
            method : 通知/增强类中的方法
            pointcut-ref : 切入点的引用id/name
        -->
        <aop:aspect ref="myAspect">
            <aop:before method="writeLog" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

5.3. 切入点表达式语法

切入点表达式:

  • bean(bean Id/bean name)

      例如:
      bean(customerService) 增强customerService的bean中所有方法,支持通配符
      bean(*Service)增强以Service结尾的bean中的所有方法 
    
  • execution(<访问修饰符>?<返回类型>空格<方法全名>(<参数>)<异常>?)

      例如:
      execution(* com.itdream.spring.a_jdkproxy.CustomerServiceImpl.*(..)) 增强该bean对象中的所有方法
    
      execution(* com.itdream.spring..*.*(..)) 增强spring包和子包所有bean所有方法
    
  • within(包.类)

      例如: within(cn.itcast.spring..*) 增强spring包和子包所有bean“所有方法 ”
    
  • this(完整类型)/target(完整类型)

      this对某一个类-(代理对象有效),target对代理对象无效(只对目标对象有效)
      this(cn.itcast.spring.a_jdkproxy.CustomerServiceImpl) 增强类型所有方法(对代理对象有效) 
    

【AspectJ类型匹配的通配符】:

*:匹配任何数量字符;
..:匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。
+:匹配指定类型的子类型;仅能作为后缀放在类型模式后边。

5.4 AspectJ的通知类型(5+种)

  • 前置通知(Before advice):在目标方法执行之前进行增强
  • 后置通知(After returning advice):在目标方法执行之后进行增强
  • 环绕通知(Around advice):在目标方法执行的前后进行增强
  • 抛出通知(After throwing advice):在目标方法执行时抛出异常的时候进行增强
  • 最终通知(After(finally) advice):在目标方法执行之后,不管是否有异常,都会进行增强
  • 引介通知(Decare Parents):在目标类中增加一些新的方法和属性

5.5 各种通知的应用场景

5.5.1. Before前置通知

应用: 权限控制 (权限不足,抛出异常)、 记录方法调用信息日志

模拟:权限控制

1. 确定目标对象bean(要增强的对象的某些方法)
2. 编写通知/增强方法
3. 配置切面

目标对象bean:
public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        System.out.println("用户注册.........");
    }
    
    public int findCount() {
        System.out.println("余额查询........");
        return 100;
    }
}   

注册bean:
<!-- 注册要增强的目标bean -->
<bean id="userService2" class="com.itdream.spring.advice.UserServiceImpl"/> 

-----------------------------------------------------------------------------

编写通知/增强类:
//通知/增强类
public class MyAspect {

    //前置增强
    //参数:连接点:就是可以拦截到的方法
    //需求:应用:权限控制(权限不足就抛出异常)、记录方法调用信息日志
    public void before(JoinPoint joinPoint) {
        System.out.println("前置增强..............");
        System.out.println("增强的对象是:"+joinPoint.getTarget().getClass().getName());
        
        //模拟权限控制
        //连接点的前面Signature就是方法
        if("findCount".equals(joinPoint.getSignature().getName())) {
            throw new RuntimeException("对不起,您没有这个权限!");
        }
    }
}

注册bean:
<!-- 注册通知,增强Advice -->
<bean id="myAspect2" class="com.itdream.spring.advice.MyAspect"/>

-----------------------------------------------------------------------------

配置切面:
<!-- 配置切面,即切入点与通知之间的关联 -->
<aop:config>
    <!-- 设置切入点 -->
    <aop:pointcut expression="bean(userService2)" id="myPointcut"/>
    
    <!-- 配置切面 -->
    <aop:aspect ref="myAspect2">
        <aop:before method="before" pointcut-ref="myPointcut"/>
    </aop:aspect>
</aop:config>

-----------------------------------------------------------------------------

测试:

方法运行到findCount时抛出异常。达到了权限控制的目的。
img30.png
5.5.2. AfterReturing 后置通知

特点:在目标方法运行后,返回值后执行增强代码逻辑。

应用场景:与业务相关的,如网上营业厅查询余额后,自动下发短信。

分析: 后置通知可以获取到目标方法返回值,如果想对返回值进行操作,使用后置通知(但不能修改目标方法返回 )

还是以上面的目标bean作为目标进行后置增强:

1. 目标对象bean
2. 通知/增强类(一个类中可以写多个通知方法,配置中method可以选择通知方法)

//后置增强
//参数1:连接点 , 参数2:返回值类型Object,参数名随意,但配置时,需要配置返回值的名称与其一致
public void afterReturnning(JoinPoint joinPoint,Object returnValue) {
    System.out.println("后置通知:系统日志:当前下发了短信的方法是:"+joinPoint.getSignature().getName());
    //获取到查询结果,向用户下发短信
    System.out.println("尊敬的用户,您的余额是:"+returnValue);
}

注册bean之前已经完成。
--------------------------------------------------------------------------------------

3. 配置切面 

这里的pointcut的定义使用了局部定义,只针对这个切面有效
<!-- 配置切面 -->
<aop:aspect ref="myAspect2">
    <aop:pointcut expression="execution(* com.itdream.spring.advice.UserServiceImpl.findCount())" id="myPointcut"/>
    <aop:after-returning method="afterReturnning" pointcut-ref="myPointcut" returning="returnValue"/>
</aop:aspect>   

测试:

img31.png
5.5.3. Around 环绕通知

特点:目标执行前后,都进行增强(控制目标方法执行)

应用场景:日志、缓存、权限、性能监控、事务管理

环绕通知的增强代码的方法要求:
    接受的参数:ProceedingJoinPoint(可执行的连接点)
    返回值:Object返回值
    抛出Throwable异常。
=======================================================================

1. 确定目标bean,注册目标bean
2. 编写通知
3. 配置切面(切入点与通知的关联),即目标对象哪些方法需要通知增强。


通知:
//环绕通知
//需求:日志,缓存,权限,性能监控,"事务管理"
//三个特点:
//参数:正在执行的连接点(方法)
//必须抛出异常Throwable
//必须返回目标对象
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    //在执行方法之前开启事务
    System.out.println("开启事务......");
    //执行目标方法
    Object object = joinPoint.proceed();
    //目标方法执行完毕后提交事务
    System.out.println("提交事务");
    //将目标对象返回
    return object;
}


配置切面:
<!-- 环绕通知 -->
<!-- 注册要增强的目标bean -->
<bean id="userService2" class="com.itdream.spring.advice.UserServiceImpl" />
<!-- 注册通知,增强Advice -->
<bean id="myAspect2" class="com.itdream.spring.advice.MyAspect" />
<!-- 配置切面,即切入点与通知之间的关联 -->
<aop:config>
    <!-- 定义切入点,即要增强目标对象的哪些方法 -->
    <aop:pointcut expression="bean(userService2)" id="myPoincut2" />
    
    <!-- 配置切面 -->
    <aop:aspect ref="myAspect2">
        <!-- 切入点与通知的关联:
            method:执行的通知中的方法,pointcut:切入点id,
         -->
        <aop:around method="around" pointcut-ref="myPoincut2"/>
    </aop:aspect>
</aop:config>

测试:

img32.png
5.5.4. AfterThrowing 抛出通知

作用:目标代码出现异常,通知执行。记录异常日志、通知管理员(短信、邮件)

应用场景:处理异常(一般不可预知),记录日志

例子:

1. 确定目标对象bean
2. 编写通知/增强方法,类

//抛出通知:在目标对象的方法发生异常情况下,拦截增强
//需求:处理异常(一般不可预知),记录日志,通知管理员(短信,邮件)
//参数1:连接点信息,参数2:异常,Throwable类型,参数名称随意,配置这个名字
public void afterThrowing(JoinPoint joinPoint,Throwable ex) {
    //发生异常时,通知管理员
    System.out.println("尊敬的管理员,发生异常了,发生异常的类是:"+joinPoint.getTarget().getClass().getName()
            +",发生异常的方法是:"+joinPoint.getSignature().getName()
            +",发生异常的信息是:"+ex.getMessage());
}   

---------------------------------------------------------------------------------------------------

3. 配置切面(切入点与增强的关联)

<!-- 抛出通知 -->
<!-- 注册要增强的目标bean -->
<bean id="userService2" class="com.itdream.spring.advice.UserServiceImpl" />
<!-- 注册通知,增强Advice -->
<bean id="myAspect2" class="com.itdream.spring.advice.MyAspect" />
<!-- 配置切面,即切入点与通知之间的关联 -->
<aop:config>
    <!-- 定义切入点,即要增强目标对象的哪些方法 -->
    <aop:pointcut expression="bean(userService2)" id="myPoincut2" />
    
    <!-- 配置切面 -->
    <aop:aspect ref="myAspect2">
        <!-- 切入点与通知的关联:
            method:执行的通知中的方法,pointcut:切入点id,throwing:抛出通知中参数2异常的名称
         -->
         <aop:after-throwing method="afterThrowing" pointcut-ref="myPoincut2" throwing="ex"/>
    </aop:aspect>
</aop:config>

测试:

创造异常:
public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        System.out.println("用户注册.........");
    }
    
    public int findCount() {
        int i = 1/0;//创造异常
        System.out.println("余额查询........");
        return 100;
    }
}

测试结果:

img33.png
5.5.5. After 最终通知

作用:不管目标方法是否发生异常,最终通知都会执行(类似于finally代码功能)

应用场景:释放资源 (关闭文件、 关闭数据库连接、 网络连接、 释放内存对象 )

1. 确定目标对象bean
2. 编写通知/增强类/方法
//最终通知:不管方法是否成功执行,都会拦截增强
//需求:释放资源 (关闭文件、 关闭数据库连接、 网络连接、 释放内存对象 )
public void after(JoinPoint joinPoint) {
    //释放资源
    System.out.println("开始释放资源了,该连接点的信息是:"+joinPoint.toLongString());
}   

3. 配置切面(切入点与通知之间的关联) 

<!-- 最终通知 -->
<!-- 注册要增强的目标bean -->
<bean id="userService2" class="com.itdream.spring.advice.UserServiceImpl" />
<!-- 注册通知,增强Advice -->
<bean id="myAspect2" class="com.itdream.spring.advice.MyAspect" />
<!-- 配置切面,即切入点与通知之间的关联 -->
<aop:config>
    <!-- 定义切入点,即要增强目标对象的哪些方法 -->
    <aop:pointcut expression="bean(userService2)" id="myPoincut2" />
    
    <!-- 配置切面 -->
    <aop:aspect ref="myAspect2">
        <!-- 切入点与通知的关联:
            method:执行的通知中的方法,pointcut:切入点id,throwing:抛出通知中参数2异常的名称
         -->
        <aop:after method="after" pointcut-ref="myPoincut2"/>
    </aop:aspect>
</aop:config>

测试:不管是否有异常都会执行最终通知,进行资源的释放)

img34.png

五种通知小结:

只要掌握Around(环绕通知)通知类型,就可实现其他四种通知效果。

可以在环绕通知的方法中编写如下代码:
    try {
        //前置通知
        Object result = proceedingJoinPoint.proceed();
        //后置通知
}catch(Exception){
    //抛出通知
}finally{
    //最终通知
}

5.5.6. 各种Advice方法可接收的参数和返回值小结(参考)

方法格式:
public returnType method (param)
public 返回值类型 方法名 (参数类型 参数名)
其中参数类型为JoinPoint接口类型,返回值类型为void或Object
注意:JoinPoint是org.aspectj.lang.JoinPoint包下的
img35.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,363评论 6 343
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,058评论 0 9
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,360评论 1 133
  • 去年有一部日剧《校阅女孩河野悦子》很是火爆,连我这个不关注日剧的人都知道了!看了一个简介,就被吸引了,因为故事真的...
    小筱茶籽阅读 444评论 5 9