Spring-IOC

IOC

1. IOC理论推导

  1. UserDao 接口

    package com.wang.dao;
    
    public interface UserDao {
        void getUser();
    }
    
  2. UserDaoImpl 实现类

    package com.wang.dao;
    
    public class UserDaoImpl implements UserDao{
        @Override
        public void getUser() {
            System.out.println("获取用户的数据");
        }
    }
    
    package com.wang.dao;
    
    public class UserDaoMysqLImpl implements UserDao{
        @Override
        public void getUser() {
            System.out.println("MysqL获取用户数据!");
        }
    }
    
    package com.wang.dao;
    
    public class UserDaoOrcaleImpl implements UserDao{
        @Override
        public void getUser() {
            System.out.println("Orcale获取用户数据!");
        }
    }
    
  3. UserService 业务接口

    package com.wang.service;
    
    public interface UserService {
        void getUser();
    }
    
  4. UserServiceImpl 业务实现类

    package com.wang.service;
    
    import com.wang.dao.UserDao;
    import com.wang.dao.UserDaoImpl;
    import com.wang.dao.UserDaoMysqLImpl;
    import com.wang.dao.UserDaoOrcaleImpl;
    
    public class UserServiceImpl implements UserService{
    
        private UserDao userDao;
    
        //利用Set进行动态实现值的注入
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        @Override
        public void getUser() {
            userDao.getUser();
        }
    }
    
  5. 测试

    import com.wang.dao.UserDaoImpl;
    import com.wang.dao.UserDaoMysqLImpl;
    import com.wang.dao.UserDaoOrcaleImpl;
    import com.wang.service.UserServiceImpl;
    import org.junit.Test;
    
    public class MyTest {
    
        @Test
        public void test() {
            //用户实际调用的是业务层,dao层他们不需要接触!
            UserServiceImpl userService = new UserServiceImpl();
            userService.setUserDao(new UserDaoMysqLImpl());
            userService.getUser();
    
            userService.setUserDao(new UserDaoOrcaleImpl());
            userService.getUser();
    
            userService.setUserDao(new UserDaoImpl());
            userService.getUser();
        }
    
    }
    

在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改代码!如果代码量十分大,修改一次的成本代价十分昂贵!

我们使用一个Set接口实现,已经发生了革命性的变化!

//利用Set进行动态实现值的注入
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}
  • 之前程序是主动创建对象!控制权在程序员手上!
  • 使用了Set注入后,程序不再具有主动性,而是变成了被动的接收对象!

这种思想从本质上解决了问题,我们程序员不再需要管理对象的创建了,系统的耦合性大大降低,可以专注在业务是实现上!这是IOC的原型!

主动权在用户,用户选择要调用什么!

2. IOC的本质

控制反转(inversion of control), 是一种设计思想,DI(dependency injection依赖注入)是IOC的一种方法.未使用IOC的程序中,我们使用面向对象编程,对象的创建和对象之间的依赖关系完全硬编码在程序中,对象的创建是由程序自己控制的.控制反转就是将对象的创建转移给了第三方.IOC就我认为是:获得依赖对象的方式反转了

avator

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

3. HelloSpring

利用Spring输出

1. 创建实体类

package com.wang.pojo;

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 + '\'' +
                '}';
    }
}

2. 创建beans.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
        Java
        类型 变量名 = new 类型()
        Hello hello = new Hello();
        Spring
        id = 变量名
        class = new的对象
        property 相当于给对象中的属性设置一个值!
        bean = 对象  ====> new Hello();
    -->
    <bean id="hello" class="com.wang.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

3. 代码测试

import com.wang.pojo.Hello;
import org.junit.Test;
import org.springframework.context.support.ClasspathXmlApplicationContext;

public class MyTest {

    @Test
    public void test() {
        //获取Spring的上下文对象!
        ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("beans.xml");
        //我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以!
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

4. 注意

  1. xml中:

    使用Spring来创建对象,这些都称为Bean
    Java
    类型 变量名 = new 类型()
    Hello hello = new Hello();
    Spring
    id = 变量名
    class = new的对象
    property 相当于给对象中的属性设置一个值!
    bean = 对象 ====> new Hello();

  2. 使用spring

    获取Spring的上下文对象:
    ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("beans.xml");
    
  3. Hello对象由Spring创建

  4. Hello对象的属性是由spring容器设置的

4. 利用Spring实现IOC

1. 创建beans.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">

    <bean id="MysqLImpl" class="com.wang.dao.UserDaoMysqLImpl"/>
    <bean id="oracleImpl" class="com.wang.dao.UserDaoOracleImpl"/>

    <bean id="userServiceImpl" class="com.wang.service.UserServiceImpl">
        <!--
            ref : 引用spring容器中创建好的对象
            value : 具体的值,基本数据类型
            此时用户只需要修改ref的对象,即可实现代码修改!
        -->
        <property name="userDao" ref="oracleImpl"/>
    </bean>


</beans>

2. 测试

import com.wang.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClasspathXmlApplicationContext;

public class MyTest {

    @Test
    public void test() {
        //获取ApplicationContext,拿到spring的容器
        ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("beans.xml");

        //需要什么就直接get什么,getBean("")里面放在xml中的id的名字!
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");

        userServiceImpl.getUser();
    }

}

3. 注意点

  1. XML中

    ref : 引用spring容器中创建好的对象
    value : 具体的值,基本数据类型

  2. 使用spring时

    //需要什么就直接get什么,getBean("")里面放在xml中的id的名字!
            UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
    

5.IOC创建对象的方式

1. 使用无参构造创建对象,认!

直接使用property

2. 使用有参构造创建对象(不一定要有无参构造)

使用constructor-arg index/type/name = " " value = " "

1. 下标赋值(属性的下标)

<!--下标赋值-->
<bean id="user" class="com.wang.pojo.User">
    <constructor-arg index="0" value="wang sky"/>
</bean>

2. 类型(存在类型相同的风险!)

<!--通过类型创建,不建议使用!-->
<bean id="user" class="com.wang.pojo.User">
    <constructor-arg type="java.lang.String" value="wang sky"/>
</bean>

3. 参数名(推荐使用!)

<!--直接通过参数名来设置-->
<bean id="user" class="com.wang.pojo.User">
    <constructor-arg name="name" value="wang sky"/>
</bean>

3. 总结

配置文件加载的时候,容器中管理的对象就已经被初始化了!

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...