alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<!--设置别名:在获取Bean的时候可以使用别名获取--> <alias name="userT" alias="userNew"/>团队的合作通过import来实现 ,可以引用多个beans.xml
<import resource="{path}/beans.xml"/> 我们在之前的案例已经讲过了
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is 。
实体类 public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public void setName(String name) { this.name = name; } public void setAddress(Address address) { this.address = address; } public void setBooks(String[] books) { this.books = books; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public void setCard(Map<String, String> card) { this.card = card; } public void setGames(Set<String> games) { this.games = games; } public void setWife(String wife) { this.wife = wife; } public void setInfo(Properties info) { this.info = info; } public void show(){ System.out.println("name="+ name + ",address="+ address.getAddress() + ",books=" ); for (String book:books){ System.out.print("<<"+book+">>\t"); } System.out.println("\n爱好:"+hobbys); System.out.println("card:"+card); System.out.println("games:"+games); System.out.println("wife:"+wife); System.out.println("info:"+info); } }配置文件 beans.xml
常量注入 <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="岳小帅"/> </bean> Bean注入 <bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="西安"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="岳小帅"/> <property name="address" ref="addr"/> </bean> 数组注入 <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> <property name="books"> <array> <value>西游记</value> <value>红楼梦</value> <value>水浒传</value> </array> </property> </bean> List注入 <property name="hobbys"> <list> <value>唱歌</value> <value>看美女</value> <value>玩游戏</value> </list> </property> Map注入 <property name="card"> <map> <entry key="中国邮政" value="456456456465456"/> <entry key="建设" value="1456682255511"/> </map> </property> set注入 <property name="games"> <set> <value>LOL</value> <value>CF</value> <value>DNF</value> </set> </property> Null注入 <property name="wife"><null/></property> Properties注入 <property name="info"> <props> <prop key="学号">20000301</prop> <prop key="性别">男</prop> <prop key="姓名">岳小帅</prop> </props> </property>注意:p是无参构造注入
c 命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:c="http://www.springframework.org/schema/c" <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法--> <bean id="user" class="com.kuang.pojo.User" c:name="狂神" c:age="18"/>发现问题:爆红了,刚才我们没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!