Spring IOC

Spring



1、IOC基础

1.1原本的方式

  1. 先写一个UserDao接口
public interface UserDao {
    void getUser();
}
  1. 再去写Dao的实现类
public class UserDaoImpl implements UserDao {
    public void getUser() {
        System.out.println("默认获取用户数据");
    }
}

  1. 然后去写UserService的接口
public interface UserService {
    void getUser();
}
  1. 最后写Service的实现类
import com.czx.dao.UserDao;
import com.czx.dao.UserDaoImpl;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    

    public void getUser() {
        userDao.getUser();
    }
}
  1. 测试
import com.czx.serivce.UserService;
import com.czx.serivce.UserServiceImpl;
import org.junit.Test;

public class MyTest {
    
    @Test public void test(){ 
        UserService service = new UserServiceImpl(); service.getUser(); 
    }
}

此时Userdao的实现类增加

public class UseDaoMysqlImpl implements UserDao {
    public void getUser() {
        System.out.println("Mysql获取用户数据");
    }
}

紧接着我们要去使用MySql的话 , 我们就需要去service实现类里面修改对应的实现 .

import com.czx.dao.UserDao;
import com.czx.dao.UserDaoMysqlImpl;

public class UserServiceImpl implements UserService {
    private UserMysqlDao userDao = new UserDaoMysqlImpl();

    

    public void getUser() {
        userDao.getUser();
    }
}

如果我们继续添加UseDao实现类,又需要去service实现类里面修改对应的实现 . 假设我们的这种需求非常大 , 这种方式就根本不适用了, 甚至反人类 , 每次变动 , 都需要修改大量代码 . 这种设计的耦合性太高了, 牵一发而动全身。

  • 解决方案
    我们可以在需要用到他的地方 , 不去实现它 , 而是留出一个接口 , 利用set , 我们去代码里修改下 .
import com.czx.dao.UserDao;

public class UserServiceImpl implements UserService {
    private UserDao userDao;


    public void getUser() {
        userDao.getUser();
    }



    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

测试:

@Test 
public void test(){ 
       UserServiceImpl service = new UserServiceImpl(); 
       service.setUserDao( new UserDaoMySqlImpl() ); 
       service.getUser(); 
       //那我们现在又想用Oracle去实现呢 
       service.setUserDao( new UserDaoOracleImpl() ); 
       service.getUser(); 
}
  • 以前所有东西都是由程序去进行控制创建 , 而现在是由我们自行控制创建对象 , 把主动权交给了调用者 . 程序不用去管怎么创建,怎么实现了 . 它只负责提供一个接口 .
  • 这种思想 , 从本质上解决了问题 , 我们程序员不再去管理对象的创建了 , 更多的去关注业务的实现 . 耦合性大大降低 . 这也就是IOC的原型 !

1.2、IOC本质

控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
在这里插入图片描述

  • IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。
  • Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从Ioc容器中取出需要的对象。The Spring IoC container
  • 控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定
    对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注
    入(DependencyInjection,DI)。

2、HelloSpring

2.1、导入jar包


    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
    </dependency>

2.2、编写代码

官方文档
按照官方文档进行简单的代码编写

  1. 编写一个Hello实体类
public class Hello {
    private String str;

    public Hello() {
    }
    public Hello(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

  1. 编写我们的spring文件 , 这里我们命名为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="hello" class="com.czx.pojo.Hello">
        <property name="str" value="spring"/>
    </bean>
</beans>
  1. 测试
import com.czx.pojo.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello);
    }

}

2.3、思考

  • Hello 对象是谁创建的 ? 【 hello 对象是由Spring创建的 】
  • Hello 对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器设置的 】
  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
    依赖注入 : 就是利用set方法来进行注入的.
    IOC是一种编程思想,由主动的编程变成被动的接收

2.4、对1.1进行修改

新增一个Spring配置文件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">

    <!-- services -->

    <bean id="MysqlImpl" class="com.czx.dao.UseDaoMysqlImpl"/>
    <bean id="OracleImpl" class="com.czx.dao.UseDaoOracleImpl"/>


    <bean id="ServiceImpl" class="com.czx.service.UserServiceImpl">
        <property name="userDao" ref="OracleImpl"/>
    </bean>
    <!-- more bean definitions for services go here -->

</beans>

测试

import com.czx.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserServiceImpl usl = (UserServiceImpl) context.getBean("ServiceImpl");
        usl.getUser();
    }
}

OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改, 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

3、IOC创建对象方式

3.1、通过无参构造方法来创建

  1. UserT.java

public class UserT {
    private String name;

    public UserT() {
        System.out.println("userT被创建");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){ 
    System.out.println("name="+ name ); 
    }
}

  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="use2" class="com.czx.pojo.UserT">
        
    </bean>

    
</beans>
  1. 测试
import com.czx.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

       User user = (User) context.getBean("use2");
       user.show();
    }
}

结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!

3.2、通过有参构造方法来创建

  1. User. java

public class User {
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){ System.out.println("name="+ name ); }
}

  1. beans.xml 有三种方式编写
<!-- 第一种根据index参数下标设置 --> 
<bean id="userT" class="com.kuang.pojo.UserT"> 
    <!-- index指构造方法 , 下标从0开始 --> 
    <constructor-arg index="0" value="kuangshen2"/> 
</bean>
<!-- 第二种根据参数名字设置 --> 
<bean id="userT" class="com.kuang.pojo.UserT"> 
    <!-- name指参数名 --> 
    <constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 --> 
<bean id="userT" class="com.kuang.pojo.UserT"> 
    <constructor-arg type="java.lang.String" value="kuangshen2"/> 
</bean>
  1. 测试

import com.czx.pojo.UserT;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        UserT user = (UserT) context.getBean("use2");
        user.show();
    }
}

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值