aop实现讲解
1 先定义一个接口 (必须定义)
public interface Performer {
void perform();
}
2 定义一个实现类(类名字不要求)
public class PerformerImp implements Performer {
private int beanBags = 3;
public void perform() {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}
3 定义切面类
@Aspect
public class Stage {
//@Pointcut("execution(* *insertPoint())")
@Pointcut("execution(* *perform())")
public void insertPoint()//pointcut
{
System.out.println("in insert point");
}
@Before("insertPoint()")
public void before1()
{
System.out.println("in insert before1");
}
@After("insertPoint()")
public void after1()
{
System.out.println("in insert after1");
}
}
4 在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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="stage" class="Stage"/>
<bean id="performer" class="PerformerImp"/>
</beans>
5 下面就可以在测试
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-idol.xml");
Performer s = (Performer) context.getBean("performer");
s.perform();
}
-----------------------------------------------------------------------------------
execution(* com.xyz.service.AccountService.*(..))
execution(* com.xyz.service.AccountService.*(..)and within(com.xyz.service.service2.*) and bean(id))
<bean id="audience" class="com.springinaction..."/>
<aop:config>
<aop:aspect ref="audience"> <!-- 切面-->
<aop:before pointcut="execution(* com.xyz.service.AccountService.*(..))" method="takeSeak" />
</aop:aspect>
</aop:config>
<!-- 环绕式切面 方法定义-->
public void method(ProceedingJoinPoint jointPoint)
{
try{
......
jointPoint.proceed();
........
}cache(Throwable e)
{ }
}
<aop:config>
<aop:aspect ref="audience"> <!-- 切面-->
<aop:pointcut id="id1" expression="execution(* com.xyz.service.AccountService.*())" />
<aop:around pointcut-ref="id1" method="m1()"/>
<aop:pointcut id="id2" expression="execution(* com.xyz.service.AccountService.*(String)) and args(s1)" />
<aop:before pointcut-ref="id2" method="m1" arg-names="s1"/>
</aop:aspect>
</aop:config>
-----------------引入新功能----------------------------------
1 定义新的接口
public interface Poem {
void recite();
}
2 定义新的接口实现
public class Sonnet29 implements Poem {
private String [] LINES={
"HELLO1",
"HELLO2"
};
public Sonnet29(){}
public void recite(){
for(int i=0;i<LINES.length;i++){
System.out.println(LINES[i]);
}
}
}
3 xml注册
<aop:aspect>
<aop:decare-parents
types-matching="...baseclass"
implement-interface="...interface"
default-impl="...impl" />
</aop:aspect>
4 测试
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-idol.xml");
Poem s = (Poem) context.getBean("performer");
s.recite();
}
<aop:aspect>
<aop:decare-parents
types-matching="...baseclass"
implement-interface="...interface"
delegate-ref="id1" />
</aop:aspect>
-------------注解------------------
@Aspect
public class Audience {
@Pointcut("execution(* com.xyz.service.AccountService.perform())")
public void insertPoint()//pointcut
{
}
@Before("insertPoint()")
public void before1()
{
...
}
@Around("insertPoint()")
public void before1(ProceedingJoinPoint point)
{
...
point.proceed();
...
}
}
<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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
</beans>
aop代码大全
最新推荐文章于 2024-01-02 23:41:48 发布