spring中bean的配置

来源:互联网 发布:美分知乎 编辑:程序博客网 时间:2024/05/28 22:13


1.配置形式:

    (1) 基于XML文件的方式

            1.在xml文件中通过节点来配置bean

[html] view plain copy
 print?
  1. <!-- 配置bean -->  
  2. <bean id="helloworld" class="com.wul.spring.beans.Helloworld">  
  3. <property name="name" value="wul"></property>  
  4. </bean>  

2.id:Bean的名称

---  在IOC容器中必须是唯一的
---  若id没有指定,Spring自动将权限定性类名作为Bean的名字
---  id可以指定多个名字,名字之间可用逗号,分号,或空格分隔

     (2)基于注解的方式

2.Bean的配置方式:

      (1)通过全类名(反射)

[html] view plain copy
 print?
  1. <!--   
  2. 配置bean   
  3. Class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器  
  4. id:标识容器中的bean,id唯一    
  5. -->  
  6. <bean id="helloworld" class="com.wul.spring.beans.Helloworld">  
  7. <property name="name" value="wul"></property>  
  8. </bean>  

      (2) 通过工厂方法(静态工厂方法&实例工厂方法) 

      (3)FactoryBean

3.IOC容器BeanFactory&ApplicationContext概述

( 1 ) SpringIOC容器

        在Spring IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化,才可以从IOC容器里获取Bean实例并使用。

( 2 )IOC容器实现

         Spring提供了两种类型的IOC容器实现
         (1)  BeanFactory IOC容器的基本实现
 (2)  ApplicationContext提供了更多的高级特性,是BeanFactory的子接口。
        -BeanFactory是Spring框架的基础设施,面向Spring本身;
         ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用  ApplicationContext而非底层的BeanFactory
       -无论使用何种方式,配置文件是相同的。

( 3 )ApplicatonContext

        (1)ApplicatonContext代表 Spring 的IOC容器,是一个接口。
        (2)ApplicationContext的主要实现类:
1.ClassPatnXmlApplicationContext:从类路径下加载配置文件
2.FileSystemXmlApplicationContext:从文件系统下加载配置文件
(3)ApplicatonContext的拓展类
       ConfigurationApplicationContext拓展于ApplicationContext,新增加两个主要方法:refresh()和close(),让ApplicationContext
        具有启动,刷新和关闭上下文的能力。
                WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。

4.依赖注入的方式:

        (1)属性注入

属性注入即通过setter方法注入Bean的属性值或依赖的对象
属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>字节点指定属性值
属性注入是实际应用中最常用的注入方式
属性注入时:必须要求对应bean有无参的构造器和setter方法
[html] view plain copy
 print?
  1. <bean id="helloworld" class="com.wul.spring.beans.Helloworld">  
  2.     <property name="name" value="wul"></property>  
  3. </bean>  

        (2)构造器注入

通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用
构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性
构造方法注入:必须要求bean有对应参数的构造器

[html] view plain copy
 print?
  1. <!-- 通过构造方法来配置bean的属性 -->  
  2. <bean id="car" class="com.wul.spring.beans.Car">  
  3.     <constructor-arg value="Audi" index="0"></constructor-arg>  
  4.     <constructor-arg value="shanghai" index="1"></constructor-arg>  
  5.     <constructor-arg value="300000" type="double"></constructor-arg>  
  6. </bean>  

使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器
Car.java
[java] view plain copy
 print?
  1. public Car(String brand, String corp, double price) {  
  2.     super();  
  3.     this.brand = brand;  
  4.     this.corp = corp;  
  5.     this.price = price;  
  6. }  
  7.   
  8. public Car(String brand, String corp, int maxSpeed) {  
  9.     super();  
  10.     this.brand = brand;  
  11.     this.corp = corp;  
  12.     this.maxSpeed = maxSpeed;  
  13. }  
application.xml
[html] view plain copy
 print?
  1. <bean id="car2" class="com.wul.spring.beans.Car">  
  2.     <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>  
  3.     <constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>  
  4.     <constructor-arg value="240" type="int"></constructor-arg>  
  5. </bean>  

     (3)工厂方法注入(很少注入,不推荐)



