java

位置:IT落伍者 >> java >> 浏览文章

用代码学习Spring:IoC、AOP


发布日期:2022年03月08日
 
用代码学习Spring:IoC、AOP

从下载Spring

用eclipse新建Java项目

建立我们的业务方法接口

publicinterfaceBusinessObject{

publicvoiddoSomething();

publicvoiddoAnotherThing();

}

importmonsloggingLog;

importmonsloggingLogFactory;publicinterfaceBusinessObject{

publicvoiddoSomething();

publicvoiddoAnotherThing();

}

importmonsloggingLog;

importmonsloggingLogFactory;

实现业务方法注意这是的setWords使用了依赖注入所谓依赖注入就是把配置文件中的字符串什么的在程序运行时自动放到我们的程序中来如果不是这样我们就只能在代码中固化这些东西从而违背了面向对象的依赖倒置原则还有一种满足依赖倒置的方法即依赖查询这就是所谓的factory模式即在代码中请求某种抽象的东西然后根据配置得到它但这种办法向对于依赖注入多了对环境的依赖且代码冗余EJB的JNDI查询就属于这种另外我们的Spring配置文件是以bean为核心的就是我们写的一个类在XML中描述它的名称位置和涵盖的内容关系

publicclassBusinessObjectImplimplementsBusinessObject{

privateStringwords;

publicvoidsetWords(Stringwords){

thiswords=words;

}

publicvoiddoSomething(){

Loglog=LogFactorygetLog(thisgetClass());

(words);

}

publicvoiddoAnotherThing(){

Loglog=LogFactorygetLog(thisgetClass());

(Anotherthing);

}

}publicclassBusinessObjectImplimplementsBusinessObject{

privateStringwords;

publicvoidsetWords(Stringwords){

thiswords=words;

}

publicvoiddoSomething(){

Loglog=LogFactorygetLog(thisgetClass());

(words);

}

publicvoiddoAnotherThing(){

Loglog=LogFactorygetLog(thisgetClass());

(Anotherthing);

}

}

建立一个运行方法类从配置文件springbeansxml中读入bo这个类的定义然后实例化一个对象

importorgspringframeworkbeansfactoryxmlXmlBeanFactory;

importorgreioClassPathResource;

publicclassMain{

publicstaticvoidmain(String[]args){

XmlBeanFactoryxbf=newXmlBeanFactory(newClassPathResource(springbeansxml));

BusinessObjectbo=(BusinessObject)xbfgetBean(bo);

bodoSomething();

bodoAnotherThing();

}

}importorgspringframeworkbeansfactoryxmlXmlBeanFactory;

importorgreioClassPathResource;

publicclassMain{

publicstaticvoidmain(String[]args){

XmlBeanFactoryxbf=newXmlBeanFactory(newClassPathResource(springbeansxml));

BusinessObjectbo=(BusinessObject)xbfgetBean(bo);

bodoSomething();

bodoAnotherThing();

}

}

建立一个拦截器类invoke是MethodInterceptor必须实现的方法表示拦截时的动作大家仔细体会代码中的含义

importorgaopallianceinterceptMethodInterceptor;

importorgaopallianceinterceptMethodInvocation;

importmonsloggingLog;

importmonsloggingLogFactory;

publicclassMyInterceptorimplementsMethodInterceptor{

privateStringbeforeafter;

publicvoidsetAfter(Stringafter){

thisafter=after;

}

publicvoidsetBefore(Stringbefore){

thisbefore=before;

}

publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{

Loglog=LogFactorygetLog(thisgetClass());

(before);

Objectrval=invocationproceed();

(after);

returnrval;

}

}importorgaopallianceinterceptMethodInterceptor;

importorgaopallianceinterceptMethodInvocation;

importmonsloggingLog;

importmonsloggingLogFactory;

publicclassMyInterceptorimplementsMethodInterceptor{

privateStringbeforeafter;

publicvoidsetAfter(Stringafter){

thisafter=after;

}

publicvoidsetBefore(Stringbefore){

thisbefore=before;

}

publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{

Loglog=LogFactorygetLog(thisgetClass());

(before);

Objectrval=invocationproceed();

(after);

returnrval;

}

}

建立配置文件组织上面的类之间的关系AOP有切入点和增强这两个重要的概念把两个概念结合到一起就是一个在某个方法执行的时候附加执行切入点表示在哪里附加增强表示附加什么配置文件中的myPointcut表示切入点myInterceptor表示增强的内容myAdvisor表示增强器即两者的结合在bo这个bean中我们把这个增强器附加到了bo这个bean上

<?xmlversion=encoding=UTF?>

<!DOCTYPEbeansPUBLIC//SPRING//DTDBEAN//ENbeansdtd>

<beans>

<beanid=businessObjectImplclass=BusinessObjectImpl>

<propertyname=words>

<value>正在执行业务方法</value>

</property>

</bean>

<beanid=myInterceptorclass=MyInterceptor>

<propertyname=before>

<value>执行业务方法前</value>

</property>

<propertyname=after>

<value>执行业务方法后</value>

</property>

</bean>

<beanid=myPointcutclass=orgspringframeworkaopsupportJdkRegexpMethodPointcut>

<propertyname=patterns>

<list>

<value>BusinessObjectdoSomething</value>

</list>

</property>

</bean>

<beanid=myAdvisorclass=orgspringframeworkaopsupportDefaultPointcutAdvisor>

<propertyname=pointcutref=myPointcut/>

<propertyname=adviceref=myInterceptor/>

</bean>

<beanid=boclass=orgspringframeworkaopframeworkProxyFactoryBean>

<propertyname=target>

<reflocal=businessObjectImpl/>

</property>

<propertyname=proxyInterfaces>

<value>BusinessObject</value>

</property>

<propertyname=interceptorNames>

<list>

<value>myInterceptor</value>

<value>myAdvisor</value>

</list>

</property>

</bean>

</beans><?xmlversion=encoding=UTF?>

<!DOCTYPEbeansPUBLIC//SPRING//DTDBEAN//ENbeansdtd>

<beans>

<beanid=businessObjectImplclass=BusinessObjectImpl>

<propertyname=words>

<value>正在执行业务方法</value>

</property>

</bean>

<beanid=myInterceptorclass=MyInterceptor>

<propertyname=before>

<value>执行业务方法前</value>

</property>

<propertyname=after>

<value>执行业务方法后</value>

</property>

</bean>

<beanid=myPointcutclass=orgspringframeworkaopsupportJdkRegexpMethodPointcut>

<propertyname=patterns>

<list>

<value>BusinessObjectdoSomething</value>

</list>

</property>

</bean>

<beanid=myAdvisorclass=orgspringframeworkaopsupportDefaultPointcutAdvisor>

<propertyname=pointcutref=myPointcut/>

<propertyname=adviceref=myInterceptor/>

</bean>

<beanid=boclass=orgspringframeworkaopframeworkProxyFactoryBean>

<propertyname=target>

<reflocal=businessObjectImpl/>

</property>

<propertyname=proxyInterfaces>

<value>BusinessObject</value>

</property>

<propertyname=interceptorNames>

<list>

<value>myInterceptor</value>

<value>myAdvisor</value>

</list>

</property>

</bean>

</beans>

运行Main类观察控制台输出结果重新审查代码反思为什么会出现这种结果

               

上一篇:Struts标签中的多层logic:iterator详解

下一篇:Eclipse RCP关闭按钮的屏蔽方法