Spring Aop (Oriented Programming)

 

 

 

Related Terms:

 

 

 

 generate the proxy bottom spring has two modes: JDK dynamic proxy (objects must implement the interface) and generates a proxy CGLIB (object can be achieved independent interface)

 

 

 

 

 

 

 

 

 Introducing two dependent 1.maven:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version></1.0version>
    </dependency>

2. Configure applicationContext.xml Spring configuration file:

<?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 id="studentDao" class="com.imooc.aop.demo6.StudentDaoImpl"/>
    <bean id="customerDao" class="com.imooc.aop.demo6.CustomerDao " Configuration enhancements<-!/>

    -->
    <bean id="myBeforeAdvice" class="com.imooc.aop.demo6.MyBeforeAdvice"/>
    <bean id="myAroundAdvice" class="com.imooc.aop.demo6.MyAroundAdvice"/>

    <!--配置切面-->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="pattern" value="com\.imooc\.aop\.demo6\.CustomerDao\.save"/>   <!--配置匹配的对象中的方法,.Need to use escape character tag nameProperty<->
        = "the advice" REF = "myAroundAdvice" />       <-! configure both methods -> 
    </ the bean > 

    <-! let audiences created automatically when the corresponding entity of the proxy object and replace -> 
    < the bean class = "of class org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" > </ the bean > 
</ Beans >

3. Examples of use:

package com.imooc.aop.demo6;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext4.xml")
public class SpringDemo6 {
    @Resource(name="studentDao")
    private StudentDao studentDao;
    @Resource(name="customerDao")
    private CustomerDao customerDao;

    @Test
    public void demo1(){
        studentDao.find();
        studentDao.save();
        studentDao.update();
        studentDao.delete();

        customerDao.find();
        customerDao.save();
        customerDao.update();
        customerDao.delete();

    }
}

 

Guess you like

Origin www.cnblogs.com/shouyaya/p/12549649.html