【Spring】(二)IOC理论&HelloSpring
IOC
1.IOC基础
-
1.UserDao 接口类
public interface UserDao { void getUser(); } -
2.UserDaoImpl 实现类
public class UserDaoImpl implements UserDao{ public void getUser(){ System.out.println("默认获取用户的数据"); } } -
3.UserDaoMysqlImpl 实现类
public class UserDaoMysqlImpl implements UserDao { public void getUser() { System.out.println("Mysql获取用户的数据"); } } -
4.UserDaoOracleImpl 实现类
public class UserDaoOracleImpl implements UserDao { public void getUser() { System.out.println("oracle获取用户的数据"); } } -
5.UserService 业务接口
public interface UserService { void getUser(); } -
6.UserServiceImpl 业务实现类
public class UserServiceImpl implements UserService{ //调用某个接口实现类,需要修改UserServiceImpl中的这行代码 private UserDao userDao=new UserDaoMysqlImpl(); public void getUser(){ userDao.getUser(); } } -
用户的需求可能会影响原来的代码,我们需要根据用户的需求修改源代码。例如上例中,
UserDao userDao=new UserDaoMysqlImpl();,可以选择实例化UserDaoImpl或UserDaoMysqlImpl或UserDaoOracleImpl,根据用户需求不同,需要修改UserServiceImpl 中的代码。如果代码量很大,修改成本会很高。
-
什么是IOC?
修改UserServiceImpl:使用一个Set接口实现
private UserDao userDao; //利用set进行动态实现值的注入 public void setUserDao(UserDao userDao) { this.userDao = userDao; }测试类中添加
((UserServiceImpl) userService).setUserDao(new UserDaoMysqlImpl()); -
这种思想,使得程序员不用再取管理对象的创建了。耦合性降低。 这是IOC的原型。
2.IOC本质
参考:https://blog.csdn.net/qq_33369905/article/details/106647330
- 控制反转IOC,是一种设计思想;依赖注入DI是实现IOC的一种方法。没有IOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方。控制反转就是:获得依赖对象的方式反转了。
- 采用XML配置Bean时,Bean的定义信息和实现是分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接已注解的形式定义在实现类中,从而达到了零配置的目的。
- 控制反转是一种通过描述(XML或注解)并通过第三方获取特定对象的方式。在Spring中实现控制反转的是IOC容器,其实现方法是依赖注入DI。
3.HelloSpring
-
Hello.java
public class Hello { private String str; public String getStr(){ return str; } public void setStr(String str){ this.str=str; } @Override public String toString() { return "Hello{" + "str='" + str + '\'' + '}'; } } -
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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--使用Spring来创建对象,在Spring中这些称为Bean id = 变量名 class = new 的对象 property 相当于给对象中的属性设置一个值 --> <bean id="hello" class="pojo.Hello"> <property name="str" value="Spring"></property> </bean> </beans> -
测试
public class MyTest { public static void main(String[] args) { //获取Spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //对象由Spring管理,取出来即可 Hello hello =(Hello) context.getBean("hello"); System.out.println(hello.toString()); } } -
这个过程就叫控制反转;
-
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的。使用Spring后,对象是由Spring创建的。
-
反转:程序本身不创建对象,而变成被动的接受对象
-
依赖注入:利用Hello.java中setStr()方法进行注入的
-
-
以后不用改动程序了,要实现不同操作,只要在xml配置文件中进行修改即可。IOC:对象由Spring创建、管理、装配。
4.IOC基础 例子修改
-
配置文件中
<bean id="mysqlImpl" class="dao.UserDaoMysqlImpl"/> <bean id="oracleImpl" class="dao.UserDaoOracleImpl"/> <bean id="userServiceImpl" class="service.UserServiceImpl"> <!-- ref: 引用Spring容器中创建好的对象 value: 具体的值,基本数据类型 --> <property name="userDao" ref="oracleImpl"/> </bean>
-
测试
public class MyTest { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userServiceImpl = (UserService) context.getBean("userServiceImpl"); userServiceImpl.getUser(); } }

浙公网安备 33010602011771号