试读部分:
3.4 Spring 2.0中的AOP
package spring.chapter3.aop.schema;
publicclass AdviceBeforeComponent ...{
publicvoid before()...{
System.out.println("用户验证");
}
}
<?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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:config>
<aop:pointcut id="beforePointcut"
expression="execution(* spring.chapter3.proxy.IComponent.*(..))" />
<aop:aspect id="before" ref="beforeAdvice">
<aop:before pointcut-ref="beforePointcut" method="before" />
</aop:aspect>
</aop:config>
<bean id="beforeAdvice"
class="spring.chapter3.aop.schema.AdviceBeforeComponent" />
<bean id="component" class="spring.chapter3.proxy.Component" />
</beans>
<aop:config>
<aop:pointcut id="somePointcut" expression=""/>
<aop:advisor id="someAdvisor" pointcut-ref="" advice-ref=""/>
<aop:aspect id="someAspect" ref="someBean">
<aop:adviceType id="someAdvice" .../>
</aop:aspect>
</aop:config> 
package spring.chapter3.aop.schema;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.chapter3.proxy.IComponent;
publicclass TestSchema ...{
publicstaticvoid main(String[] args) ...{
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring/chapter3/aop/schema/advice.xml");
IComponent component = (IComponent) context.getBean("component");
component.bussiness1();
component.bussiness2();
component.bussiness3();
}
}
package spring.chapter3.aop.schema;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AdviceBeforeComponent ...{
@Before("execution(* spring.chapter3.proxy.IComponent.*(..))")
public void before()...{
System.out.println("用户验证");
}
}
<?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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:aspectj-autoproxy />
<bean id="beforeAdvice"
class="spring.chapter3.aop.schema.AdviceBeforeComponent" />
<bean id="component" class="spring.chapter3.proxy.Component" />
</beans>发表于 @ 2008年01月17日 15:11:00 | 评论( loading... ) | 举报| 收藏