码迷,mamicode.com
首页 > 编程语言 > 详细

spring属性依赖注入

时间:2015-09-02 20:31:36      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

一、构造方法方式注入

1、项目结构如下:

 技术分享

2、新建Customer类

技术分享
package hjp.spring.attributeinject;

public class Customer {
    private String name;
    private Integer age;
    private String city;

    public Customer() {

    }

    public Customer(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Customer(Integer age, String city) {
        this.age = age;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", city=" + city + "]";
    }
}
Cusomer

3、新建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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 如果没有配置 constructor-arg节点,则使用无参构造器-->
    <bean id="customerId" class="hjp.spring.attributeinject.Customer">
    <!--constructor-arg 配置构造参数
        index 表示参数索引号
        type 设置参数数据类型
        value 设置普通数据
        ref 设置引用数据
        
        如果只使用index和value,而不指定数据类型,则默认匹配符合条件的第一个构造函数
        如果配置了type,那么索引处的数据类型要对应正确
      -->
    <constructor-arg index="0" value="23" type="java.lang.Integer"></constructor-arg>
    <constructor-arg index="1" value="Tom" type="java.lang.String"></constructor-arg>
    </bean>
</beans>

4、新建测试类

package hjp.spring.attributeinject;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {
    @Test
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "hjp/spring/attributeinject/beans.xml");
        Customer customer = applicationContext.getBean("customerId", Customer.class);
        System.out.println(customer);
    }
}

二、setter方法注入

1、新增类Contact

技术分享
package hjp.spring.attributeinject;

public class Contact {
    private String address;
    private String telphone;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelphone() {
        return telphone;
    }

    public void setTelphone(String telphone) {
        this.telphone = telphone;
    }

    @Override
    public String toString() {
        return "Contact [address=" + address + ", telphone=" + telphone + "]";
    }
}
Contact

2、为Customer类新增属性contact,新增构造函数

public Customer(String name,Integer age,Contact contact){
this.name=name;
this.age=age;
this.contact=contact;
}

技术分享
package hjp.spring.attributeinject;

public class Customer {
    private String name;
    private Integer age;
    private String city;
    
    private Contact contact;

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Customer() {

    }

    public Customer(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Customer(Integer age, String city) {
        this.age = age;
        this.city = city;
    }
    
    public Customer(String name,Integer age,Contact contact){
        this.name=name;
        this.age=age;
        this.contact=contact;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", city=" + city + ", contact=" + contact + "]";
    }
}
Customer

3、更改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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="customerId" class="hjp.spring.attributeinject.Customer">
    <property name="name" value="Tom"></property>
    <property name="age" value="23"></property>
    <property name="contact" ref="contactId"></property>
    </bean>
    <bean id="contactId" class="hjp.spring.attributeinject.Contact">
    <property name="address" value="北京"></property>
    <property name="telphone" value="12345678"></property>
    </bean>
</beans>

4、测试类不变

spring属性依赖注入

标签:

原文地址:http://www.cnblogs.com/hujiapeng/p/4779164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!