spring:按照Bean的名称自动装配User

本实例将介绍如何按照Bean 的名称自动装配 User 对象!

<bean> 元素的 autowire 属性负责自动装配 <bean> 标签,定义 JavaBean 的属性。这样做可以省去很多配置 JavaBean 属性的标签代码,使代码更整洁、美观;

但是也有负面影响:使用自动装配之后,无法从配置文件中读懂 JavaBean 需要什么属性。

1.编写User 对象,代码如下:

package com.importnew;

public class User {
    private String name="test";
    private String sex="男";
    private int age=29;
    private String tel="123456789";

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", sex=" + sex + ", age=" + age
                + ", tel=" + tel + "]";
    }
    
}

 

2.定义 Bean (TestUtil),将 User 对象注入到 TestUtil 对象中。代码如下:

package com.importnew;
  
public class TestUtil {    private User user;    public User getUser() {    return user;    }    public void setUser(User user) {    this.user = user;    } }

 

3.在 Spring 的配置文件 applicationContext.xml 中设置 Bean 的自动装配,Spring 将根据 Bean 中的属性名称自动将 User 对象注入到指定的 Bean 中。代码如下;

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
   
     <bean id="user" class="com.importnew.User" />
     
     <bean autowire="byName" id="testUtil" class="com.importnew.TestUtil"  />
     
</beans>

 

4.编写测试类 TestSpring ,代码如下:

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.importnew.TestUtil;
import com.importnew.User;

public class TestSpring {

    public static void main(String[] args) {     
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestUtil testUtil = (TestUtil) context.getBean("testUtil");
        User user = testUtil.getUser();
        System.out.println(user.getAge());
        System.out.println(user.getName());
    }
}

 

———————————————————————————————————————————————————

备注:

1.按 Bean 名称自动装配存在的问题。

按照 Bean 名称自动装配存在错误装配 JavaBean 的可能,如果配置文件中定义了与需要自动装配的 JavaBean 的名称相同而类型不同的 JavaBean ,则会错误的注入不同类型的 JavaBean。

2.若将配置文件中的 autowire=“byName” 改成 "byType" ,则可以实现按照 Bean 的类型自动装配 User 。实际配置文件中的代码如下:

  <bean autowire="byType"  id="testUtil" class="com.importnew.TestUtil"  />

3.关于 byName 与 byType 两者之间的区别:

  byName 通过 Bean 的名称自动装配。如果一个 Bean 的 name  和另外一个 Bean 的 name 相同,就自动装配。
  byType 通过 Bean 的属性类型自动自动装配。如果一个 Bean 的属性类型和另一个 Bean 的属性类型兼容,就自动装配。
4.无论是按照 Bean 的类型,还是按照 Bean 的名称,自动装配有时候都会出现无法自动装配的情况。例如在配置文件中再添加一个 User 类的实现对象,byType 自动装配类型会因为无法自动识别装配那一个JavaBean 而抛出 org.springframework.beans.factory.UnsatisfiedDependencyException 异常。要解决此类问题,只能通过混合使用手动装配来指定装配哪个 JavaBean 。

////end

posted @ 2016-10-17 12:46  Hosens  阅读(3065)  评论(0编辑  收藏  举报