Spring DI依赖注入
Spring DI依赖注入
1.构造器注入:
<bean id="user" class="cn.pinked.pojo.User"> <!--无参构造 <property name="name" value="张三"/>--> <!--有参下标赋值 <constructor-arg index="0" value="张三"/>--> <!--有参类型赋值 <constructor-arg type="java.lang.String" value="张三"/>--> <!--有参通过参数名赋值 <constructor-arg name="name" value="张三"/>--> </bean>
2.set注入:
先新建两个类
Address:
package com.xiaofu.pojo; public class Adderss { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Student:
package com.xiaofu.pojo; import java.util.*; public class Student { private String name; private Adderss adderss; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Adderss getAdderss() { return adderss; } public void setAdderss(Adderss adderss) { this.adderss = adderss; } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", adderss=" + adderss + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
然后在容器中完成 所有属性的注入:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.xiaofu.pojo.Adderss"/> <bean id="text" class="com.xiaofu.pojo.Student"> <!-- 第一种,普通注入--> <property name="name" value="xiaofu"/> <!-- 第二种,Bean注入 ref--> <property name="adderss" ref="address"/> <!-- 数组注入 --> <property name="books"> <array> <value>西游记</value> <value>三国演义</value> <value>水浒传</value> <value>红楼梦</value> </array> </property> <!-- List注入--> <property name="hobbys"> <list> <value>听歌</value> <value>看电影</value> <value>喝奶茶</value> </list> </property> <!-- Map注入--> <property name="card"> <map> <entry key="身份证" value="6788939282"/> <entry key="银行卡" value="909039282"/> </map> </property> <!-- set注入--> <property name="games"> <set> <value>LoL</value> <value>LPL</value> </set> </property> <!-- null注入--> <property name="wife" > <null/> </property> <!-- Properties注入--> <property name="info"> <props> <prop key="学号">0922</prop> <prop key="性别">男</prop> <prop key="年龄">20</prop> </props> </property> </bean> </beans>
最后编写测试类 测试一下:
import com.xiaofu.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Text { public static void main(String[] args) { //获取spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //我们的对象现在都在spring容器中管理了,我们要使用,直接去里面取出来就可以了! Student text = (Student) context.getBean("text"); System.out.println(text.toString()); } }
运行程序:
所有属性都完成了注入。
3.p命名空间和c命名空间注入:
p命名空间是set注入的一种快捷实现方式,想要使用p命名空间注入,需要注意一下几点。
1. 实体类中必须有set方法;
2. 实体类中必须有无参构造器(默认存在);
3. 必须导入p命名空间注入方式依赖。
xml依赖代码:
xmlns:p="http://www.springframework.org/schema/p"
导入后即可使用:
<bean id="user" class="com.yd.pojo.User" p:age="18" p:name="老王"/>
c命名空间是构造器注入的一种快捷实现方式,想要使用c命名空间,需要注意一下几点。
1. 实体类中必须存在有参构造器;
2. 必须导入c命名空间注入方式依赖。
xml依赖代码:
xmlns:c="http://www.springframework.org/schema/c"
导入后即可使用:
<bean id="user2" class="com.yd.pojo.User" c:age="23" c:name="中王"/>