Spring自学第一天

 

Spring提供的控制反转(Inversion of Control,即IOC)和面向方面编程(Aspect oriented programming ,Aop)插件式架构降低了应用组件之间的依赖性。

借助于xml定义文件,开发者能够在运行时连接不同的应用组件。这对于单元测试特别有用,特别是那些需要针对不同客户实施不同配置的应用而言。

 目前,存在依赖注入的类型有三种:基于设置(setter-based)方法、基于构造器(才constructor-based)、以及基于借口(interface-based)注入。而Spring_Ioc支持前两种,即借助于Spring开发者可以通过设置或者可以通过构造器创建对象,并对象的状态进行管理。

 

其中,依赖注入是Spring框架的基础。Spring在基于依赖注入的基础之上,同时还提供了其他大量的功能,比如Spring MVC框架,事务管理框架、DAO支持、支持主流的O/R Mapping工具,支持各种标准J2EE组件技术的集成(JMS/JavaMail/EJB/Web)服务等、集成各种视图(Web视图和非Web视图)技术,这也是Spring Ioc由于其他Ioc的理由。

package com.openv.spring;

 

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

 

import org.apache.commons.logging.Log;

 

import org.apache.commons.logging.LogFactory;

 

/**

 * 基于文件形式,读取HelloWorld所需的字符串

 *

 * @author jiangao

 * 实现了对传入名为profilename属性文件的读取工作,并能够打印出相关异常信息

 */

public class FileHelloStr {

       protected static final Log log = LogFactory.getLog(FileHelloStr.class);

 

       private String profilename;

   //通过构造函数new一个bean类

       public FileHelloStr(String profilename) {

              this.profilename = profilename;

       }

   /**

    *

    * @return 属性文件信息

    */

       public String getContent() {

              String helloworld = "";

              try {

                     Properties properties = new Properties();//new一个属性文件对象

                     InputStream is = getClass().getClassLoader().getResourceAsStream(

                                   profilename); //new一个文件流

                     properties.load(is);

                     is.close();

                     helloworld = properties.getProperty("Helloworld"); //读取文件的内容的键值

              } catch (FileNotFoundException ex) {

                     log.error(ex); //打印出异常

              } catch (IOException ex) {

                     log.error(ex);

              }

              return helloworld;

       }

}

 

 

 

 

package com.openv.bean;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

import com.openv.spring.FileHelloStr;

 

/**

 * 该类获得 hellowrold字符串

 *

 * @author jiangao

 *

 */

public class HelloWorld {

       protected static final Log log = LogFactory.getLog(HelloWorld.class);

 

       public String getContent() {

              FileHelloStr fhStr = new FileHelloStr("Helloworld.properties");

              String helloworld = fhStr.getContent();

 

              return helloworld;

       }

}

 

 

 

 

 

 

package com.openv.client;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

import com.openv.bean.HelloWorld;

/**

 * 客户端应用

 * @author jiangao

 *

 */

public class HelloWorldClient {

       protected static final Log log = LogFactory.getLog(HelloWorldClient.class);

 

       public static void main(String[] args) {

              HelloWorld hw = new HelloWorld();

              log.info(hw.getContent());

       }

}

 

通过上述过程,开发者可以看出:HelloWorld明显依赖于FileHelloStr。如果开发者需要通过其他途径获得”HelloWorld”信息,则需要重构现有的FileHelloStr类,即通过更通用的HelloStr接口形式给出。一种较好的实现方式是将创建FiileHelloStr对象的职责委派给HelloWorldClient客户。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值