用代码一步步学习Spring:IoC,AOP

1 从http://www.springframework.org下载Spring
2 用eclipse新建Java项目
3 建立我们的业务方法接口
java 代码
 
  1. public interface BusinessObject {  
  2. public void doSomething();  
  3. public void doAnotherThing();  
  4. }  

4 实现业务方法,注意这是的setWords使用了依赖注入,所谓依赖注入就是把配置文件中的字符串什么的在程序运行时“自动”放到我们的程序中来。如果不 是这样,我们就只能在代码中固化这些东西,从而违背了面向对象的依赖倒置原则,还有一种满足依赖倒置的方法,即依赖查询,这就是所谓的factory模 式,即在代码中请求某种抽象的东西,然后根据配置得到它,但这种办法向对于依赖注入多了对环境的依赖,且代码冗余,EJB的JNDI查询就属于这种。另外 我们的Spring配置文件是以bean为核心的,就是我们写的一个类,在XML中描述它的名称、位置和涵盖的内容、关系。
java 代码
 
  1. public class BusinessObjectImpl implements BusinessObject {  
  2. private String words;  
  3. public void setWords(String words){  
  4. this.words = words;  
  5. }  
  6. public void doSomething() {  
  7. Log log = LogFactory.getLog(this.getClass());  
  8. log.info(words);  
  9. }  
  10. public void doAnotherThing() {  
  11. Log log = LogFactory.getLog(this.getClass());  
  12. log.info("Another thing");  
  13. }  
  14.   
  15. }  

5 建立一个运行方法类,从配置文件spring-beans.xml中读入bo这个类的定义,然后实例化一个对象
java 代码
 
  1. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  2. import org.springframework.core.io.ClassPathResource;  
  3.   
  4.   
  5. public class Main {  
  6. public static void main(String[] args){  
  7. XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));  
  8. BusinessObject bo = (BusinessObject)xbf.getBean("bo");  
  9. bo.doSomething();  
  10. bo.doAnotherThing();  
  11. }  
  12. }  

6 建立一个拦截器类invoke是MethodInterceptor必须实现的方法,表示拦截时的动作,大家仔细体会代码中的含义
java 代码
 
  1. import org.aopalliance.intercept.MethodInterceptor;  
  2. import org.aopalliance.intercept.MethodInvocation;  
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5.   
  6.   
  7. public class MyInterceptor implements MethodInterceptor {  
  8. private String before, after;  
  9. public void setAfter(String after) {  
  10. this.after = after;  
  11. }  
  12. public void setBefore(String before) {  
  13. this.before = before;  
  14. }  
  15. public Object invoke(MethodInvocation invocation) throws Throwable {  
  16. Log log = LogFactory.getLog(this.getClass());  
  17. log.info(before);  
  18. Object rval = invocation.proceed();  
  19. log.info(after);  
  20. return rval;  
  21. }  
  22. }  

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

xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="businessObjectImpl" class="BusinessObjectImpl">  
  5.         <property name="words">  
  6.             <value>正在执行业务方法</value>  
  7.         </property>  
  8.     </bean>  
  9.     <bean id="myInterceptor" class="MyInterceptor">  
  10.         <property name="before">  
  11.             <value>执行业务方法前</value>  
  12.         </property>  
  13.         <property name="after">  
  14.             <value>执行业务方法后</value>  
  15.         </property>  
  16.     </bean>  
  17.     <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
  18.     <property name="patterns">  
  19.         <list>  
  20.             <value>BusinessObject.doSomething</value>  
  21.         </list>  
  22.     </property>  
  23.     </bean>  
  24.     <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
  25.       <property name="pointcut" ref="myPointcut"/>  
  26.       <property name="advice" ref="myInterceptor"/>  
  27.     </bean>  
  28.     <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">  
  29.         <property name="target">  
  30.             <ref local="businessObjectImpl"/>  
  31.         </property>  
  32.         <property name="proxyInterfaces">  
  33.             <value>BusinessObject</value>  
  34.         </property>  
  35.         <property name="interceptorNames">  
  36.             <list>  
  37.                 <value>myInterceptor</value>  
  38.                 <value>myAdvisor</value>  
  39.             </list>  
  40.         </property>  
  41.     </bean>  
  42. </beans>  


8 运行Main类,观察控制台输出结果,重新审查代码,反思为什么会出现这种结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值