spring属性注入

Posted song.yan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring属性注入相关的知识,希望对你有一定的参考价值。

1,set方法注入

(1)对于值类型的属性:

在对象中一定要有set方法

package com.songyan.demo1;

import com.songyan.injection.Car;



/**
 * 要创建的对象类
 * @author sy
 *
 */
public class User {
private String name;
private int age ;/**
 * 重写toString方法在输出对象时将属性值输出
 */
@Override
    public String toString() {
        
        return "name:"+this.name+",age:"+this.age;
    }
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}

 

在配置文件中进行配置

<!--在user对象中为属性名为name的属性赋值为“tom”  -->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>

 测试

 

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
    User user=(User)applicationContext.getBean("user");
    System.out.println(user);

(2)对于引用类型的属性

package com.songyan.injection;

public class Car {
    private String name;
    private String color;
    @Override
    public String toString() {
        
        return "【carname:"+this.name+",carcolor:"+this.color+"】";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    

}
package com.songyan.demo1;

import com.songyan.injection.Car;



/**
 * 要创建的对象类
 * @author sy
 *
 */
public class User {
private String name;
private int age ;
private Car car;



public Car getCar() {
    return car;
}
public void setCar(Car car) {
    this.car = car;
}
/**
 * 重写toString方法在输出对象时将属性值输出
 */
@Override
    public String toString() {
        
        return "name:"+this.name+",age:"+this.age+",car:"+this.car;
    }
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}
package com.songyan.injection;

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

import com.songyan.demo1.User;

public class Client {
public static void main(String[] args) {
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
    User user=(User)applicationContext.getBean("user");
    System.out.println(user);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    <!-- 引用类型的属性注入需要先将属性配置 -->
    <bean name="car" class="com.songyan.injection.Car">
        <property name="name" value="宝马"></property>
        <property name="color" value="red"></property>
    </bean>
    <!--set 方法注入值类型 -->
    <bean name="user" class="com.songyan.demo1.User">
        <!--在user对象中为属性名为name的属性赋值为“tom” -->
        <property name="name" value="tom"></property>
        <property name="age" value="18"></property>
        <!--引用类型的赋值使用ref属性 -->
        <property name="car" ref="car"></property>
    </bean>

</beans>

 

2,构造方法注入

 

package com.songyan.injection;

public class Car {
    private String name;
    private String color;

    // 通过构造函数对属性赋值
    public Car(String name, String color) {
        super();
        this.name = name;
        this.color = color;
    }

    public Car() {

    }

    @Override
    public String toString() {

        return "【carname:" + this.name + ",carcolor:" + this.color + "】";
    }

}
package com.songyan.demo1;

import com.songyan.injection.Car;

/**
 * 要创建的对象类
 * @author sy
 *
 */
public class User {
private String name;
private int age ;
private Car car;

/**
 * 通过构造函数对属性赋值
 * @param name
 * @param age
 * @param car
 */
public User(String name, int age, Car car) {
    super();
    this.name = name;
    this.age = age;
    this.car = car;
}
public User()
{
    
}


/**
 * 重写toString方法在输出对象时将属性值输出
 */
@Override
    public String toString() {
        
        return "name:"+this.name+",age:"+this.age+",car:"+this.car;
    }


}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    <!-- 引用类型的属性注入需要先将属性配置 -->
    <bean name="car" class="com.songyan.injection.Car">
        <constructor-arg name="name" value="baoma"></constructor-arg>
        <constructor-arg name="color" value="red"></constructor-arg>
    </bean>
    <!--构造方法注入值类型 -->
    <bean name="user" class="com.songyan.demo1.User">
        <!--在user对象中为属性名为name的属性赋值为“tom” -->
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="car" ref="car"></constructor-arg>
        
    </bean>

</beans>
package com.songyan.injection;

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

import com.songyan.demo1.User;

public class Client {
public static void main(String[] args) {
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/injection/beans5.xml");
    User user=(User)applicationContext.getBean("user");
    System.out.println(user);
}
}

3,p名称空间注入

 

4,spel注入

 

5,复杂类型注入

package com.songyan.injection;

public class Car {
    private String name;
    private String color;

    // 通过构造函数对属性赋值
    public Car(String name, String color) {
        super();
        this.name = name;
        this.color = color;
    }

    public Car() {

    }

    @Override
    public String toString() {

        return "【carname:" + this.name + ",carcolor:" + this.color + "】";
    }

}
package com.songyan.complex;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;



public class Complex {
    private Object[] obj;//数组类型注入
    private List list;//list类型注入
    private Map map;//map类型注入
    private Properties properties;//文件类型注入
    
    @Override
    public String toString() {
        
        return "array:"+Arrays.toString(obj)+"    \\nlist:"+this.list+"  \\nproperties:"+this.properties+"\\nmap:"+this.map;
    }
    public Object[] getObj() {
        return obj;
    }
    public void setObj(Object[] obj) {
        this.obj = obj;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public Map getMap() {
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    
    
    

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    <!-- 引用类型的属性注入需要先将属性配置 -->
    <bean name="car" class="com.songyan.injection.Car">
        <constructor-arg name="name" value="baoma"></constructor-arg>
        <constructor-arg name="color" value="red"></constructor-arg>
    </bean>
    
    
    <bean name="complex" class="com.songyan.complex.Complex">
    <!--数组类型-->
        <!--数组中只有一个值类型  -->
        <!-- <property name="obj" value="tom"></property> -->
        <!--数组中只有一个引用类型  -->
        <!-- <property name="obj" ref="car"></property> -->
        <!--数组中有多个值  -->
         <property name="obj" >
             <array>
                 <value>tom</value>
                 <value>jack</value>
                 <ref bean="car" />
             </array>
         </property>
    <!--map类型  -->
    <property name="map">
        <map>
            <entry key="name" value="zhangsan"></entry>
            <entry key="car" value-ref="car"></entry>
        </map>    
    </property>
    <!--list类型  -->
        <!--一个值  -->
        <!-- <property name="list" value="jack"></property> -->
        <!--多个值  -->
        <property name="list">
            <list>
                <value>jack1</value>
                <value>jack2</value>
                <ref bean="car"/>
            </list>
        </property>
        
    <!--properties  -->
    <property name="properties">
        <props>
            <prop key="name">user</prop>
            <prop key="pass">123</prop>
            <prop key="id">192.168.1.1</prop>
        </props>
    </property>
    </bean>
    

</beans>
package com.songyan.complex;

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

import com.songyan.demo1.User;

public class Cliect {
public static void main(String[] args) {
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/complex/beans.xml");
    Complex complex=(Complex)applicationContext.getBean("complex");
    System.out.println(complex);
}
}

 

以上是关于spring属性注入的主要内容,如果未能解决你的问题,请参考以下文章

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

spring autowired怎么按照name注入

Spring依赖注入的方式

Spring配置文件中注入复杂类型属性

Spring根据XML配置文件注入对象类型属性

spring使用注解,自动扫描类的时候怎么注入