【Spring框架】Spring入门(一)——IoC与DI

一、IoC

1.IoC介绍
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。

2.IoC实现原理
将需要实现的对象创建、依赖的代码,反转给spring容器来帮忙实现,从而降低代码之间的耦合度。

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
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--配置Serivice
            <bean>配置需要创建的对象
                id,用于之后从spring容器获得实例时使用的
                class,需要创建实例的全限定类名
        -->
        <bean id="UserServiceId" class="com.spring.a_ioc.UserServiceImpl"></bean>
</beans>

java代码:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc {
    @Test
    public void demo01() {//这是原始的创建对象方式
        UserService userService = new UserServiceImpl();
        userService.addUser();
    }

    @Test
    public void demo02() {//这是反转到容器中进行操作
        //从spring容器获得
        //1.获取容器
        String xmlPath = "com/spring/a_ioc/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //2.获得内容,不需要自己new一个对象
        UserService userService = (UserService) applicationContext.getBean("UserServiceId");
        userService.addUser();
    }
}


//下面是接口和实现类----->
//接口
public interface UserService {
    public void addUser();
}

//实现类
public class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
        System.out.println("a_ioc_user");
    }
}

二、DI

1.DI介绍
类a要依赖类b,则必须要获得类b的实例。这个获得类b的实例的过程则就是依赖注入(DI)。

2.DI实现原理
通过在spring容器中进行对象的创建,对类之间有关系的bean建立联系,从而达到a类获得b类的实例。

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
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--配置Serivice
            <bean>配置需要创建的对象
                id,用于之后从spring容器获得实例时使用的
                class,需要创建实例的全限定类名
                property,对相同或相关工厂中的其他bean的引用,列表,映射和属性
        -->
        <bean id="BookServiceId" class="com.spring.b_di.BookServiceImpl">
                <property name="bookDao" ref="BookDaoId"></property>
        </bean>

        <!--创建Dao实例-->
        <bean id="BookDaoId" class="com.spring.b_di.BookDaoImpl"></bean>
</beans>

java代码:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDi {
    @Test
    public void demo01() {
        //从spring容器获得
        //1.获取容器
        String xmlPath = "com/spring/b_di/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //2.获得内容,不需要自己new一个对象
        BookService bookService = (BookService) applicationContext.getBean("BookServiceId");
        bookService.addBook();
    }
}


//下面是接口和实现类----->
//接口
public interface BookDao {
    public void save();
}

public interface BookService{
    public abstract void addBook();
}

//实现类
public class BookDaoImpl implements BookDao{
    @Override
    public void save() {
        System.out.println("DI实现了");
    }
}

public class BookServiceImpl implements BookService{

    //方式一,之前接口+实现类
    //private BookDao bookDao = new BookDaoImpl();
    //方式二,接口+setter
    private BookDao bookDao;
    public void setBookDao(BookDao bookDao){
        this.bookDao = bookDao;
    }

    @Override
    public void addBook() {
        this.bookDao.save();
    }
}

下一节:【Spring框架】Spring入门(二)——静态工厂与实例工厂

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谦风(Java)

一起学习,一起进步(✪ω✪)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值