自己简单封装spring容器

简介: 本文主要讲如何自己简单封装spring容器
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 服务提供核心类
 * 该类的主要作用是加载beans.xml文件
 * @author grace
 *
 */
public class ServiceProvinderCore {
  protected ApplicationContext ctx;
  /**
   * @param filename beans.xml
   */
  public void load(String filename){
    ctx=new ClassPathXmlApplicationContext(filename);
  }
}


import org.apache.commons.lang.xwork.StringUtils;
public class ServiceProvinder {
  private static ServiceProvinderCore sc;
//  封装方法:
//  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
//  静态方法,一调用该类就执行该方法
  static{
    sc=new ServiceProvinderCore();
    sc.load("beans.xml");
  }
//  封装方法:
//  ISysUserGroupService sysUserGroupService=(ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME);
  public static Object getService(String beanName){
    if(StringUtils.isBlank(beanName)){
      throw new RuntimeException("您要访问的服务名称不能为空");
    }
    Object bean=null;
    //如果spring容器中包含beanName
    if(sc.ctx.containsBean(beanName)){
      bean=sc.ctx.getBean(beanName);
    }
    //如果spring容器中不包含beanName
    if(bean==null){
      throw new RuntimeException("您要访问的服务名称["+beanName+"]不存在");
    }
    return bean;
  }
}

测试类:

import java.util.List;
import org.junit.Test;
import cn.grace.container.ServiceProvinder;
import cn.grace.domain.SysUserGroup;
import cn.grace.service.ISysUserGroupService;
public class TestSysUserGroupService {
  @Test
  public void testSave() {
//    使用自己封装的spring容器拿到service
    ISysUserGroupService sysUserGroupService=(ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME);
//    原本的方法
//    ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
//    ISysUserGroupService sysUserGroupService=(ISysUserGroupService) ctx.getBean(ISysUserGroupService.SERVICE_NAME);
    SysUserGroup sysUserGroup = new SysUserGroup();
    sysUserGroup.setName("销售部");
    sysUserGroup.setPrincipal("xxx");
    sysUserGroup.setIncumbent("ttt");
    sysUserGroupService.saveSysUserGroup(sysUserGroup);
  }
}
public interface ISysUserGroupService {
  public final static String  SERVICE_NAME="cn.grace.service.impl.SysUserGroupServiceImpl";
}

优点:


1.操作方便,通过ServiceProvinder.getService(beanName);直接拿到service.

2.如果请求的beanName不合法,通过异常可以看到效果。

相关文章
|
6月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
6月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
669 2
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult<T>`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码"0"和消息"操作成功!",有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
925 0
|
9月前
|
JSON Java 数据格式
Spring Boot返回Json数据及数据封装
在Spring Boot中,接口间及前后端的数据传输通常使用JSON格式。通过@RestController注解,可轻松实现Controller返回JSON数据。该注解是Spring Boot新增的组合注解,结合了@Controller和@ResponseBody的功能,默认将返回值转换为JSON格式。Spring Boot底层默认采用Jackson作为JSON解析框架,并通过spring-boot-starter-json依赖集成了相关库,包括jackson-databind、jackson-datatype-jdk8等常用模块,简化了开发者对依赖的手动管理。
795 3
|
11月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
287 18
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
Spring容器的本质
本文主要讨论Spring容器最核心的机制,用最少的代码讲清楚Spring容器的本质。
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
490 6
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
381 1