依赖注入(Dependency Injection,DI)
PS:一定需要pojo中实现set才可以利用bean标签注入
构造器注入
也就是上面提到的创建对象的方式,分为无参构造和有参构造
无参构造
默认使用
有参构造
下标赋值
<!--通过下标赋值--> <bean> <constructor-arg index="0" value="Spring2"/> </bean>
类型复制
<!--通过类型赋值--> <bean> <constructor-arg type="java.lang.String" value="Spring3"/> </bean>
属性名赋值
<!--属性名赋值--> <bean> <constructor-arg value="Spring4"/> </bean>
set注入
依赖:bean对象的注入依赖于Spring容器
注入:bean对象的属性,由容器来注入
拓展注入
Student.java
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 String apache; private Properties info; public String getApache() { return apache; } public void setApache(String apache) { this.apache = apache; } 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 getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }beans.xml
<bean> <property value="beijing"/> </bean> <bean> <!-- 普通属性值注入--> <property value="zh1z3ven"/> <!-- ref 引用对象注入--> <property ref="address"/> <!-- 数组array注入--> <property> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!-- List注入--> <property> <list> <value>听音乐</value> <value>看电影</value> <value>敲代码</value> <value>写文章</value> </list> </property> <!-- Map注入--> <property> <map> <entry key="银行卡" value="1"></entry> <entry key="身份证" value="2"></entry> <entry key="学生证" value="3"></entry> <entry key="电话卡" value="4"></entry> <entry key="校园卡" value="5"></entry> </map> </property> <!-- Set注入--> <property> <set> <value>LOL</value> <value>CF</value> <value>qq</value> </set> </property> <!-- null注入--> <property> <null></null> </property> <!-- 空值注入--> <property value=""/> <!-- properties--> <property> <props> <prop key="id">10</prop> <prop key="city">北京</prop> </props> </property> </bean> P-namespcae&C-namespace
p命名空间注入,可以直接注入属性值,类似于bean标签中property-name-value
Beans.xml头部需导入
xmlns:p="http://www.springframework.org/schema/p" <bean p:name="zh1z3ven1" p:age="18"/>