Spring Bean基本管理实例详解

时间:2022-06-23 23:43:50

本文实例讲述了Spring Bean基本管理。分享给大家供大家参考,具体如下:

一、使用setter方式完成依赖注入

下面是Bean和beans-config.xml文件。

?
1
2
3
4
public class HelloBean {
  private String helloWord;
  //...省略getter、setter方法  
}
?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="helloBean"
     class="onlyfun.caterpillar.HelloBean">
    <property name="helloWord">
      <value>Hello!Justin!</value>
    </property>
  </bean>
</beans>
?
1
2
3
4
5
6
7
8
public class SpringDemo {
  public static void main(String[] args) {
    Resource rs = new FileSystemResource("beans-config.xml");
    BeanFactory factory = new XmlBeanFactory(rs);
    HelloBean hello = (HelloBean) factory.getBean("helloBean");
    System.out.println(hello.getHelloWord());
  }
}

二、使用constructor方式完成注入

?
1
2
3
4
5
6
7
8
9
10
11
12
public class HelloBean {
  private String name;
  private String helloWord;
  // 建议有要无参数建构方法
  public HelloBean() {
  }
  public HelloBean(String name, String helloWord) {
    this.name = name;
    this.helloWord = helloWord;
  }
  //...省略getter、setter方法  
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="helloBean"
     class="onlyfun.caterpillar.HelloBean">
    <constructor-arg index="0">
      <value>Justin</value>
    </constructor-arg>
    <constructor-arg index="1">
      <value>Hello</value>
    </constructor-arg>
  </bean>
</beans>
?
1
2
3
4
5
6
7
8
9
10
11
public class SpringDemo {
  public static void main(String[] args) {
    ApplicationContext context =
      new FileSystemXmlApplicationContext("beans-config.xml");
    HelloBean hello = (HelloBean) context.getBean("helloBean");
    System.out.print("Name: ");
    System.out.println(hello.getName());
    System.out.print("Word: ");
    System.out.println(hello.getHelloWord());
  }
}

三、属性参考

?
1
2
3
4
5
public class HelloBean {
  private String helloWord;
  private Date date;
  //...省略getter、setter方法  
}
?
1
2
3
4
5
6
7
8
9
10
11
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
    <property name="date">
      <ref bean="dateBean"/>
    </property>
  </bean>
</beans>
?
1
2
3
4
5
6
7
8
9
10
11
public class SpringDemo {
  public static void main(String[] args) {
    ApplicationContext context =
      new FileSystemXmlApplicationContext("beans-config.xml");
    HelloBean hello = (HelloBean) context.getBean("helloBean");
    System.out.print(hello.getHelloWord());
    System.out.print(" It's ");
    System.out.print(hello.getDate());
    System.out.println(".");
  }
}

四、“byType”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按类型自动绑定。

?
1
2
3
4
5
6
7
8
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
  </bean>
</beans>

五、“byName”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按名称自动绑定。

?
1
2
3
4
5
6
7
8
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
  </bean>
</beans>

六、“constructor”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的按构造方法自动绑定。在建立依赖关系时,Srping容器会试图比对容器中的Bean实例类型,及相关的构造方法上的参数类型,看看在类型上是否符合,如果有的话,则选用该构造方法来建立Bean实例。如果无法绑定,则抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。

?
1
2
3
4
5
6
7
8
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
  </bean>
</beans>

六、“autodetect”自动绑定

将“三”中的配置文件改为下面,即可完成bean属性的自动绑定,这个自动绑定是Spring会尝试用入constructor来处理依赖关系的建立,如果不行,则再尝试用byType类建立依赖关系。

?
1
2
3
4
5
6
7
8
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
  </bean>
</beans>

七、依赖检查方式

在自动绑定中,由于没办法从定义文件中,清楚地看到是否每个属性都完成设定,为了确定某些依赖关系确实建立,您可以假如依赖检查,在<bean>标签使用时设定"dependency-check",可以有四种依赖检查方式:simple、objects、all、none。

simple:只检查简单的类型(像原生数据类型或字符串对象)属性是否完成依赖关系,。
objects:检查对象类型的属性是否完成依赖关系。
all:则检查全部的属性是否完成依赖关系。
none:设定是默认值,表示不检查依赖性。

?
1
2
3
4
5
6
7
8
<beans>
  <bean id="dateBean" class="java.util.Date"/>
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all">
    <property name="helloWord">
      <value>Hello!</value>
    </property>
  </bean>
</beans>

八、集合对象注入

对于像数组、List、Set、Map等集合对象,在注入前必须填充一些对象至集合中,然后再将集合对象注入至所需的Bean时,也可以交由Spring的IoC容器来自动维护或生成集合对象,并完成依赖注入。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class SomeBean {
  private String[] someStrArray;
  private Some[] someObjArray;
  private List someList;
  private Map someMap;
  public String[] getSomeStrArray() {
    return someStrArray;
  }
  public void setSomeStrArray(String[] someStrArray) {
    this.someStrArray = someStrArray;
  }
  public Some[] getSomeObjArray() {
    return someObjArray;
  }
  public void setSomeObjArray(Some[] someObjArray) {
    this.someObjArray = someObjArray;
  }
  public List getSomeList() {
    return someList;
  }
  public void setSomeList(List someList) {
    this.someList = someList;
  }
  public Map getSomeMap() {
    return someMap;
  }
  public void setSomeMap(Map someMap) {
    this.someMap = someMap;
  }
}
public class Some {
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String toString() {
    return name;
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="some1" class="onlyfun.caterpillar.Some">
    <property name="name">
      <value>Justin</value>
    </property>
  </bean>
  <bean id="some2" class="onlyfun.caterpillar.Some">
    <property name="name">
      <value>momor</value>
    </property>
  </bean>
  <bean id="someBean" class="onlyfun.caterpillar.SomeBean">
    <property name="someStrArray">
      <list>
        <value>Hello</value>
        <value>Welcome</value>
      </list>
    </property>
    <property name="someObjArray">
      <list>
         <ref bean="some1"/>
         <ref bean="some2"/>
      </list>
    </property>
    <property name="someList">
      <list>
         <value>ListTest</value>
         <ref bean="some1"/>
         <ref bean="some2"/>
      </list>
    </property>
    <property name="someMap">
      <map>
         <entry key="MapTest">
           <value>Hello!Justin!</value>
         </entry>
         <entry key="someKey1">
           <ref bean="some1"/>
         </entry>
      </map>
    </property>
  </bean>
</beans>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class SpringDemo {
  public static void main(String[] args) {
    ApplicationContext context =
      new FileSystemXmlApplicationContext(
          "beans-config.xml");
    SomeBean someBean =
      (SomeBean) context.getBean("someBean");
    // 取得数组型态依赖注入对象
    String[] strs =
      (String[]) someBean.getSomeStrArray();
    Some[] somes =
      (Some[]) someBean.getSomeObjArray();
    for(int i = 0; i < strs.length; i++) {
      System.out.println(strs[i] + ","
          + somes[i].getName());
    }
    // 取得List型态依赖注入对象
    System.out.println();
    List someList = (List) someBean.getSomeList();
    for(int i = 0; i < someList.size(); i++) {
      System.out.println(someList.get(i));
    }
    // 取得Map型态依赖注入对象
    System.out.println();
    Map someMap = (Map) someBean.getSomeMap();
    System.out.println(someMap.get("MapTest"));
    System.out.println(someMap.get("someKey1"));
  }
}

希望本文所述对大家Java程序设计有所帮助。