Spring的IOC理论和使用

IOC本质

控制反转IoC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IoC的一种方法。没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方。

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

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

Spring的使用

样例1:

1.pom.xml文件中加入Spring的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.0</version>
        </dependency>
    </dependencies>

2.创建pojo,一定要有set方法,因为Spring是需要通过set方法注入去生成对象

package com.cbbpp.pojo;

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;
    }
}

3.添加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-->
    <bean id="hello" class="com.cbbpp.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

<!--Beans就是IoC容器,用来托管bean(对象)-->

4.测试使用

import com.cbbpp.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {

        //获取Spring的上下文对象,拿到Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在Spring中被管理了,我们要使用,直接去里面取出来就可以了
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());

    }
}

控制台输入结果:

Hello{str='Spring'}

Process finished with exit code 0

样例2:

1.同上

2.设置接口和实现类

//接口UserDao 
package com.cbbpp.dao;

public interface UserDao {
    void getUser();
}
//接口UserService 
package com.cbbpp.service;

public interface UserService {
    void getUser();
}


//实现类UserDaoImpl 
package com.cbbpp.dao;

public class UserDaoImpl implements UserDao{
    public void getUser() {
        System.out.println("默认获取接口的数据");
    }
}

//实现类MysqlDaoImpl 
package com.cbbpp.dao;

public class MysqlDaoImpl implements UserDao{
    public void getUser() {
        System.out.println("Mysql的数据");
    }
}

//实现类OracleDaoImpl 
package com.cbbpp.dao;

public class OracleDaoImpl implements UserDao{
    public void getUser() {
        System.out.println("Oracle的数据");
    }
}

//实现类SqlServerDaoImpl 
package com.cbbpp.dao;

public class SqlServerDaoImpl implements UserDao{
    public void getUser() {
        System.out.println("sqlServer的数据");
    }
}

//实现类UserServiceImpl 
package com.cbbpp.service;

import com.cbbpp.dao.UserDao;
import com.cbbpp.dao.UserDaoImpl;

public class UserServiceImpl implements UserService{


    private UserDao userDao;

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


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

3.添加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-->
    <bean id="mysqlImpl" class="com.cbbpp.dao.MysqlDaoImpl"/>
    <bean id="OracleImpl" class="com.cbbpp.dao.OracleDaoImpl"/>
    <bean id="SqlServerImpl" class="com.cbbpp.dao.SqlServerDaoImpl"/>
    <bean id="UserImpl" class="com.cbbpp.dao.UserDaoImpl"/>


    <!--ref是用来应用Spring容器中已经创建好的对象,value是直接手动设置的具体值(基本类型)-->
    <bean id="UserServiceImpl" class="com.cbbpp.service.UserServiceImpl">
        <property name="userDao" ref="OracleImpl"/>
    </bean>

</beans>
        <!--Beans就是IoC容器,用来托管bean(对象)-->

4.测试使用

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
    }

控制台输入结果: 

Oracle的数据

Process finished with exit code 0

 

控制:谁来控制对象的创建,在传统的应用程序中对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的

反转:程序本身不参加创建对象,而变成被动的接收对象

依赖注入:就是利用set方法来进行注入的

IOC是一种编程思想,由主动的编程变成被动的接收

我们使用Spring彻底不要再去程序中进行改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓IoC,也就是:对象由Spring来创建、管理、装配

我们只需要玩配置文件就行了,程序不用改动任何

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值