【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入门(二)——静态工厂与实例工厂

发布了17 篇原创文章 · 获赞 1 · 访问量 636

猜你喜欢

转载自blog.csdn.net/weixin_43316702/article/details/104918082
今日推荐