Helloworld.java
[java] view plain copy
 print?
  1. package com.wul.spring.beans;  
  2.   
  3. public class Helloworld {  
  4.       
  5.     private String name;  
  6.   
  7.   
  8.     public void setName(String name) {  
  9.         this.name = name;  
  10.     }  
  11.       
  12.     public void hello(){  
  13.         System.out.println("Hello "+name);  
  14.     }  
  15.       
  16.     /*   
  17.      若写带参构造器必须再写个无参构造器 
  18.     public Helloworld(String name){ 
  19.         this.name = name; 
  20.     }*/  
  21.       
  22. }  
Car.java
[java] view plain copy
 print?
  1. package com.wul.spring.beans;  
  2.   
  3. public class Car {  
  4.       
  5.     private String brand;  
  6.     private String corp;  
  7.     private double price;  
  8.     private int maxSpeed;  
  9.       
  10.       
  11.       
  12.       
  13.     public String getBrand() {  
  14.         return brand;  
  15.     }  
  16.   
  17.     public void setBrand(String brand) {  
  18.         this.brand = brand;  
  19.     }  
  20.   
  21.     public String getCorp() {  
  22.         return corp;  
  23.     }  
  24.   
  25.     public void setCorp(String corp) {  
  26.         this.corp = corp;  
  27.     }  
  28.   
  29.     public double getPrice() {  
  30.         return price;  
  31.     }  
  32.   
  33.     public void setPrice(double price) {  
  34.         this.price = price;  
  35.     }  
  36.   
  37.     public int getMaxSpeed() {  
  38.         return maxSpeed;  
  39.     }  
  40.   
  41.     public void setMaxSpeed(int maxSpeed) {  
  42.         this.maxSpeed = maxSpeed;  
  43.     }  
  44.   
  45.     public Car(){  
  46.           
  47.     }  
  48.       
  49.     public Car(String brand, String corp, double price) {  
  50.         super();  
  51.         this.brand = brand;  
  52.         this.corp = corp;  
  53.         this.price = price;  
  54.     }  
  55.       
  56.     public Car(String brand, String corp, int maxSpeed) {  
  57.         super();  
  58.         this.brand = brand;  
  59.         this.corp = corp;  
  60.         this.maxSpeed = maxSpeed;  
  61.     }  
  62.   
  63.   
  64.   
  65.   
  66.   
  67.   
  68.   
  69.     @Override  
  70.     public String toString() {  
  71.         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price  
  72.                 + ", maxSpeed=" + maxSpeed + "]";  
  73.     }  
  74.       
  75.       
  76.       
  77. }  

applicationContext.xml
[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  5.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">  
  6.       
  7.     <!--   
  8.         配置bean   
  9.         Class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器  
  10.         id:标识容器中的bean,id唯一    
  11.     -->  
  12.     <bean id="helloworld" class="com.wul.spring.beans.Helloworld">  
  13.         <property name="name" value="wul"></property>  
  14.     </bean>  
  15.       
  16.     <!-- 通过构造方法来配置bean的属性 -->  
  17.     <bean id="car" class="com.wul.spring.beans.Car">  
  18.         <constructor-arg value="Audi" index="0"></constructor-arg>  
  19.         <constructor-arg value="shanghai" index="1"></constructor-arg>  
  20.         <constructor-arg value="300000" type="double"></constructor-arg>  
  21.     </bean>  
  22.       
  23.     <!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器 -->  
  24.     <bean id="car2" class="com.wul.spring.beans.Car">  
  25.         <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>  
  26.         <constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>  
  27.         <constructor-arg value="240" type="int"></constructor-arg>  
  28.     </bean>  
  29.       
  30. </beans>  
Main.java
[java] view plain copy
 print?
  1. package com.wul.spring.beans;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Main {  
  7.       
  8.     public static void main(String[] args){  
  9.           
  10.   
  11.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  12.   
  13.         Helloworld helloworld = (Helloworld) ctx.getBean("helloworld");  
  14.           
  15.         helloworld.hello();  
  16.           
  17.         Car car = (Car) ctx.getBean("car");  
  18.         System.out.println(car);  
  19.           
  20.         Car car2 = (Car) ctx.getBean("car2");  
  21.         System.out.println(car2);  
  22.           
  23.           
  24.     }  
  25. }  
0 0
原创粉丝点击