Spring配置
别名
- 使用alias标签
<alias name="user" alias="123"/>
- 使用name属性
<bean id="student" class="com.practice.pojo.User" name="user"/>
import标签
import标签作用:可以将多个bean.xml文件合并成一个总的bean.xml文件。
依赖注入,对于数组、列表等参数赋值
- 相关类创建
//Address类创建
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
//student类创建
public class student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
'}';
}
}
//User类创建
public class User {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String friend;
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> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getFriend() {
return friend;
}
public void setFriend(String friend) {
this.friend = friend;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", friend='" + friend + '\'' +
", info=" + info +
'}';
}
}
- 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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.practice.pojo.Address" name="add" >
<property name="address" value="limtong"/>
</bean>
<alias name="user" alias="123"/>
<bean id="student" class="com.practice.pojo.User" name="user">
<property name="name" value="zhangsan"/>
<property name="address" ref="add"/>
<property name="books">
<array>
<value>最萌三千</value>
<value>萌宠无敌</value>
<value>二哈差价</value>
<value>啦啦啦哈</value>
</array>
</property>
<property name="card">
<map>
<entry key="银行卡" value="11111111" />
<entry key="hahaha" value="123456"/>
</map>
</property>
<property name="friend">
<null></null>
</property>
<property name="games">
<set>
<value>lol</value>
<value>qq</value>
<value>创越火线</value>
</set>
</property>
<property name="hobbys">
<list>
<value>吃</value>
<value>喝</value>
<value>睡</value>
</list>
</property>
<property name="info">
<props>
<prop key="root">123456</prop>
<prop key="word">111111</prop>
</props>
</property>
</bean>
<bean id="d" class="com.practice.pojo.student" p:name="zhangsan"/>
</beans>
拓展注入
- p命名
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="d" class="com.practice.pojo.student" p:name="zhangsan"/>
</beans>
- c命名
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="student" class="com.practice.pojo.Student" c:_0="zhangsan" c:_1="123456"></bean>
<bean id="student1" class="com.practice.pojo.Student" c:_0="zhang" c:_1="123456"></bean>
<bean id="student2" class="com.practice.pojo.Student" c:_0="san" c:_1="123456"></bean>
<bean id="teacher" class="com.practice.pojo.Teacher" c:_0="lisi" c:_1="456789"/>
<bean id="school" class="com.practice.pojo.School"/>
</beans>