04 Spring IoC
1、IoC的概念
IoC,控制反转,就是将对象的实例化交给Spring容器,通过Spring容器的实例化来降低层与层之间以及程序之间的耦合与依赖。
2、IoC容器对象
BeanFactory
ApplicationContext
通过ClassPathXmlApplicationContext创建容器对象ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
通过容器对象获取注入的对象
UserDao userDao = (UserDao)context.getBean("userDao");
3、DI的方式
3.1 基于属性的注入
User代码
public class User { private String name; private String mobile; //构造函数 - 构造函数注入 public User(String name,String mobile) { this.name = name; this.mobile = mobile; } public String toString() { return "用户名:"+this.name+";手机号:"+this.mobile; } }
UserDao代码
public interface UserDao { public void insert(User user); //添加用户 }
接口实现1:
public class UserDaoImpl1 implements UserDao { @Override public void insert(User user) { System.out.println("用户["+user+"]被新增1"); } }
接口实现2:
public class UserDaoImpl2 implements UserDao { @Override public void insert(User user) { System.out.println("用户["+user.toString()+"]被新增2"); } }
用户服务层:
public class UserService { //属性注入 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } //添加用户 public void addUser(User user) { userDao.insert(user); } }
applicationContext.xml
user使用的是基于构造函数的注入
userDao使用的基于属性的注入
<?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="cn.sjxy.domain.User"> <constructor-arg index="0" name="name" type="String" value="XXX" ></constructor-arg> <constructor-arg index="1" name="mobile" type="String" value="1534518XXXX" ></constructor-arg> </bean> <bean id="userDao" class="cn.sjxy.daoImpl.UserDaoImpl1"> </bean> <bean id="userService" class="cn.sjxy.service.UserService"> <property name="userDao" ref="userDao" ></property> </bean> </beans>
测试代码:
public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取用户 User user = (User)context.getBean("user"); System.out.println(user.toString()); //获取用户业务类 UserService userService = (UserService)context.getBean("userService"); userService.addUser(user); } }
3.2 基于构造函数的注入
Bank代码
public class Bank { private String bankName; private String bankNo; //构造函数注入 public Bank(String bankName,String bankNo) { this.bankName = bankName; this.bankNo = bankNo; } public String toString() { return "[bankName="+this.bankName+";bankNo="+this.bankNo+"]"; } }
Address代码
public class Address { private String province; private String city; private String info; public String getProvince() { return province; } //属性注入 public void setProvince(String province) { this.province = province; } public String getCity() { return city; } //属性注入 public void setCity(String city) { this.city = city; } public String getInfo() { return info; } //属性注入 public void setInfo(String info) { this.info = info; } public String toString() { return "[Province="+this.province+",City="+this.city+",info="+this.info+"]"; } }
Customer代码
public class Customer { private String id; private String name; private Address add; private Bank bank; //构造函数重载 public Customer(String id,String name,Address add,Bank bank) { this.id = id; this.name = name; this.add = add; this.bank = bank; } public String toString() { return "id="+this.id+",name="+this.name+",add="+this.add+",bank="+this.bank+""; } }
applicationContext配置
<?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="bank" class="cn.sjxy.domain.Bank"> <constructor-arg index="0" name="bankName" type="String" value="交通银行" ></constructor-arg> <constructor-arg index="1" name="bankNo" type="String" value="88888888" ></constructor-arg> </bean> <bean id="address" class="cn.sjxy.domain.Address"> <property name="province" value="江苏省"></property> <property name="city" value="南京市"></property> <property name="info" value="玄武区"></property> </bean> <bean id="customer" class="cn.sjxy.domain.Customer"> <constructor-arg index="0" name="id" type="String" value="10001" ></constructor-arg> <constructor-arg index="1" name="name" type="String" value="王志瑞" ></constructor-arg> <constructor-arg index="2" name="add" ref="address" ></constructor-arg> <constructor-arg index="3" name="bank" ref="bank" ></constructor-arg> </bean> </beans>
测试代码:
public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cus = (Customer)context.getBean("customer"); System.out.println(cus.toString()); } }