Spring入门

 
一、IOC反向控制:
反向控制,是工厂模式的简单实现。引用类的接口,在Client程序中,为了注入远程方法,直接将使用 BeanFactory 来得到希望得到的类。该类在XML文件中被定义。
1.applicationContext.xml文件定义
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
 
     < bean name = "fileHelloWorld"
          class = "com.openv.spring.HelloWorld" >
          < constructor-arg >
               < ref bean = "fileHello" />
          </ constructor-arg >    
     </ bean >
 
     < bean name = "fileHello"
          class = "com.openv.spring.FileHelloStr" >
          < constructor-arg >
               < value > helloworld.properties </ value >
          </ constructor-arg >  
     </ bean >
</ beans >
 
2.       Client调用:
Resource resource = new ClassPathResource( "applicationContext.xml " );//读入资源文件
        BeanFactory factory = new XmlBeanFactory(resource);//构造Bean工厂
        IHelloWorld hw = (IHelloWorld) factory.getBean( "helloworldbean" );//得到XML定义的Bean;
log .info(hw.getContent( "luoshifei" ));//调用BEAN方法。
 
 
二、AOP切面编程:
该功能主要是模拟EJB的日志,线程管理等任务性功能。不是业务功能。被称为装置。这些装置分为:提前装置,之后装置,异常装置,环绕装置( LoggingAroundAdvice)。
环绕装置具有其他所有的功能,因此最提倡使用。
1、 装置XML的定义模板:
<? 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 = "helloworldbean"
       class = "org.springframework.aop.framework.ProxyFactoryBean" >//实现协议 ProxyFactoryBean
      < property name = "proxyInterfaces" >
         < value > com.openv.spring.IHelloWorld </ value >
      </ property >
      < property name = "target" >//该属性“目标”代表要实际运行的Bean
         < ref local = "helloworldbeanTarget" />
      </ property >
      < property name = "interceptorNames" >// 该属性“交互者”代表指向装置BEAN.
         < list >
            < value > loggingAroundAdvisor </ value >
         </ list >
      </ property >
   </ bean >
 
   < bean id = "helloworldbeanTarget"
       class = "com.openv.spring.HelloWorld" />
 
   < bean id = "loggingAroundAdvisor"
      class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
      < property name = "advice" >
         < ref local = "loggingAroundAdvice" />
      </ property >
      < property name = "pattern" >
         < value > .* </ value >
      </ property >
   </ bean >
   
   < bean id = "loggingAroundAdvice"
      class = "com.openv.spring.LoggingAroundAdvice" />
 
</ beans >
2、  装置的自我实现:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
 
/**
 * Around装备实现
 *
 * @author luoshifei
 */
public class LoggingAroundAdvice implements MethodInterceptor {
    protected static final Log log = LogFactory.getLog(LoggingAroundAdvice.class);
 
    public Object invoke(MethodInvocation invocation) throws Throwable {
        log.info("before: The Invocation of getContent()");
        invocation.getArguments()[0] = "luoshifei";
        invocation.proceed();
        log.info("after: The Invocation of getContent()");
 
        return null;
    }
}
 
3、  装置会在目标对象动作时自我启动和按要求运行:
/**
 * HelloWorld客户应用
 *
 * @author luoshifei
 */
public class HelloClient {
    protected static final Log log = LogFactory.getLog(HelloClient.class);
 
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("appcontext.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        IHelloWorld hw = (IHelloWorld) factory.getBean("helloworldbean");//注意这个BEAN是实际的运行BEAN和装置绑定后的结果。
        log.info(hw.getContent("luoshifei"));
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值