Spring的一个入门例子

时间:2019-03-18
本文章向大家介绍Spring的一个入门例子,主要包括Spring的一个入门例子使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

例子参考于:Spring系列教材 以及《轻量级JavaEE企业应用实战》

Axe.class

package com.how2java.bean;

public class Axe {
    public String chop() {
        return "使用斧头砍柴";
    }
}

Person.class

package com.how2java.bean;

public class Person {
    private Axe axe;

    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    public void useAxe() {
        System.out.println("我打算去砍点柴!");
        System.out.println(axe.chop());
    }

}

BeanTest.class

package com.how2java.bean;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BeanTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
        //创建Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"beans.xml"});
        /*
         * Spring 容器获取Bean对象主要有如下两个方法
         * 1).Object getBean(String id):根据容器中Bean的id来获取指定Bean,获取Bean之后要进行强制类型转换
         * 2).T getBean(String name,Class<T> requiredType): 根据容器中的Bean的id来获取指定Bean,但该方法
         *    带一个泛型参数,因此获取Bean后无须进行强制类型转换。
         */
        //使用方法2).来获取id为person的Bean
        Person p = ctx.getBean("person",Person.class);
        
        //使用方法1).来获取id为person的Bean
        //Person p = (Person) ctx.getBean("person");
        
        p.useAxe();

    }

}

beans.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <!-- 配置名为person的Bean,其实现类为com.how2java.Person -->
    <!-- 下面配置的property元素name为axe,该元素将驱动Spring以反射方式执行person Bean中的setAxe()方法,ref的属性值为 
        axe,该属性值指定以容器中名为axe的Bean作为执行setter方法的传入参数 也就是在底层,会执行一些方法,最终将name="axe"这个属性反射成setAxe()这个方法,然后因为下面的bean指定了一个名为axe的 
        Bean,因此通过ref="axe"来指定setter方法的参数为名为axe的Bean -->
        
    <bean id="person" class="com.how2java.bean.Person">
        <!-- 控制调用setAxe()方法,将容器中的axe Bean作为传入参数 -->
        <property name="axe" ref="axe" />
    </bean>

    <!-- 配置名为axe的Bean,其实现类为com.how2java.Axe -->
    <bean id="axe" class="com.how2java.bean.Axe" />

</beans>

笔记:

<property>作为<bean>的子元素,它驱动Spring在底层以反射执行一次setter方法,其中<property...>的name属性决定执行哪个setter方法,而value或者是ref决定执行setter方法传入的参数

那么什么时候用ref什么时候用value?

当传入参数是基本类型及其包装类、string等类型,使用value
如果以容器中其他Bean作为传入参数则使用ref指定传入参数