依赖注入就不不多讲直接见程序体现,主要说说面向切面的编程(AOP)
首当其冲的自然是创建一个典型的spring切面
- 创建通知,通常Spring有5种形式的通知:Before,After-returning ,After-throwing,Around,Introduction
- 定义切点和通知者:正则表达式切点and联合切点与通知者
其次就是创建代理了,通常采用ProxyFactoryBean,当然还有自动代理
ApplicationContext.xml(spring的关键配置文件)如下:
<?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="music" class="entertainment.PopMusic"></bean>
<bean id="singerTarget" class="entertainment.PopSinger">
<property name="name" value="Michael Jackson"></property>
<property name="song" ref="music"></property>
</bean>
<bean id="listener"
class="entertainment.Listener"/>
<bean id="listenerAdvice"
class="entertainment.ListenerAdvice">
<property name="listener" ref="listener"/>
</bean>
<!--定义切点和通知者-->
<bean id="listenAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="listenerAdvice"/>
<property name="pattern" value=".*sing"/>
</bean>
<!--设置代理-->
<bean id="audienceProxyBase"
class="org.springframework.aop.framework.ProxyFactoryBean"
abstract="true">
<property name="proxyInterfaces" value="entertainment.Singer"/>
<property name="interceptorNames" value="listenAdvisor"/>
</bean>
<bean id="singer" parent="audienceProxyBase">
<property name="target" ref="singerTarget"/>
</bean>
</beans>
来看看这几个关键的bean:
- singerTarget,对应了PopSinger类(实现了Singer接口),property(name)调用setName()方法,property(song)调用setSong()方法,其中song指定到music Bean
- listenAdvisor Bean定义了一个具有正则表达式切点的通知者,RegexpMethodPointcutAdvisor是个特殊的通知者类,可以在一个Bean里定义切点和通知者。
- Singer Bean是扩展自audienceProxyBase Bean,
- Singer Bean的第一个属性告诉ProxyFactoryBean哪个是Bean的代理,interceptorNames则告诉ProxyFactoryBean哪个通知者要应用于被代理的Bean
- proxyInterfaces属性告诉ProxyFactoryBean代理应该实现哪个接口
通知者类:
package entertainment;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/*
* 创建通知
*/
public class ListenerAdvice implements MethodBeforeAdvice,AfterReturningAdvice{
private Listener listener;
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
listener.takeSeat();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
listener.applaud();
}
public void setListener(Listener listener){
this.listener=listener;
}
}
分享到:
相关推荐
java 无需数据库 简单实例 如何实现Spring依赖注入 AOP
Unity结合三导实现依赖注入跟AOP Unity结合三导实现依赖注入跟AOP.zip
以Spring5为基础进行的仿写,测试代码有简单的依赖注入和aop日志打印
简单的Java依赖注入框架,代码量少,实现了依赖注入和AOP。适合Spring源码的初学者掌握其核心原理
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
ORM框架 整合asp.net core mvc和Dapper、Autofac实现依赖注入和AOP,同时结合Redis实现 _缓
其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的...
Spring-注入依赖,AOP,自动注入Bean。入门。为什么没有0积分?
Unity结合三导实现依赖注入跟AOP
Spring的核心:IOC与AOP。IOC是控制反转或依赖注入,AOP是面向切面编程。
使用Spring配置文件实现AOP
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring基础Spring IOC和Spring AOP的原理及使用
SpringAOP 7个依赖包 SpringAOP 7个依赖包 SpringAOP 7个依赖包
spring做AOP开发所依赖的三个jar包 aopalliance-1.0.jar aspectjweaver-1.7.0.jar spring-aspects-4.0.2.RELEASE.jar
源代码是spring-boot添加aop切面功能的demo,简单易懂.
学习Spring开发的AOP面向切面编程时所需要的jar包,包括com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar