IoC
概念
IoC(控制反转,Inversion of Control),是面向对象编程中的一种设计原则,可以用来降低计算机代码之间的耦合度。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它;也可以说,依赖被注入到对象中。
属性注入
set方法注入属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.joe;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Book { private String name; private String author;
public void setAuthor(String author) { this.author = author; }
public void setName(String name) { this.name = name; } }
|
1 2 3 4 5 6 7 8 9
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="book" class="com.joe.Book"> <property name="name" value="SE"/> <property name="author" value="Zhang Haifan"/> </bean> </beans>
|
带参构造器注入属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.joe;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Book { private String name; private String author;
public Book(String name, String author) { this.name = name; this.author = author; } }
|
1 2 3 4 5 6 7 8 9
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="book" class="com.joe.Book"> <constructor-arg name="author" value="Zhang Haifan"/> <constructor-arg name="name" value="SE"/> </bean> </beans>
|
注入外部Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.joe.service;
import com.joe.dao.UserDAO;
public class UserService { private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } }
|
1 2 3 4 5 6 7 8 9
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userService" class="com.joe.service.UserService"> <property name="userDAO" ref="userDAO"/> </bean> <bean id="userDAO" class="com.joe.dao.UserDAO"/> </beans>
|
注入内部Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.joe;
public class Employment { private String name; private String gender; private Department department;
public void setName(String name) { this.name = name; }
public void setGender(String gender) { this.gender = gender; }
public void setDepartment(Department department) { this.department = department; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="employment" class="com.joe.Employment"> <property name="name" value="lucy"/> <property name="gender" value="女"/> <property name="department"> <bean class="com.joe.Department"> <property name="name" value="安保部"/> </bean> </property> </bean> </beans>
|
注入集合属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.joe;
import java.util.List; import java.util.Map; import java.util.Set;
public class Student { private String[] courses; private List<String> list; private Map<String,String> map; private Set<String> set;
public void setCourses(String[] courses) { this.courses = courses; }
public void setList(List<String> list) { this.list = list; }
public void setMap(Map<String, String> map) { this.map = map; }
public void setSet(Set<String> set) { this.set = set; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.joe.Student"> <property name="courses"> <array> <value>数据库</value> <value>软件工程</value> </array> </property> <property name="list"> <list> <value>张三</value> <value>小三</value> </list> </property> <property name="map"> <map> <entry key="数据库" value="100"/> <entry key="软件工程" value="10"/> </map> </property> <property name="set"> <set> <value>岂可修</value> <value>可恶</value> </set> </property> </bean> </beans>
|