SpringMvc自动代理

自动配置的好处是不需要挨个 实现【org.springframework.aop.framework.ProxyFactoryBean】 ,只需要 advisor 配置和

<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />  代码即可

1.config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="logAfter" class="com.gc.action.LogAfter"></bean>
<bean id="timeBook" class="com.gc.action.TimeBook"></bean>
<bean id="logAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- advice 要加入的代码 -->
<property name="advice">
<ref bean="logAfter"></ref>
</property> <!--指定do开头的方法 -->
<property name="patterns">
<value>.*do.*</value>
</property>
</bean> <!-- 自动aopd的配置 -->
<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans>

2.使用logbefor 后 logafter  后 logaround的advice代码

3.ITimeBook

package com.gc.action;

public interface ITimeBook {

    public void doAudit(String name);

    public void doCheck(String name);
}

4.TimeBook

package com.gc.action;

public class TimeBook implements ITimeBook {

    @Override
public void doAudit(String name) {
// TODO Auto-generated method stub
System.out.println("this is doAudit"); int m=1/0;
} @Override
public void doCheck(String name) {
// TODO Auto-generated method stub
System.out.println("this is doCheck");
} }

5.测试

 ApplicationContext context=new FileSystemXmlApplicationContext("config-auto.xml");
ITimeBook tb=(ITimeBook)context.getBean("timeBook");
tb.doCheck("xiaox");
上一篇:C# 利用Aspose.Words .dll将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)


下一篇:为iPhone6设计自适应布局(一)