IOC全称是Inversion of Control(控制反转),所谓控制反转是对于程序自己而言,之前咱们在建立对象的时候是本身经过new建立出来,而如今咱们把这个权力交给了Spring去作,让它帮咱们去建立对象和管理对象,对对象的控制权反转给了Springjava
DI全称是Dependency Injection(依赖注入),所谓依赖注入是对于Spring的IOC容器而言,之前咱们在给对象的属性赋值时有两种渠道,一种是经过带参数的构造方法,另外一种是经过set方法。而如今IOC容器在建立对象的同时把属性的值注入进去。
什么是依赖:好比A对象的属性里有B对象,那么就叫A依赖Bspring
依赖注入有两种方式,一种是基于对象的构造函数,另外一种是基于对象属性对象的set方法,基本实现思路是这样的:dom
/* User.java */ public class User { private String account; private String password; private Float balance; public User(String account, String password, Float balance) { this.account = account; this.password = password; this.balance = balance; } public User(){ System.out.println("User对象建立了"); } @Override public String toString() { return "User{" + "account='" + account + ''' + ", password='" + password + ''' + ", balance=" + balance + '}'; } public String getAccount() { System.out.println("set方法调用了"); return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Float getBalance() { return balance; } public void setBalance(Float balance) { this.balance = balance; } }
/* main函数 */ public class TestMain { public static void main(String[] args) { System.out.println(userDao.selectUserByAccount("2018")); AbstractApplicationContext factory = new ClassPathXmlApplicationContext("ApplicationContext.xml"); User user = (User) factory.getBean("user"); System.out.println(user); } }
<!-- 配置文件--> <?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="user" class="com.cjh.domain.User"> <property name="account" value="2019"></property> <property name="password" value="123"></property> <property name="balance" value="777"></property> </bean> </beans>
若是是基本数据类型/String的话,直接经过value写入要传递的值,底层会根据属性的具体类型将value转换成对应的数据类型ide
注入聚合对象函数
第一种写法this
/* person.java */ public class Person { private String name; //引入了上面的user对象 private User user; public Person(){} @Override public String toString() { return "Person{" + "name='" + name + ''' + ", user=" + user + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
//主函数 public class TestMain { public static void main(String[] args) { System.out.println(userDao.selectUserByAccount("2018")); ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } }
<!-- 配置文件 --> <?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="person" class="com.cjh.domain.Person"> <property name="name" value="john"></property> <property name="user"> <bean id="user" class="com.cjh.domain.User"> <property name="account" value="2019"></property> <property name="password" value="123"></property> <property name="balance" value="777"></property> </bean> </property> </bean> </beans>
第二种写法code
<!-- 配置文件 --> <?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="person" class="com.cjh.domain.Person"> <property name="name" value="john"></property> <property name="user" ref="user"></property> </bean> <bean id="user" class="com.cjh.domain.User"> <property name="account" value="2019"></property> <property name="password" value="123"></property> <property name="balance" value="777"></property> </bean> </beans>
注入集合xml
//TestCollection.java package com.cjh.domain; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class TestCollection { private List<String> list; private Set<String> set; private Map<Integer, String> map; private Properties properties; public TestCollection(){} @Override public String toString() { return "TestCollection{" + "list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<Integer, String> getMap() { return map; } public void setMap(Map<Integer, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
//主函数 public class TestMain { public static void main(String[] args) { System.out.println(userDao.selectUserByAccount("2018")); ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); TestCollection collection = (TestCollection) context.getBean("testCollection"); System.out.println(collection); } }
<!-- 配置信息 --> <?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="testCollection" class="com.cjh.domain.TestCollection"> <property name="list"> <list value-type="java.lang.String"> <value>12</value> <value>13</value> <value>14</value> <value>15</value> </list> </property> <property name="set"> <set value-type="java.lang.String"> <value>abc</value> <value>def</value> <value>ghi</value> <value>jkl</value> <value>mno</value> </set> </property> <property name="map"> <map key-type="java.lang.Integer" value-type="java.lang.String"> <entry key="1" value="a"/> <entry key="2" value="b"/> <entry key="3" value="c"/> </map> </property> <property name="properties"> <props value-type="String"> <prop key="a">1</prop> <prop key="b">2</prop> <prop key="c">3</prop> </props> </property> </bean> </beans>
<bean id="person" class="com.cjh.domain.Person"> <property name="name" value=""></property> <property name="user"> <null></null> </property></bean>
如上的配置中,value什么都不给就默认为""空串,若是须要注入null,须要使用null标签,若是直接用value="null",Spring会把它解析成字符串null对象
byName:在获取当前bean对象的时候,会根据当前对象的属性名,在配置文件下找到id/name属性与之同样的bean对象,装配给当前bean对象(经过属性的set方法)接口
byType:在获取当前bean对象的时候,根据当前对象属性的类型,在配置文件下找到相同类型的bean对象,并装配给当前bean对象(经过属性的set方法)
若是当前bean对象的属性类型是接口、抽象类或者普通类的父类,有三种状况
constructor:根据带参的构造方法进行装配
根据构造方法的参数类型和bean标签的class类型是否一致