Development of Spring's AOP:


Enhancements for all methods : ( tangents without tangents )
Step 1: Import the corresponding jar package.
* spring-aop-3.2.0.RELEASE.jar
* com.springsource.org.aopalliance-1.0.0.jar
 
Step 2: Write the proxy object:
* CustomerDao interface
* CustoemrDaoImpl implementation class
 
Step 3: Write the Enhanced Code:
public class MyBeforeAdvice implements MethodBeforeAdvice{
 
/**
 * method: the method to execute
 * args: parameters
 * target: target object
 */
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Pre-enhancement...");
}
}
 
Step 4: Generate Proxy: (Configure Generate Proxy:)
* Generate proxy Spring based on ProxyFactoryBean class. The bottom layer automatically chooses to use JDK's dynamic proxy or CGLIB's proxy.
* Attributes:
target : the target object of the proxy
proxyInterfaces : the interfaces to be implemented by the proxy
If multiple interfaces can be assigned using the following format
<list>
    <value></value>
    ....
</list>
proxyTargetClass : Whether to proxy for class instead of interface, when set to true, use CGLib proxy
interceptorNames : Advice that needs to be woven into the target
singleton : returns whether the proxy is a single instance, the default is singleton
optimize : when set to true, force the use of CGLib
 
<!-- Define the target object -->
<bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl"></bean>
<!-- 定义增强 -->
<bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice"></bean>
 
<!-- Spring支持配置生成代理: -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置目标对象 -->
<property name="target" ref="customerDao"/>
<!-- 设置实现的接口 ,value中写接口的全路径 -->
<property name= "proxyInterfaces " value ="cn.itcast.spring3.demo3.CustomerDao"/>
<!-- 需要使用value:要的名称 -->
<property name="interceptorNames" value= "beforeAdvice"/>
</bean>
 
***** 注入的时候要注入代理对象:
@Autowired
// @Qualifier("customerDao")// 注入是真实的对象,必须注入代理对象.
@Qualifier(" customerDaoProxy ")
private CustomerDao customerDao;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341144&siteId=291194637