✨setter注入
- 依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入!
✨案例
✨BookDao接口
package com.study.dao;
public interface BookDao {
void save();
}
✨BookDaoImpl实现类
package com.study.dao.impl;
import com.study.dao.BookDao;
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
✨UserDao接口
package com.study.dao;
public interface UserDao {
void save();
}
✨UserServiceImpl实现类
package com.study.dao.impl;
import com.study.dao.UserDao;
public class UserDaoImpl implements UserDao {
private String name;
private int age;
public void save() {
System.out.println(name+","+age);
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
✨BookService接口
package com.study.service;
public interface BookService {
void save();
}
✨BookServiceImpl实现类
package com.study.service.impl;
import com.study.dao.BookDao;
import com.study.dao.UserDao;
import com.study.service.BookService;
public class BookServiceImpl implements BookService {
//简单类型
private BookDao bookDao;
private UserDao userDao;
public void save() {
bookDao.save();
userDao.save();
System.out.println("BookServiceImpl...");
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
✨applicationContext.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">
<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl"></bean>
<bean id="UserDao" class="com.study.dao.impl.UserDaoImpl">
<property name="name" value="小马哥"></property>
<property name="age" value="34"></property>
</bean>
<bean id="bookService" class="com.study.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
<property name="userDao" ref="UserDao"></property>
</bean>
</beans>
✨Test测试类
import com.study.dao.BookDao;
import com.study.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService service = app.getBean(BookService.class);
service.save();
}
}
/*
BookDaoImpl...
小马哥,34
BookServiceImpl...
*/
✨构造器注入
- 使用无参构造创建对象,默认!
- 假设我们要使用有参构造创建对象。
✨案例
✨BookDao接口
package com.study.dao;
public interface BookDao {
void save();
}
✨BookDaoImpl实现类
package com.study.dao.impl;
import com.study.dao.BookDao;
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("BookDaoImpl...");
}
}
✨UserDao接口
package com.study.dao;
public interface UserDao {
void save();
}
✨UserServiceImpl实现类
package com.study.dao.impl;
import com.study.dao.UserDao;
public class UserDaoImpl implements UserDao {
private String name;
private int age;
public void save() {
System.out.println(name+","+age);
}
public UserDaoImpl(String name, int age) {
this.name = name;
this.age = age;
}
}
✨BookService接口
package com.study.service;
public interface BookService {
void save();
}
✨BookServiceImpl实现类
package com.study.service.impl;
import com.study.dao.BookDao;
import com.study.dao.UserDao;
import com.study.dao.impl.BookDaoImpl;
import com.study.dao.impl.UserDaoImpl;
import com.study.service.BookService;
public class BookServiceImpl implements BookService {
//简单类型
private BookDao bookDao;
private UserDao userDao;
public void save() {
bookDao.save();
userDao.save();
System.out.println("BookServiceImpl...");
}
public BookServiceImpl(BookDao bookDao, UserDao userDao) {
this.bookDao = bookDao;
this.userDao = userDao;
}
}
✨applicationContext.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">
<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl"></bean>
<bean id="UserDao" class="com.study.dao.impl.UserDaoImpl">
<!--第一种方式:直接通过参数名来设置 -->
<constructor-arg name="name" value="小马哥"/>
<constructor-arg name="age" value="45"/>
<!--第二种方式:下标赋值 -->
<!-- <constructor-arg index="0" value="小马哥"/>
<constructor-arg index="1" value="45"/>-->
</bean>
<bean id="bookService" class="com.study.service.impl.BookServiceImpl">
<!--name是形参的值-->
<constructor-arg name="bookDao" ref="bookDao"></constructor-arg>
<constructor-arg name="userDao" ref="UserDao"></constructor-arg>
</bean>
</beans>
✨Test测试类
import com.study.dao.BookDao;
import com.study.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService service = app.getBean(BookService.class);
service.save();
}
}
/*
BookDaoImpl...
小马哥,45
BookServiceImpl...
*/
✨依赖注入方式选择
- 强制依赖使用构造器进行,使用setter注入有概率不进行注入导致null对象出现
- 可选依赖使用setter注入进行,灵活性强
- Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨
- 如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入
- 实际开发过程中还要根据实际情况分析,如果受控对象没有提供setter方法就必须使用构造器注入
- 自己开发的模块推荐使用setter注入