1.bean.xml 创建完成成为Spring的一个bean,创建完成,然后注入使用:
注: 依赖注入:1.依赖:bean对象的创建依赖spring容器。 2.注入:bean对象中的所有属性,由容器来注入。
注入方式:1.构造器注入:
2.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-->
<property name="hobbys">
<list value-type="java.lang.String">
<value>游泳</value>
<value>游泳</value>
<value>游泳</value>
</list>
</property>
<!-- Map<String,String> cards-->
<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-->
<property name="games">
<set value-type="java.lang.String">
<value>LOL</value>
<value>WOW</value>
</set>
</property>
<!-- String wife-->
<property name="wife">
<null/>
</property>
<!-- Properties info-->
<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:命名空间需要 有参构造函数注入