spring_如何让spring帮你实现对象创建

前言

  上一篇博客中,小咸儿提到IOC就是让spring帮你实现对象创建和依赖的代码,那么他是如何实现的呢?


叙述

传统模式

  先来看一下传统模式中是如何实现的。

接口层

  按照原来的方式的话,需要自己创建一个接口层:userService

package ioc;

/**
 * IOC示例:接口
 * @author Phyllis
 * @date 2019年7月18日17:35:06
 */
public interface UserService {
    public void addUser();
}

实现层

  接下来就是需要自己去创建一个具体实现类:UserServiceImpl

package ioc;

/**
 * IOC示例:具体实现类
 * @author Phyllis
 * @date 2019年7月18日17:36:38
 */
public class UserServiceImpl implements UserService{
    public void addUser() {
        System.out.println("a_ioc add user");
    }
}

测试类

  这时候就可以做一个简单的测试了:Test

package ioc;

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

/**
 * 测试IOC
 * @author Phyllis
 * @date 2019年7月18日17:44:30
 */
public class TestIoC {

    @Test
    public void demo01(){
        // 之前的开发模式,所以说这里需要自己去new一个对象
        UserService userService = new UserServiceImpl();
        userService.addUser();
    }
}

spring模式

  那么接下来看一下spring是如何帮我们管理的吧!

接口层

  接口层中的类和之前传统模式中的一样:UserService

package ioc;

/**
 * IOC示例:接口
 * @author Phyllis
 * @date 2019年7月18日17:35:06
 */
public interface UserService {
    public void addUser();
}

实现层

  实现层中的类和代码也是和传统模式中一样的:UserServiceImpl

package ioc;

/**
 * IOC示例:具体实现类
 * @author Phyllis
 * @date 2019年7月18日17:36:38
 */
public class UserServiceImpl implements UserService{
    public void addUser() {
        System.out.println("a_ioc add user");
    }
}

xml配置文件

  既然我们让spring进行对象创建和依赖,那么肯定会和传统模式不一样,那么这时候就需要xml配置文件了,让bean容器就帮助我们实例化对象。

  首先需要创建一个beans.xml文件放入到resources中
在这里插入图片描述
  接着在配置文件中配置好需要创建的对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context.xsd
                          http://www.springframework.org/schema/aop
                          http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置service
        <bean> 配置需要创建的对象
        id:用于之后从spring容器获得示例时使用的
        class:需要创建实例的全限定类名
    -->
    <bean id="userServiceId" class="ioc.UserServiceImpl"></bean>    
</beans>

测试类

  接下来只需要在测试类中,再次进行测试:

package ioc;

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

/**
 * 测试IOC
 * @author Phyllis
 * @date 2019年7月18日17:44:30
 */
public class TestIoC {
    @Test
    public void demo02(){
        // 从spring容器中获得
        // 1、获得容器
        String xmlPath = "beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        // 2、获得内容---不需要自己new,都是从spring容器获得
        UserService userService = (UserService) applicationContext.getBean("userServiceId");
        userService.addUser();
    }
}

结果: 这两个实例运行的结果都是一模一样,只不过我们不在自己去创建对象实例,而且交给spring的bean容器去帮我们实现。


总结

  有关更多spring的内容分享,还请见小咸儿的博客内容。

感谢您的阅读~~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值