zoukankan      html  css  js  c++  java
  • 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指定传入参数

  • 相关阅读:
    Three Algorithms for Fibonacci
    微软面试经历
    [TIP]命令行快速查看图片(Ubuntu)
    emacs as the c++ ide on the Ubuntu
    boost learn notes
    ReadingNotes@02122013
    ignoreunderline.org
    cnblogsminormode.org
    c++ 0x 新特性
    noip模拟赛 思考熊的马拉松
  • 原文地址:https://www.cnblogs.com/Guhongying/p/10553327.html
Copyright © 2011-2022 走看看