Spring Bean基本管理

Spring Bean基本管理
 
一、使用setter方式完成依赖注入
public class HelloBean {
 
 private String helloWord;
 private Date date;
           ......
}
public class TDemo {
 public static void main(String[] args){
    ApplicationContext acc=new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloBean hb=(HelloBean)acc.getBean("helloword");
    System.out.println(hb.getHelloWord());
 }
}
<beans
  <bean id="helloword" class="org.jack.com.HelloBean">
  <property name="helloWord" value="hello word !!!"></property>
  </bean>
</beans>
二、使用constructor方式完成注入
public class HelloBean {
 
 private String helloWord;
 private String name;
 public HelloBean(String helloWord,String name){
  this.helloWord=helloWord;
  this.name=name;
 }
               ........
}
public class TDemo {
 public static void main(String[] args){
    ApplicationContext acc=new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloBean hb=(HelloBean)acc.getBean("ides");
    System.out.println(hb.getHelloWord());
    System.out.println(hb.getName());
 }
}
<beans
  <bean id="ides" class="org.jack2.com.HelloBean">
  <constructor-arg index="0" value="我的世界,我来做主!!!">
  </constructor-arg>
  <constructor-arg index="1" value="我的名字,jack!">
  </constructor-arg>
  </bean>
</beans>
三、属性参考
public class HelloBean {
 
 private String helloWord;
 private Date date;
         ..........
}
public class TDemo {
 public static void main(String[] args){
    ApplicationContext acc=new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloBean hb=(HelloBean)acc.getBean("helloword");
    System.out.println(hb.getHelloWord());
    System.out.println(hb.getDate());
     }
}
<beans
<bean  id="datetime" class="java.util.Date"></bean>
<bean id="helloword" class="org.jack3.com.HelloBean">
 <property name="helloWord" value="这个世界是好,是坏,都控制在自己心态!!!">
 </property>
 <property name="date" ref="datetime">
 </property>
 </bean>
</beans>
四、综合的总结:
普通类:
public class Ts {
 
 private String helloWord;
 private Date date;
 
 public Ts(){}
 
 public Ts(Date date){
  this.date=date;
   }
}
测试类:
public static void main(String[] args){
    ApplicationContext  acc= new ClassPathXmlApplicationContext("applicationContext.xml");
    Ts t=(Ts)acc.getBean("根据实际情况填写");
   
    System.out.println("date: "+t.getDate());
    System.out.println("helloWord: "+t.getHelloWord());
 }
配置文件如下:
<beans
<bean id="date" class="java.util.Date"></bean>
<!-- 以指定属性的形式,写配置文件 -->
<!--
<bean id="tt" class="org.jack.com.Ts">
<property name="helloWord" value="你还,好吗?"></property>
<property name="date" ref="date"></property>
</bean>
 -->
 
<!-- autowire="autodetect"; 这个自动绑定是Spring会尝试用入constructor来处理依赖关系的建立 -->
<!--
<bean id="ts" class="org.jack.com.Ts" autowire="autodetect">
       <property name="helloWord" value="hello!!!"/>
</bean>
 -->
 
<!-- autowire="byType";即可完成bean属性的按类型自动绑定  -->
<!--
<bean id="hw" class="org.jack.com.Ts" autowire="byType">
       <property name="helloWord" value="hello!!!"/>
</bean>
 -->
 
<!--  autowire="byName";即可完成bean属性的按名称自动绑定 --> 
   <!--
    <bean id="helloBean" class="org.jack.com.Ts" autowire="byName"> 
        <property name="helloWord"> 
            <value>Hello!</value> 
        </property> 
    </bean>
     -->
    
<!-- autowire="constructor";即可完成bean属性的按构造方法自动绑定。在建立依赖关系时,
Srping容器会试图比对容器中的Bean实例类型,及相关的构造方法上的参数类型,看看在类型上是否符合,
如果有的话,则选用该构造方法来建立Bean实例。  -->
<!--  
    <bean id="helloBean" class="org.jack.com.Ts" autowire="constructor"> 
        <property name="helloWord"> 
            <value>今天应该完成的任务!!!</value> 
        </property> 
    </bean>   
 -->   
     
<!-- 在自动绑定中,由于没办法从定义文件中,清楚地看到是否每个属性都完成设定,
     为了确定某些依赖关系确实建立,您可以假如依赖检查,在<bean>标签使用时设定"dependency-check",
     可以有四种依赖检查方式:simple、objects、all、none。
 
    simple:只检查简单的类型(像原生数据类型或字符串对象)属性是否完成依赖关系,。
    objects:检查对象类型的属性是否完成依赖关系。
    all:则检查全部的属性是否完成依赖关系。
    none:设定是默认值,表示不检查依赖性。 -->
<bean id="ts" class="org.jack.com.Ts" autowire="autodetect" dependency-check="all">
       <property name="helloWord" value="hello!!!"/>
</bean> 
</beans>

猜你喜欢

转载自jackroomage.iteye.com/blog/1013951