第一个spring实例

http://www.2cto.com/kf/201204/127850.html


第一个spring实例 
http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下 
1. 首先导入两个包,分别为: 
dist\spring.jar      为spring的核心jar文件 
lib\jakarta-commons\commons-logging.jar  
注:不是最新的版本,如果找不到commons-logging.jar,可以引入struts2下的日志jar: 
commons-logging-1.1.1.jar 
2.写spring的配置文件 
首先在src下创建一个xml,名称为applicationContext.xml,在xml中配置,代码如下: 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
... 
</beans> 
这样就声明了spring的配置文件,需要注意的是applicationContext.xml是spring默认的xml文件名,这个文件名和hibernate中的hibernate.cfg.xml的不同,spring中的applicationContext.xml可以改变。 
3.因为spring是对service进行操作的,所以我们在service下来测试 
首先在cn.csdn.hr.service包下创建一个接口为GreetingService.java,写上抽象的方法,为: 
package cn.csdn.hr.service; 
public interface GreetingService { 
    public void sayGeeting(); 

 
在GreetingServiceImpl中去实现接口的方法。为: 
package cn.csdn.hr.service; 
public class GeetingServiceBean implements GreetingService { 
    private String greeting; 
    /* bean配置文件中property属性name 的名称和greeting一致,自动通过set方法注入 */ 
    // 依赖注入 
    public void setGreeting(String greeting) { 
        this.greeting = greeting; 
    } 
 
    @Override 
    public void sayGeeting() { 
        // TODO Auto-generated method stub 
        System.out.println(greeting); 
    } 

4.在applicationContext.xml中加入bean属性,为: 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
    <bean id="geetingServiceBean" class="cn.csdn.hr.service.GeetingServiceBean" scope="prototype"> 
        <property name="greeting"> 
            <value>你好</value> 
        </property> 
    </bean> 
</beans> 
 
这样,我们就配置好了,那么怎么运行一下spring,看一下效果呢?来建一个junit测试吧! 
测试的内容,第一种: 
public void test() { 
        // 装配bean 
        // 创建依赖的资源文件 
        Resource resource = new ClassPathResource("applicationContext.xml"); 
        // 创建beanFacory工厂 
        BeanFactory beanFactory = new XmlBeanFactory(resource); 
        //获取BeanFactory工厂创建的bean对象 得到一个bean实例对象 
        GreetingService greetingService = (GreetingService) beanFactory 
                .getBean("geetingServiceBean");// xml资源文件中的classbean的id名称 
        // beanFactory.getBean("geetingServiceBean")实现类的对象 面向接口编程 
        // 使用bean的实例 
        greetingService.sayGeeting(); 
    } 
注:在创建Resource的实例的时候可以使用FileSystemResource,在写路径的时候要写为绝对的路径,为: 
D:\\Ncode\\workSpace\\spring\\springJava\\src\\applicationContext.xml 
第二中测试的方式: 
    public void test1() { 
        // 装配bean 
        // 创建依赖的资源文件 
        BeanFactory beanFactory = new ClassPathXmlApplicationContext( 
                "applicationContext.xml"); 
        // 获取BeanFactory工厂创建的bean对象 
        GreetingService greetingService = (GreetingService) beanFactory 
                .getBean("geetingServiceBean");// xml资源文件中的classbean的id名称 
 
        // 使用bean的实例 
        greetingService.sayGeeting(); 
    } 
 
 
好了,这样第一个简单的spring实例就ok了。 
 
来说一下xml中bean的scope中的用法,着重的说两个,即为scope="" 
第一:默认的是singleton,为单例, 
        单例:当容器初始化的时候就创建对象一次,以后每次使用都是同一个对象 
    第二:prototype:  原型bean 
        每次请求都创建一个崭新的对象 
        说明:当容器初始化的时候不被创建,当使用该bean的时候再创建 
一般情况下使用的是prototype 


摘自 韩晓爽的专栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值