什么是依赖注入
- 依赖 : 指Bean对象的创立依赖于容器 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和拆卸 .
依赖注入的类型有三类
- 根本数据类型和String类型
- 其余bean类型(在配置文件中或者注解配置过的bean)
- 简单类型/汇合类型
注入的形式有三种
- 应用构造函数注入
构造函数形式注入:应用标签constructor-arg
- type:用于指定注入的数据类型
- index:用于指定注入的数据给构造函数中指定索引地位的参数赋值,索引地位是从0开始
- name:用于指定给构造函数中指定名称的参数赋值(举荐应用)
- value:用于提供根本类型和string类型数据赋值
- ref:用于指定其余的bean数据类型,是指在spring容器中呈现过的bean对象,援用id进来
测试:
实体类
public class User { private String name; private Integer age; private Date date; public User(){ } public User(String name, Integer age, Date date) { this.name = name; this.age = age; this.date = date; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; }}
Spring外围配置文件beans.xml
<bean id="user" class="com.Dao.User"> <constructor-arg name="name" value="chenhui"></constructor-arg> <constructor-arg name="age" value="22"></constructor-arg> <constructor-arg name="date" ref="date"></constructor-arg> </bean> <bean id="date" class="java.util.Date"></bean>
测试类:
@Test public void test(){ //通过ClassPathXmlApplicationContext对象加载配置文件形式将javabean对象交给spring来治理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,通过id和类字节码来获取 User user = Context.getBean("user", User.class); System.out.println(user); }
后果:
- 应用set办法注入(罕用)
- 应用标签property
- name:用于指定注入时所调用的set办法名称
- value:用于提供根本类型和string类型数据赋值
- ref:用于指定其余的bean数据类型,是指在spring容器中呈现过的bean对象,援用id进来
要求被注入的属性 , 必须有set办法 , set办法的办法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set办法 , 是 is .
测试:
实体类
public class User { private String name; private Integer age; private Date date; public User(){ } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setDate(Date date) { this.date = date; }}
Spring外围配置文件beans.xml
<!-- 应用的是默认构造函数创建对象--> <bean id="user" class="com.Dao.User"> <property name="name" value="chenhui"></property> <property name="age" value="22"></property> <property name="date" ref="date"></property> </bean> <bean id="date" class="java.util.Date"></bean>
测试类:
@Test public void test(){ //通过ClassPathXmlApplicationContext对象加载配置文件形式将javabean对象交给spring来治理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,通过id和类字节码来获取 User user = Context.getBean("user", User.class); System.out.println(user); }
后果:
简单类型/汇合类型的注入
- 用于给list构造汇合注入标签:
- list array set
- 用于给map构造汇合注入的标签:
- map property
- 构造雷同,标签能够调换,即标签体能够调换应用,不会报错
测试:
实体类:
public class User { private String[] strings; private List<String> list; private Map map; private Properties properties; private Set set; public void setStrings(String[] strings) { this.strings = strings; } public void setList(List<String> list) { this.list = list; } public void setMap(Map map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void setSet(Set set) { this.set = set; } public User(){ } public void print(){ System.out.println(Arrays.toString(strings)); System.out.println(list); System.out.println(map); System.out.println(set); System.out.println(properties); }}
Spring外围配置文件beans.xml
<bean id="user" class="com.Dao.User"> <property name="strings"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="list"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="set"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="map"> <map><!-- 应用第一种形式--> <entry key="chenhui" value="123"></entry> <entry key="xie" value="123"></entry><!-- 也可应用第二种形式--> <entry key="chen"> <value>333</value> </entry> </map> </property> <property name="properties"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop> <prop key="dsa">3</prop> </props> </property> </bean>
测试类:
@Test public void test(){ //通过ClassPathXmlApplicationContext对象加载配置文件形式将javabean对象交给spring来治理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,通过id和类字节码来获取 User user = Context.getBean("user", User.class); //调用办法使汇合类型注入执行 user.print(); }
后果:
当构造雷同,标签能够调换
<bean id="user" class="com.Dao.User"> <property name="strings"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="list"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="set"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="map"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop> <prop key="dsa">3</prop> </props> </property> <property name="properties"> <map> <!-- 应用第一种形式--> <entry key="chenhui" value="123"></entry> <entry key="xie" value="123"></entry> <!-- 也可应用第二种形式--> <entry key="chen"> <value>333</value> </entry> </map> </property> </bean>
后果:
[图片上传中...(image-85fd26-1613912046687-0)]
最初
欢送关注公众号:前程有光,支付一线大厂Java面试题总结+各知识点学习思维导+一份300页pdf文档的Java外围知识点总结! 这些材料的内容都是面试时面试官必问的知识点,篇章包含了很多知识点,其中包含了有基础知识、Java汇合、JVM、多线程并发、spring原理、微服务、Netty 与RPC 、Kafka、日记、设计模式、Java算法、数据库、Zookeeper、分布式缓存、数据结构等等。