Sring---注入方式

1.bean.xml  创建完成成为Spring的bean的配置文件,通过Spring容器类 加载配置文件 使用。

   注: 依赖注入:1.依赖:bean对象的创建依赖spring容器。   2.注入:bean对象中的所有属性,由容器来注入。

   注入方式:1.构造器注入:3种方式

      2.set注入:必须要有无参构造函数   和  set方法

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
};
package com.chen.pojo;

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

public class Student {


private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private Properties info;

public String getName() {
return name;
}

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

public Address getAddress() {
return address;
}

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

public String[] getBooks() {
return books;
}

public void setBooks(String[] books) {
this.books = books;
}

public List<String> getHobbys() {
return hobbys;
}

public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}

public Map<String, String> getCards() {
return cards;
}

public void setCards(Map<String, String> cards) {
this.cards = cards;
}

public Set<String> getGames() {
return games;
}

public void setGames(Set<String> games) {
this.games = games;
}

public Properties getInfo() {
return info;
}

public void setInfo(Properties info) {
this.info = info;
}

}
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="address" class="com.chen.pojo.Address">
<property name="address" value=""/>
</bean>
<bean id="student" class="com.chen.pojo.Student">
<!-- String name--> //普通属性注入
<property name="name" value="chen"/>
<!-- Address addres--> //对象类型注入
<property name="address" ref="address"/>
<!-- String[] books--> //数组注入
<property name="books">
<array value-type="java.lang.String">
<value>java数</value>
<value>java书2</value>
<value>java书3</value>
</array>
</property>
<!-- List<String> hobbys--> //list 结合注入
<property name="hobbys">      
<list value-type="java.lang.String">
<value>游泳</value>
<value>游泳</value>
<value>游泳</value>
</list>
</property>
<!-- Map<String,String> cards--> //Map注入
<property name="cards">
<map value-type="java.lang.String" key-type="java.lang.String">
<entry key="学生卡" value="123456"/>
<entry key="学生卡2" value="123fds456"/>
<entry key="学生卡3" value="1fsf23456"/>
</map>
</property>
<!-- Set<String> games--> //set注入
<property name="games">
<set value-type="java.lang.String">
<value>LOL</value>
<value>WOW</value>
</set>
</property>
<!-- String wife--> //Null注入 默认不赋值
<property name="wife">
<null/>
</property>
<!-- Properties info--> //properties 注入
<property name="info">
<props>
<prop key="name">chentao</prop>
<prop key="sex">男</prop>

</props>
</property>
</bean>


<bean id="hello" class="com.chen.pojo.Hello">

<property name="id" value="1"/>
<property name="name" value="lll"/>
</bean>

</beans>

 3.P: 和 C: 命名空间的用法需要在配置文件里引入头配置链接

   C(constructor 构造器的英文简写):命名空间需要 有参构造函数注入  但是必须要有无参构造函数

 P (property 属性的英文简写)  

  两种方式,一种针对构造器注入  一种针对普通和set注入 (不太)

原文地址:https://www.cnblogs.com/chencn/p/12331132.html