通过spring表达式可以在配置 Y
节点时,如果Y的某些属性需要注入值,可以是已经配置的好的 X
类的节点中的值
直接使用 #{id.属性名}
set
方法 新建两个类
Message
public class Message{
private String name;
private List<String> cities; //城市 。List集合
private Set<String> friend; //Set集合
private Map<Integer,String> bookes; //Map集合
private Properties properties; //Properties集合
private String[] names;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String[] getNames() {
return names;
}
public void setNames(String[] names){
this.names = names;
}
public List<String> getCities(){
return cities;
}
public void setCities(List<String> cities){
this.cities = cities;
}
public Set<String> getFriend(){
return friend;
}
public void setFriend(Set<String> friend){
this.friend = friend;
}
public Map<Integer, String> getBookes(){
return bookes;
}
public void setBookes(Map<Integer, String> bookes){
this.bookes = bookes;
}
public Properties getProperties(){
return properties;
}
public void setProperties(Properties properties){
this.properties = properties;
}
}
public class ValueBean{
private String username;
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
}
<util:listid="cities">
<value>徐州</value>
<value>无锡</value>
<value>常州</value>
</util:list>
<util:setid="friends">
<value>Jack</value>
<value>Tom</value>
<value>陈加兵</value>
</util:set>
<util:mapid="bookes">
<entrykey="1001"value="java编程基础"></entry>
<entrykey="1002"value="java编程思想"></entry>
</util:map>
<!-- 引入外部的Properties文件,location指定的就是位置 -->
<util:propertiesid="properties"location="classpath:jdbc.properties"></util:properties>
<beanid="message"class="cn.tedu.spring.beans.Message">
<propertyname="name"value="陈加兵"></property>
<!-- List集合的注入 ref指定的上面定义的List的id -->
<propertyname="cities"ref="cities"></property>
<!-- Set集合的注入 -->
<propertyname="friend"ref="friends"></property>
<!-- Map集合的注入 -->
<propertyname="bookes"ref="bookes"></property>
<!-- properties的集合的注入 -->
<propertyname="properties"ref="properties"></property>
<!-- 为数组赋值 -->
<propertyname="names">
<array>
<value>Alex</value>
<value>Billy</value>
</array>
</property>
</bean>
<!-- 配置ValueBean -->
<beanid="valueBean"class="cn.tedu.spring.beans.ValueBean">
<!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
<propertyname="username"value="#{message.name}"></property>
</bean>
#{bean的id.数组名[index]}
<!-- 配置ValueBean -->
<beanid="valueBean"class="cn.tedu.spring.beans.ValueBean">
<!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
<propertyname="username"value="#{message.names[0]}"></property>
</bean>
#{person.address.city}
Person类中有一个成员变量是Address类的对象
<!-- 创建一个Address的实例 -->
<beanid="address"class="cn.tedu.spring.beans.Address">
<propertyname="city"value="无锡"></property>
<propertyname="pro"value="江苏"></property>
</bean>
<beanid="person"class="cn.tedu.spring.beans.Person">
<propertyname="name"value="陈加兵"></property>
<propertyname="age"value="22"></property>
<!-- 引用上面address -->
<propertyname="address"ref="address"></property>
</bean>
<!-- 配置ValueBean -->
<beanid="valueBean"class="cn.tedu.spring.beans.ValueBean">
<!-- value的值是使用spring表达式获取的,形式为:#{前面定义好的id.属性名} -->
<propertyname="username"value="#{person.address.city}"></property>
</bean>
#{id.Map名称.key名称}
#{id.Map名称['key名称']}
<bean id="" class="" autowire="">
<property>
节点来注入,spring会自动的为属性注入值
在 bean
节点中添加 autowire
属性以配置自动装配,当值为 byName
,表示根据名称自动装配,即spring会检查这个bean的所有属性名称,然后在搜平日那个管理的所有Bean中查找bean-id一致的Bean对象,如果找到,则自动赋值
当取值为 byType
时,表示根据类型自动装配,及自动化赋值的标准是找数据类型匹配的Bean对象,需要注意的是:如果根据类型装配,必须保证可以匹配上的,由spring自动管理的Bean只有一个,如果有2个或者更多,会导致异常
public class UserDao(){
public void reg(){
}
}
public class UserService{
private UserDao userDao;
private void reg(){
userDao.reg();
}
}
<beanid="userDao"class="cn.tedu.spring.UserDao"/> <!--这里不需要配置property节点来ref上面定义的bean,只需要使用自动装配即可--> <beanid="userService"class="cn.tedu.spring.UserService"autowire="byName">