spring_day03 (proxy mode, AOP principle, xml-based AOP implementation, to achieve AOP-based annotation)

A dynamic proxy

1. Dynamic Agent features  

* Bytecode used with the creation, as used with the load. 

* It differs from the static agent is also here. Because static agent is a bytecode up has been created, and finished loading. 

* Decorator pattern is a reflection of the static agent.

2. Two Dynamic Acting:

* Based on a dynamic proxy interface:

  Proxy class by jdk

  Requires at least one interface is implemented proxy class

* Dynamic proxy subclasses:

  Provided by a third party, CGLib, if reported asmxxxx abnormal, you need to import asm.jar.

  Acting classes are not required to be final

3. Why use dynamic proxies:

* Without changing the source code on the enhancement of the proxy object.

Two .AOP

1. Role

Aspect Oriented Programming, spring ape instead of a program, do not have to write proxy mode, but managed to spring a dynamic agent, to decouple dependencies between methods.

Developers only need to focus on enhancing part of the method.

2. The term

* Joinpoint (connection points): There may need to be enhanced method

* Pointcut (entry point): all are likely to be enhanced in the process, it does require an enhanced method

* Advice (notification / Enhanced): Enhanced method implementation

         Type of notification: notification front, rear notification, abnormality notification, the final notification, around advice.

* Aspect (section): integration and entry point for notifications

* Weaving (weaving): it refers to the enhancement applied to the target object to the process of creating a new proxy object. Agent dynamic spring weaving, while the weaving AspectJ using compile and class loading of weaving

3. Step

* Prepared a core business code

* Extraction repeated code notification form

* Description xml configuration file woven into the starting point and notification,

* In the operational phase, managed to spring, the starting point will inform woven into them to generate dynamic proxy objects.

4. Note

spring whether the entry point class is judged according to the interface, which use proxy way.

III. Based on AOP xml implementation

1.jar 包

Aop constraints introduced 2.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"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

 

3. Configure aop

 <!-- 配置 service --> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>

        <!-- 配置 dao --> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> 
property name="dbAssit" ref="dbAssit"></property>
        </ Configuration database operation target<-!>The bean
        --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
<property name="dataSource" ref="dataSource"></property>
<!-- 指定 connection 和线程绑定 -->
<property name="useCurrentConnection" value="true"></property>
</bean>
        <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>

 

4. The method of extraction of public notification class

public class TransactionManager { 
};

 

5. The notification class labels arranged together with bean

 <!-- 配置通知 -->
<bean id="txManager" class="com.itheima.utils.TransactionManager">
<property name="dbAssit" ref="dbAssit"></property>
</bean> 

 

6. Use aop: config statement aop configuration

aop: aspect:
 Role: configuration section. Attribute: id: section to provide a unique identification. ref: reference id configured notification class bean.

< AOP: Aspect ID = "txAdvice" REF = "txManager" >  
  <-! Type Configuration write notification here ->  
</ AOP: Aspect > 

 

7 .: Use aop: pointcut pointcut expression Configuration

aop: pointcut: Role: expression used to configure the entry point. Which method is to specify which enhanced the class right. Properties: expression: expression used to define the starting point. id: entry point for expression to provide a unique identification

<aop:pointcut
        expression="execution(  public void com.itheima.service.impl.AccountServiceImpl.transfer(    java.lang.String, java.lang.String, java.lang.Float) )"
        id="pt1"/> 

 8. Use aop: xxx configuration corresponding to the type of notification

aop: before
 action: a pre-configured notification. Enhanced properties specified method performed prior to entry point methods: method: name specifies the notification method for enhancing class ponitcut-ref: reference to a specific starting point for the expression of poinitcut: for expression pointcuts

<aop:before method="beginTransaction" pointcut-ref="pt1"/> 

 

 *aop:before

*aop:after-returning

*aop:after-throwing

*aop:after

* AOP: around Surround notifications: Customize for implementing the notification sequence

9.

<aop:config>
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
    <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置环绕通知 -->
        <aop:around method="transactionAround" pointcut-ref="pt1"/>
    </aop:aspect>
</aop:config> 

 

9. Under normal circumstances, a method we are enhanced layer service, the entry point so that expressions are cut to the business layer implementation class. execution (* com.itheima.service.impl. *. * (..))

 

 IV. Based on annotation of AOP

 1.jar 包

2. Import context namespace in the 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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
</beans>
 

 

 3 .: The resource configuration using annotations

4. Specify the spring package to be scanned in the configuration file

<! - inform the spring, when creating the container package to be scanned -> 
< context: Scan-Component Base-Package = "com.itheima" > </ context: Component-Scan > 

 

 The class also uses the annotation configuration notification

6. Use @Aspect annotation declared on the notification section class

@Component ( "txManager" ) 
@Aspect 
// that the current class is a class section 
public  class the TransactionManager { 
    defines a DBAssit 
    @Autowired 
    Private DBAssit dbAssit; 
}

 

 7. Use annotations arranged on an enhanced method of notification

@Before
 role: the current method as a pre-notification. Property: value: expression used to specify the starting point, you can also specify a reference pointcut expression.

@Before("execution(* com.itheima.service.impl.*.*(..))")
public void beginTransaction(){
        try{dbAssit.getCurrentConnection().setAutoCommit(false);
        }catch(SQLException e)
        {e.printStackTrace();}
        } 
 
 

 

 *@Before

* @ After Returning

* @AfterThrowing

*@After

*@Around

8. Turn on the spring support for AOP annotations in the spring configuration file

<! - open spring support for annotations for AOP ->  
< AOP: AspectJ-autoproxy />

 

Note: Do not use the xml way

@Configuration
@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy public class SpringConfiguration { }

 

9.@Pointcut和@Around:

* @ Around the value: expression used to specify the starting point, you can also specify the entry point for the expression of reference.

@Pointcut("execution(* com.itheima.service.impl.*.*(..))") private void pt1() {};

@Around ( "pt1 ()") // Note: Do not forget to write brackets

 

Guess you like

Origin www.cnblogs.com/counter-biao/p/11614020.html