SpringIOC 依赖注入的方式
手动注入
- set 方式注入
- 构造器注入
- 静态工厂注入
- 实例化工厂方式注入
1.set方式注入
需求: 想要在 UserService 这个类中 使用UserDao 的方法
定义UserService.java
public class UserService {
/**
* SpringIOC 容器注入方式:
* set方式注入
* 1) 在需要注入的类中加入setter方法
* 2) 在配置文件中加入property 属性,ref依赖 被注入的对象
*/
/**
* java bean注入
*/
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 基本类型注入
*/
private String ip;
public void setIp(String ip) {
this.ip = ip;
}
/**
* 注入list 类型
*/
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
private Set<String> set;
public void setSet(Set<String> set) {
this.set = set;
}
private Map<String, Object> maps;
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void printList() {
list.stream().forEach(e -> {
e = e.toUpperCase();
System.out.println(e);
});
}
/**
* set
*/
public void printSet() {
set.stream().forEach(e -> {
e = e.toUpperCase();
System.out.println(e);
});
}
public void printMap() {
maps.forEach((k, v) -> System.out.println(k + "=" + v));
}
public void printProperties() {
properties.forEach((k, v) -> System.out.println(k + "=" + v));
}
public void test() {
userDao.method01(ip);
printList();
printSet();
printMap();
printProperties();
}
}
定义 UserDao.java
public class UserDao {
public void method01(String ip) {
System.out.println("UserDao test... " + ip);
}
}
定义Spring.xml
<?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">
<!-- SpringIOC 依赖注入 -->
<!-- set方式注入-->
<bean id="userService" class="org.wlc.service.UserService">
<property name="userDao" ref="userDao"></property>
<property name="ip" value="192.168.83.129"></property>
<!-- 注入list 属性-->
<property name="list">
<list>
<value>上海</value>
<value>天津</value>
<value>a</value>
<value>b</value>
</list>
</property>
<!-- 注入set 属性-->
<property name="set">
<set>
<value>上海</value>
<value>天津</value>
<value>a</value>
<value>b</value>
</set>
</property>
<!-- 注入map属性-->
<property name="maps">
<map>
<entry>
<key>
<value>周杰伦</value>
</key>
<value>稻香</value>
</entry>
<entry>
<key>
<value>张杰</value>
</key>
<value>少年</value>
</entry>
</map>
</property>
<!-- properties属性配置文件-->
<property name="properties">
<props>
<prop key="bj">北京</prop>
<prop key="sh">上海</prop>
</props>
</property>
</bean>
<bean id="userDao" class="org.wlc.dao.UserDao"></bean>
</beans>
测试
public class App {
private static ApplicationContext context ;
static {
context = new ClassPathXmlApplicationContext("Spring.xml");
}
public static void main(String[] args) {
method02();
}
/**
* 手动注入: set方式注入
*/
public static void method02(){
UserService userService = (UserService) context.getBean("userService");
userService.test();
}
/**
*
* 手动注入: 构造器注入方式注入
*/
public void method01(){
UserService2 userService = (UserService2) context.getBean("userService2");
userService.test();
UserDao2 userDao2 = (UserDao2) context.getBean("userDao2");
userDao2.method01();
}
}
2.构造器注入
需求是: 在 UserService3这个Java类中,需要使用UserDao3.java这个对象,
所以需要注入进UserService3这个类中
在spring2.xml配置文件中进行配置
<bean id="userService3" class="org.wlc.service.UserService3">
<constructor-arg name="userDao333" ref="userDao3"></constructor-arg>
</bean>
<bean id="userDao3" class="org.wlc.dao.UserDao3"></bean>
定义 UserService3.java
public class UserService3 {
/**
* java bean注入
* 构造器注入
*/
private UserDao3 userDao333;
public UserService3(UserDao3 userDao333) {
this.userDao333 = userDao333;
}
public void test() {
System.out.println("UserService3 test...");
}
}
定义UserDao3.java
public class UserDao3 {
public void method01() {
System.out.println("UserDao2, ip地址为: -》");
}
}
定义Spring2.xml 配置文件
<?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">
<!-- SpringIOC 依赖注入 -->
<!-- 构造器注入 -->
<bean id="userService3" class="org.wlc.service.UserService3">
<constructor-arg name="userDao333" ref="userDao3"></constructor-arg>
</bean>
<bean id="userDao3" class="org.wlc.dao.UserDao3"></bean>
</beans>
测试
public class App {
private static ApplicationContext context ;
static {
context = new ClassPathXmlApplicationContext("Spring2.xml");
}
public static void main(String[] args) {
method01();
}
/**
*
* 手动注入: 构造器注入方式注入
*/
public static void method01(){
UserService3 userService3 = (UserService3) context.getBean("userService3");
userService3.test();
}
}
2.1 构造器注入 有可能会出现循环依赖的问题
A 类 实例化的时候需要B类; B类实例化的时候需要A类 ,这个时候就造成了循环依赖
解决循环依赖的方法是: 使用setter方式注入 即可
构造器注入: 会等待构造函数中实例化好以后,才会实例化bean
3.静态工厂注入
定义静态工厂
public class StaticFactory {
/**
* 实例化 TypeDao
* @return
*/
public static TypeDao createTypeDao(){
return new TypeDao();
}
}
定义被注入的对象
public class TypeDao {
public void getName(){
System.out.println("TypeDao getName test....");
}
}
定义 TypeService
public class TypeService {
private TypeDao typeDao;
public void setTypeDao(TypeDao typeDao) {
this.typeDao = typeDao;
}
public void test(){
System.out.println("TypeService test...");
typeDao.getName();
}
}
定义 Spring.xml
<?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">
<!-- SpringIOC 依赖注入 -->
<!-- 静态工厂注入-->
<bean id="typeService" class="org.wlc.service.TypeService">
<property name="typeDao" ref="staticFactory"></property>
</bean>
<!-- 静态工厂注入-->
<bean id="staticFactory" class="org.wlc.factory.StaticFactory" factory-method="createTypeDao"></bean>
</beans>
测试
public class App {
private static ApplicationContext context ;
public static void main(String[] args) {
method03();
}
/**
* 手动注入: 静态工厂注入
*/
public static void method03(){
context = new ClassPathXmlApplicationContext("Spring3.xml");
TypeService typeService = (TypeService) context.getBean("typeService");
typeService.test();
}
}
4.实例化工厂注入
定义 实例化工厂类
public class InstanceFactory {
public TypeDao getTypeDao() {
return new TypeDao();
}
}
定义被注入的类 TypeDao
public class TypeDao {
public void getName(){
System.out.println("TypeDao getName test....");
}
}
定义TypeService
public class TypeService {
/**
* 静态工厂注入和 实例化工厂注入其实还是使用了set方式注入
*/
private TypeDao typeDao;
public void setTypeDao(TypeDao typeDao) {
this.typeDao = typeDao;
}
public void test(){
System.out.println("TypeService test...");
typeDao.getName();
}
}
配置Spring.xml
<?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">
<!-- SpringIOC 依赖注入 -->
<bean id="typeService" class="org.wlc.service.TypeService">
<property name="typeDao" ref="typeDao"></property>
</bean>
<!-- 实例化工厂注入
1. 定义实例化工厂的bean
2. 定义要被注入的bean, 使用工厂bean factory-bean 指向 实例化工厂的bean
使用工厂方法 factory-method 指向实例化工厂中的方法
-->
<bean id="instanceFactory" class="org.wlc.factory.InstanceFactory"></bean>
<bean id="typeDao" factory-bean="instanceFactory" factory-method="getTypeDao"></bean>
</beans>
测试
public class App {
private static ApplicationContext context ;
public static void main(String[] args) {
method04();
}
/**
* 手动注入: 实例化工厂注入
*/
public static void method04(){
context = new ClassPathXmlApplicationContext("Spring4.xml");
TypeService typeService = (TypeService) context.getBean("typeService");
typeService.test();
}
}
结果:
TypeService test...
TypeDao getName test....