IOC和DI

目录

1.IOC(控制反转)

1.1 理解

1.2 代码实现

2.DI(依赖注入)

2.1 理解

2.2 代码实现

3.总结

4.建议采纳


1.IOC(控制反转)

1.1 理解

        将原本程序new出来的对象,交给spring管理(创建),使用时,调用getBean方法从spring工厂获取对象,使用spring来负责控制对象的生命周期和对象间的关系。

1.2 代码实现

(1)配置spring的pom配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-demo</artifactId>
        <groupId>com.fs</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-demo-ioc</artifactId>

    <properties>
        <spring.version>5.2.15.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>

</project>

(2)创建Hello类

public class Hello implements IHello{
    public String sayHello(String word){
        return "hello:" + word;
    }
}

(3)在resources包中创建spring配置文件,并使用bean标签将Hello对象交给spring容器管理。

<?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">


    <!--把hello对象交给spring管理-->
    <bean id="Hello" class="com.fs.ioc.Hello"></bean>
</beans>

(4)创建HelloMain类,在类中的main方法中,首先先创建applicationContext对象,其次,调用其getBean方法,获取Hello对象,最后再使用Hello对象完成相关操作。

public class HelloMain {

    public static void main(String[] args) {

        //调用hello类的sayHello()方法
        //1.传统方法
//        Hello hello = new Hello();
//        System.out.println(hello.sayHello("spring"));

        //2.spring的方式
        //1.创建ioc容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //2.获取hello对象 getBean,(1)根据id获取
        IHello hello = (IHello) applicationContext.getBean("Hello");
        //(2)根据类型获取
        IHello ihello = applicationContext.getBean(IHello.class);
        //(3)根据id、类型获取
        applicationContext.getBean("Hello",IHello.class);
        //3.调用方法
        System.out.println(hello.sayHello("spring"));
    }

}

2.DI(依赖注入)

2.1 理解

       将原本程序new出来的对象,交给spring管理(创建),使用时,通过依赖注入,被动获取bean对象。

2.2 代码实现

(1)配置spring的pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-demo</artifactId>
        <groupId>com.fs</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-demo-ioc</artifactId>

    <properties>
        <spring.version>5.2.15.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>

</project>

(2)创建UserDao接口

public interface UserDao {

    public void saveUser();
}

(3)创建对应的UserDao接口的实现类UserDaoImpl

public class UserDaoImpl implements UserDao {
    @Override
    public void saveUser() {
        System.out.println("保存用户成功。。。");
    }
}

(4)在resources包中配置spring配置文件,将UserDao对象交给spring容器管理

<?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">

    <!--把UserDaoImpl对象交给spring-->
    <bean id="UserDao" class="com.fs.dao.impl.UserDaoImpl"></bean>

(5)创建UserService接口

public interface UserService {

    public void register();
}

(6)创建UserService的实现类UserServiceImpl

(7)在spring配置文件中,将UserService对象交给spring容器管理并引入UserDao的bean对象

<!--需要spring给你东西,前提条件:把自己交给spring管理-->
    <bean id="userService" class="com.fs.service.impl.UserServiceImpl" autowire="byName">
        <!--ref:引用一个bean,写的bean的id,告诉spring你需要的-->
        <property name="userDao" ref="UserDao"/>
    </bean>

(8)在UserServiceImpl中声明UserDao对象并设置对应的set函数,在使用UserDao对象时,由spring容器自动注入。

public class UserServiceImpl implements UserService {

    //基于接口隔离
    private UserDao userDao;//依赖UserDao 被动

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

    @Override
    public void register() {
        System.out.println("业务层的代码");
        userDao.saveUser();

    }
}

3.总结

        IOC和DI都是把要手动创建的对象交给spring容器管理(创建),无需手动创建对象,它们是同一个概念的不同角度描述,IOC使用时,通过getBean方法去获取bean对象,而DI通过依赖注入,在使用时,由spring容器自动注入。它们利用依赖关系注入的方式,实现对象之间的解耦。

4.建议采纳

如有建议或者错误请私信我进行修改,感谢!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值