Spring Aop Demo

eclipse 如何自动新建bean.xml
new–>others–>spring Bean Configuration File–>命名

一、aop

1、 aop面向切面有两种方式:1 接口代理 2 cglib代理
2、由于在编程过程中会涉及到代码重用,或者是在获得第三方代码后,只能去添加功能,而不能去修改源码,可以通过继承、代理来实现,继承的话使得代码耦合度增加,aop核心就是通过代理来实现
3、aop使用的场景:安全、日志、监控等
一下的demo是通过两种配置来实例化bean,xml 和Java Config

二、xml方式

1、定义接口类

public interface Performance {
    //表示演员表演
	public void actor() ;

}

2、具体实现类

public class PerImp implements Performance{
	@Override
	public void actor() {
		System.out.println("开始表演");
	}
	
}

3、切入类

@org.aspectj.lang.annotation.Aspect
public class Aspect {
//表明在演员表演之前分别需要关闭手机,表演之后需要欢呼,在表演出演问题的时候去退票,表演结束之后离场
	@Pointcut("execution(* book4.Performance.actor(..))")
	public void pointcut() {} 
	@Before("pointcut()")
	public void before() {
		System.out.println("关闭手机");
	}
	@After("pointcut()")
	public void after() {
		System.out.println("呼叫欢呼");
	}
	@Around("pointcut()")
	public void arround(ProceedingJoinPoint jp)  {
		System.out.println("round关闭手机");
		System.out.println("round请坐");
		try {
			jp.proceed();
		} catch (Throwable e) {
			System.out.println("round退票");
		}
		
	}
	@AfterReturning("pointcut()")
	public void afterReturn() {
		System.out.println("离场");
	}
	@AfterThrowing("pointcut()")
	public void Throwing() {
		System.out.println("退票");
	}

}

4、xml配置方式

	<!-- 开启自动化代理 -->
   <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<bean id="perimp" class="book4.PerImp"></bean>
	<bean id="aspect" class="book4.Aspect"></bean>

5、test

        ApplicationContext context = new ClassPathXmlApplicationContext("book4/bean.xml");
		Performance performance = (Performance) context.getBean("perimp");
		performance.actor();

二、Config配置方式

1、配置类

@Configuration
//配置方式
@ComponentScan
//组件扫描
@EnableAspectJAutoProxy
//配置自动代理
public class Config { 
	@Bean(name = "asp")
	public Aspect getAsp() {
		return new Aspect();
	}
	@Bean(name = "per")
	public Performance getPer() {
		return new PerImp();
	}
}

2、test

        ApplicationContext context2 = new AnnotationConfigApplicationContext(Config.class);
		Performance performance2 = (Performance) context2.getBean("per");
		performance2.actor();

三、通过注释引入增加新功能

1、接口类

public interface ExtendPerformance {
	public void perCore();
}

2、实现类

public class DefaultExtendsPerformanceImp implements ExtendPerformance {

	@Override
	public void perCore() {
		System.out.println("增加功能:来个大跳");
	}
}

3、切入类

value表示当前引入接口,+表示实现当前接口的子类
defaultImpl ,表示增加功能的具体实现类

@Aspect
public class AspectCore {
	@DeclareParents(value = "book4.Performance+" 
			,defaultImpl = DefaultExtendsPerformanceImp.class)
	public static ExtendPerformance extendCor;
	
}

4、xml配置

<!-- aop扩展 -->
<bean class="book4.AspectCore"></bean>

5、test

实例化bean后,需要将其转化为增加功能的接口

ExtendPerformance performance3 =(ExtendPerformance)context.getBean("perimp");
performance3.perCore();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值