别名(alias)
见字如意,就是给bean对象起一个别名,在代码中可用别名获取该对象
用法 <alias name="原名" alias="取成的别名"/> 或
<bean id="bean对象ID" class="类名" name="别名1,别名2,...,别名n">
其中第二种别名取法,之间可用其它符号代替逗号分隔,如别名1;别名2
<alias name="people" alias="xiaozhou"/>
<bean id="people" class="com.qian.test1.People" name="zhouzhou,KOGmaw"/>
导入(import)
可以将多个配置文件导入到一个文件中
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
依赖注入(DI)
bean.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="people" class="com.qian.test1.People"/>
<bean id="test" class="com.qian.test1.DItest">
<!--值注入-->
<property name="str" value="str"></property>
<!--bean注入,使用ref-->
<property name="people" ref="people"></property>
<!--给属性赋null值-->
<property name="nullstr">
<null></null>
</property>
<!--数组注入,使用array-->
<property name="strs">
<array>
<value>str1</value>
<value>str2</value>
</array>
</property>
<!--List注入,使用list-->
<property name="strlist">
<list>
<value>strlist1</value>
<value>strlist2</value>
</list>
</property>
<!--Set注入,使用set-->
<property name="pset">
<set>
<!--因为注入的是People对象,所以我这也用了bean注入-->
<ref bean="people"></ref>
</set>
</property>
<!--Map注入,使用map+entry-->
<property name="ismap">
<map>
<entry key="22" value="strmap1"></entry>
<entry key="222" value="strmap2"></entry>
</map>
</property>
<!--property注入,props+prop-->
<property name="properties">
<props>
<prop key="author">Medivhm</prop>
</props>
</property>
</bean>
</beans>
People类:
package com.qian.test1;
public class People {
private String name;
private int age;
private String address;
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
DItest类:
package com.qian.test1;
import java.util.*;
public class DItest {
private String str;
private People people;
private String nullstr;
private String[] strs;
private List<String> strlist;
private Set<People> pset;
private Map<Integer,String> ismap;
private Properties properties;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public String getNullstr() {
return nullstr;
}
public void setNullstr(String nullstr) {
this.nullstr = nullstr;
}
public String[] getStrs() { return strs; }
public void setStrs(String[] strs) {
this.strs = strs;
}
public List<String> getStrlist() {
return strlist;
}
public void setStrlist(List<String> strlist) { this.strlist = strlist; }
public Set<People> getPset() {
return pset;
}
public void setPset(Set<People> pset) {
this.pset = pset;
}
public Map<Integer, String> getIsmap() {
return ismap;
}
public void setIsmap(Map<Integer, String> ismap) {
this.ismap = ismap;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
@Override
public String toString() {
return "DItest{" +
"str='" + str + '\'' +
", people=" + people +
", nullstr='" + nullstr + '\'' +
", strs=" + Arrays.toString(strs) +
", strlist=" + strlist +
", pset=" + pset +
", ismap=" + ismap +
", properties=" + properties +
'}';
}
}
测试类:
import com.qian.test1.DItest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
DItest test=(DItest)context.getBean("test");
System.out.println(test.toString());
}
}
运行结果:
DItest{str='str', people=com.qian.test1.People@769f71a9, nullstr='null', strs=[str1, str2], strlist=[strlist1, strlist2], pset=[com.qian.test1.People@769f71a9], ismap={22=strmap1, 222=strmap2}, properties={author=Medivhm}}
具体内容可以去Spring官网文档上查阅---1.4.2. Dependencies and Configuration int Detail