spring BeanProcessor接口详解

1. 简单认识BeanProcessor

BeanProcessor的理解

BeanProcessor是spring中的一个重要接口,他有两个接口方法一个是postProcessBeforeInitialization前置初始化,另一个是postProcessAfterInitialization后置初始化。从名称上就可以大概清楚这个接口的作用:在一个业务流程的前后加入两个接口方法,当执行这个业务流程时,就会触发这两个接口方法的执行。简单的总结一下有两个要点:

  1. 在业务流程中,根据BeanProcessor接口方法加在不同的位置(一般是前后),可以实现对业务逻辑的扩展。
  2. 在业务逻辑执行前,BeanProcessor的实现类必须已经被创建完成(BeanProcessor接口类必须要优先实例化)。

而在spring中,就有很多实现了BeanProcessor的bean,通过在重要的业务流程(如bean的生命周期流程)的前后加上BeanProcessor接口方法,就可以对业务逻辑进行修改或补充。

一个BeanProcessor的使用实例

在spring的bean生命周期中,BeanProcessor接口方法会在bean创建后的初始化方法(init-method或@PostConstruct指向的方法)前后执行before和after方法;那有没有在bean创建前后执行的接口方法呢?答案是肯定有的,这个功能是由BeanProcessor的子接口InstantiationAwareBeanPostProcessor来实现的,他也是有before和after方法,会在bean实例化前后执行。

我们先定义一个BeanProcessor接口实现类和一个InstantiationAwareBeanPostProcessor接口实现类。

BeanPostProcessor实现类:

//net.postProcessor.CustomerPostProcessor
@Component
public class CustomerPostProcessor implements BeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("执行CustomerPostProcessor的PostConstruct");
  }

  public CustomerPostProcessor(){
   System.out.println("执行CustomerPostProcessor的构造方法");
  }

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======BeforeInitialization======"+ beanName);
   return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======AfterInitialization======"+ beanName);
   return bean;
  }

}

InstantiationAwareBeanPostProcessor实现类:

//net.postProcessor.CustomerInitialPostProcessor
@Component
public class CustomerInitialPostProcessor implements InstantiationAwareBeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("执行CustomerInitialPostProcessor的PostConstruct");
  }

  public CustomerInitialPostProcessor(){
   System.out.println("执行CustomerInitialPostProcessor的构造方法");
  }

  @Override
  public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
   System.out.println("bean初始化前执行:class为"+beanClass.getName()+"|beanName为"+beanName);
   return null;
  }

  @Override
  public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
   System.out.println("bean初始化后执行:Object为"+bean+"|beanName为"+beanName);
   return false;
  }
}

再创建一个普通的bean对象:

//net.postProcessor.FirstBean
@Component
public class FirstBean implements InitializingBean {

  private String msg = "hello";

  @PostConstruct
  public void init(){
   System.out.println("执行FirstBean的PostConstruct");
  }

  public FirstBean(){
   System.out.println("FirstBean构造方法!"+msg);
  }

  public String getMsg() {
   return msg;
  }

  public void setMsg(String msg) {
   this.msg = msg;
  }

  @Override
  public void afterPropertiesSet() throws Exception {
   System.out.println("执行FirstBean的afterPropertiesSet");
  }
}

我们创建一个spring工厂对象将上述bean加载进去:

@Test
public void test(){
  AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("net.postProcessor");
}
//执行得到以下结果:
执行CustomerInitialPostProcessor的构造方法
执行CustomerInitialPostProcessor的PostConstruct
执行CustomerPostProcessor的构造方法
执行CustomerPostProcessor的PostConstruct

bean初始化前执行:class为net.postProcessor.FirstBean|beanName为firstBean
FirstBean构造方法!hello
bean初始化后执行:Object为net.postProcessor.FirstBean@79179359|beanName为firstBean

net.postProcessor.FirstBean@79179359======BeforeInitialization======firstBean
执行FirstBean的PostConstruct
执行FirstBean的afterPropertiesSet
net.postProcessor.FirstBean@79179359======AfterInitialization======firstBean

通过上述结果证明了我们之前的说法是正确的:

1.BeanPostProcessor接口类会优先实例化,且在实例化中无法不会调用BeanPostProcessor接口方法的

2.InstantiationAwareBeanPostProcessor接口方法会在FirstBean构造方法构造方法前后执行

3.BeanPostProcessor接口方法会在FirstBean实例化后进行初始化的前后执行

注意:若@PostConstruct注解方法方法未执行,请加入javax.annotation:javax.annotation-api:1.3.2jar包依赖,原因是@PostConstruct是J2EE标准的注解,不是spring自己的接口,而在JDK8往上的版本中设计者打算弃用这些注解,所以做了处理,我们是没有办法直接使用J2EE标准注解的(@Resource、@PostConstruct、@PreDestroy等几个注解),为了兼容这种情况,所以有了javax.annotation-apijar包的产生(或者降低JDK版本)。

2. BeanProcessor的实现思路和简化实例

BeanProcessor大概的实现思路

通过之前的了解BeanProcessor的使用,我们可以知道BeanProcessor并不复杂,但是却十分的重要,下面来分析下BeanProcessor的实现思路:

  1. 创建个接口A,接口包含一些切点方法(Before、After、Around之类的),实现这个接口A的类要在使用前就创建好
  2. 我们需要有个业务流程,这个业务流程由若干步组成;将接口A的接口方法插入到这些业务步骤之间(需要扩展的地方)
  3. 要执行这个业务流程时,把接口A的实现类对象赋值到业务流程中,在执行业务流程中,就会触发接口方法的执行完成功能扩展

当我们更换赋值到业务流程中的接口A的实现类时,对应的扩展逻辑也会随之变化,这样就实现了可插拔式的扩展逻辑(策略模式)。

一个BeanProcessor的简化逻辑实例

在spring中我们可以创建任意数量的bean实现BeanProcessor接口,所以实际上我们是要一个全局的beanProcessorList对象用来存储这些BeanProcessor对象;在执行业务代码时,要循环这个beanProcessorList对象,获取你需要的BeanProcessor对象来执行接口方法。下面是一个模拟spring bean生命周期的简化版,来帮助你理解spring中BeanProcessor的工作原理。

net.postProcessor.SecondBean.java

@Component
public class SecondBean {

  private String msg = "world";

  public SecondBean(){
   System.out.println("SecondBean构造方法!"+msg);
  }

  public String getMsg() {
   return msg;
  }

  public void setMsg(String msg) {
   this.msg = msg;
  }
}

net.postProcessor.CustomerPostProcessor.java

@Component
public class CustomerPostProcessor implements BeanPostProcessor {

  @PostConstruct
  public void init(){
   System.out.println("执行CustomerPostProcessor的PostConstruct");
  }

  public CustomerPostProcessor(){
   System.out.println("执行CustomerPostProcessor的构造方法");
  }

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======BeforeInitialization======"+ beanName);
   return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   System.out.println(bean+"======AfterInitialization======"+ beanName);
   return bean;
  }

}

net.postProcessor.PostProcessor.java

public class PostProcessor {

  //模拟扫描到的bean信息<"SecondBean", "net.postProcessor.SecondBean">
  Map<String, String> scanBeanMap = new HashMap<>();

  //模拟spring的beanPostProcessors列表
  List<BeanPostProcessor> processorBeanList = new ArrayList<>();

  //模拟bean对象缓存
  Map<String, Object> beanCache = new HashMap<>();

  //添加扫描的bean信息
  public PostProcessor addBeanInfo(String beanName, String classPath){
   this.scanBeanMap.put(beanName, classPath);
   return this;
  }

  //模拟bean创建流程
  public Object execute(){
   try {
     //先临时存储实现了postProcessor接口的bean对象
     List<BeanPostProcessor> postProcessorStrList = new ArrayList<>();
     //循环scanBeanMap,获取bean列表中实现了postProcessor接口的类,加入processorBeanList中
     for(String temp: scanBeanMap.keySet()){
      Class<?> clazz = Class.forName(scanBeanMap.get(temp));
      //判断是否实现了BeanPostProcessor接口
      if(BeanPostProcessor.class.isAssignableFrom(clazz)){
        //实例化让如临时容器
        postProcessorStrList.add((BeanPostProcessor)createBean(temp));
      }
     }
     //将实现了postProcessor接口的bean加入processorBeanList中
     for(BeanPostProcessor obj: postProcessorStrList){
      processorBeanList.add(obj);
     }

     //再次循环scanBeanMap初始化所用bean
     for(String temp: scanBeanMap.keySet()){
      createBean(temp);
     }

   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   }
   return null;
  }

  //bean实例化
  public Object createBean(String beanName){
   //从缓存中获取
   if(beanCache.containsKey(beanName)){
     return beanCache.get(beanName);
   }else{
     //缓存中取不到,则进行创建后加入缓存
     try {
      Class<?> clazz = Class.forName(scanBeanMap.get(beanName));
      //processor前置方法执行
      for(BeanPostProcessor processor : processorBeanList){
        processor.postProcessBeforeInitialization(clazz, beanName);
      }

      //bean实例化
      Object result = clazz.getConstructor().newInstance();

      //processor后置方法执行
      for(BeanPostProcessor processor : processorBeanList){
        processor.postProcessAfterInitialization(result, beanName);
      }

      //将bean加入缓存
      beanCache.put(beanName, result);
      return result;
     } catch (ClassNotFoundException e) {
      e.printStackTrace();
     } catch (IllegalAccessException e) {
      e.printStackTrace();
     } catch (InstantiationException e) {
      e.printStackTrace();
     } catch (NoSuchMethodException e) {
      e.printStackTrace();
     } catch (InvocationTargetException e){
      e.printStackTrace();
     }
   }
   return null;
  }

}

代码调用

public static void main(String[] args) {
  PostProcessor postProcessor = new PostProcessor();
  //添加扫描到的bean
  postProcessor
  .addBeanInfo("SecondBean", "net.postProcessor.SecondBean")
  .addBeanInfo("CustomerPostProcessor", "net.postProcessor.CustomerPostProcessor");
  postProcessor.execute();
}

//执行结果
执行CustomerPostProcessor的构造方法
class net.postProcessor.SecondBean======BeforeInitialization======SecondBean
SecondBean构造方法!world
net.postProcessor.SecondBean@1b40d5f0======AfterInitialization======SecondBean

代码逻辑如下:

  1. 循环bean信息列表,将BeanPostProcessor接口bean分离出来优先实例化(实例化中缓存bean对象),并将之放入临时容器。
  2. 循环完成,将临时容器中的BeanPostProcessor接口bean赋值到全局BeanPostProcessor接口列表中
  3. 再次循环bean信息列表,缓存存在则直接返回缓存对象,不存在则进行bean实例化,期间循环调用全局BeanPostProcessor接口对象方法

3. spring中BeanProcessor的源码解析

我们要从spring中的refresh()开始看起:

public void refresh() throws BeansException, IllegalStateException {
  synchronized (this.startupShutdownMonitor) {
   // Prepare this context for refreshing.
   //刷新准备
   prepareRefresh();

   // Tell the subclass to refresh the internal bean factory.
   //告诉子类刷新内部bean工厂。
   ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

   // Prepare the bean factory for use in this context.
   //为容器准备bean工程
   prepareBeanFactory(beanFactory);

   try {
     // Allows post-processing of the bean factory in context subclasses.
     //允许在上下文bean的后处理工厂子类。
     postProcessBeanFactory(beanFactory);

     // Invoke factory processors registered as beans in the context.
     //优先将BeanDefinitionRegistryPostProcessor\BeanFactoryPostProcessor接口的bean对象实例化
     //属于spring内部组件调用
     invokeBeanFactoryPostProcessors(beanFactory);

     // Register bean processors that intercept bean creation.
     //处理用户自定义PostProcessor接口对象,之后加入spring的beanPostProcessors列表,
     // 供之后预实例化其他bean时触发这些PostProcessor方法
     registerBeanPostProcessors(beanFactory);

		//...省略代码
    //实例化所有(non-lazy-init)单件。
		finishBeanFactoryInitialization(beanFactory);
   }

   catch (BeansException ex) {
     if (logger.isWarnEnabled()) {
      logger.warn("Exception encountered during context initialization - " +
         "cancelling refresh attempt: " + ex);
     }

     // Destroy already created singletons to avoid dangling resources.
     //bean销毁
     destroyBeans();

     // Reset 'active' flag.
     //取消刷新
     cancelRefresh(ex);

     // Propagate exception to caller.
     throw ex;
   }

   finally {
     // Reset common introspection caches in Spring's core, since we
     // might not ever need metadata for singleton beans anymore...
     //重置公共缓存
     resetCommonCaches();
   }
  }
}

其中包含有postProcess字段都有可能和BeanProcessor相关,这里有三个相关方法:

  1. postProcessBeanFactory(beanFactory),这个是一共空的扩展方法,显然无关
  2. invokeBeanFactoryPostProcessors(beanFactory),处理spring中实现了BeanProcessor接口的内部组件直接调用接口方法
  3. registerBeanPostProcessors(beanFactory),实例化用户自定义BeanProcessor接口bean组件,之后循环赋值到全局BeanProcessor列表中

所以registerBeanPostProcessors()就是我们要找的对象,来跟进看下registerBeanPostProcessors():

//AbstractApplicationContext#registerBeanPostProcessors
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
  //委托给PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理
	PostProcessorRegistrationDelegate.registerBeanPostProcessors进行处理(beanFactory, this);
}
public static void registerBeanPostProcessors(
   ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

  //查询实现了BeanPostProcessor接口的beanName
  String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

  // Register BeanPostProcessorChecker that logs an info message when
  // a bean is created during BeanPostProcessor instantiation, i.e. when
  // a bean is not eligible for getting processed by all BeanPostProcessors.
  int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
  beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

  // Separate between BeanPostProcessors that implement PriorityOrdered,
  // Ordered, and the rest.
  List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
  List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
  List<String> orderedPostProcessorNames = new ArrayList<>();
  List<String> nonOrderedPostProcessorNames = new ArrayList<>();
  //根据beanName循环调用getBean进行实例化
  for (String ppName : postProcessorNames) {
   if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
     BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
     priorityOrderedPostProcessors.add(pp);
     if (pp instanceof MergedBeanDefinitionPostProcessor) {
      internalPostProcessors.add(pp);
     }
   }
   else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
     orderedPostProcessorNames.add(ppName);
   }
   else {
     nonOrderedPostProcessorNames.add(ppName);
   }
  }

  // First, register the BeanPostProcessors that implement PriorityOrdered.
  //对BeanPostProcessor接口对象进行排序
  sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
  //将获取到的PostProcessors接口对象加入到spring的beanPostProcessors列表
  registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

  // Next, register the BeanPostProcessors that implement Ordered.
  List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
  for (String ppName : orderedPostProcessorNames) {
   BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
   orderedPostProcessors.add(pp);
   if (pp instanceof MergedBeanDefinitionPostProcessor) {
     internalPostProcessors.add(pp);
   }
  }
  sortPostProcessors(orderedPostProcessors, beanFactory);
  registerBeanPostProcessors(beanFactory, orderedPostProcessors);

  // Now, register all regular BeanPostProcessors.
  List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
  for (String ppName : nonOrderedPostProcessorNames) {
   BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
   nonOrderedPostProcessors.add(pp);
   if (pp instanceof MergedBeanDefinitionPostProcessor) {
     internalPostProcessors.add(pp);
   }
  }
  registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

  // Finally, re-register all internal BeanPostProcessors.
  sortPostProcessors(internalPostProcessors, beanFactory);
  registerBeanPostProcessors(beanFactory, internalPostProcessors);

  // Re-register post-processor for detecting inner beans as ApplicationListeners,
  // moving it to the end of the processor chain (for picking up proxies etc).
  beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

果然这里就是处理BeanPostProcessor接口的地方,逻辑和之前的思路类似:

  1. 循环扫描到的bean列表,获取实现了BeanPostProcessor接口的beanName数组
  2. 循环beanName数组数组,调用beanFactory.getBean()将bean实例化,并放入priorityOrderedPostProcessors列表中
  3. 调用sortPostProcessors对priorityOrderedPostProcessors列表进行排序(处理BeanPostProcessor调用的顺序)
  4. 调用registerBeanPostProcessors将priorityOrderedPostProcessors列表中的bean对象赋值到全局列表beanPostProcessors中
  5. 回到refresh()中,当调用finishBeanFactoryInitialization()对所用bean进行预实例化时就会调用这些BeanPostProcessor接口方法

以上就是spring BeanProcessor接口详解的详细内容,更多关于spring BeanProcessor接口的资料请关注我们其它相关文章!

(0)

相关推荐

  • 基于注解实现 SpringBoot 接口防刷的方法

    该示例项目通过自定义注解,实现接口访问次数控制,从而实现接口防刷功能,项目结构如下: 一.编写注解类 AccessLimit package cn.mygweb.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Targ

  • springboot运行时新增/更新外部接口的实现方法

    最近有个需求:需要让现有springboot项目可以加载外部的jar包实现新增.更新接口逻辑.本着拿来主义的思维网上找了半天没有找到类似的东西,唯一有点相似的还是spring-loaded但是这个东西据我网上了解有如下缺点: 1.使用java agent启动,个人倾向于直接使用pom依赖的方式 2.不支持新增字段,新增方法,估计也不支持mybatis的xml加载那些吧,没了解过 3.只适合在开发环境IDE中使用,没法生产使用 无奈之下,我只能自己实现一个了,我需要实现的功能如下 1.加载外部扩展

  • Spring Boot接口限流的常用算法及特点

    前言 在一个高并发系统中对流量的把控是非常重要的,当巨大的流量直接请求到我们的服务器上没多久就可能造成接口不可用,不处理的话甚至会造成整个应用不可用. 那么何为限流呢?顾名思义,限流就是限制流量,就像你宽带包了1个G的流量,用完了就没了.通过限流,我们可以很好地控制系统的qps,从而达到保护系统的目的.本篇文章将会介绍一下常用的限流算法以及他们各自的特点. 算法介绍 计数器法 计数器法是限流算法里最简单也是最容易实现的一种算法.比如我们规定,对于A接口来说,我们1分钟的访问次数不能超过100个.

  • Spring的BeanFactoryPostProcessor接口示例代码详解

    接口简介 BeanFactoryPostProcessor 接口是 Spring 初始化 BeanFactory 时对外暴露的扩展点,Spring IoC 容器允许 BeanFactoryPostProcessor 在容器实例化任何 bean 之前读取 bean 的定义,并可以修改它. BeanDefinitionRegistryPostProcessor 继承自 BeanFactoryPostProcessor,比 BeanFactoryPostProcessor 具有更高的优先级,主要用来在

  • 详解Springboot集成sentinel实现接口限流入门

    Sentinel是阿里巴巴开源的限流器熔断器,并且带有可视化操作界面. 在日常开发中,限流功能时常被使用,用于对某些接口进行限流熔断,譬如限制单位时间内接口访问次数:或者按照某种规则进行限流,如限制ip的单位时间访问次数等. 之前我们已经讲过接口限流的工具类ratelimter可以实现令牌桶的限流,很明显sentinel的功能更为全面和完善.来看一下sentinel的简介: https://github.com/spring-cloud-incubator/spring-cloud-alibab

  • SpringBoot 防止接口恶意多次请求的操作

    前言 刚写代码不就,还不能做深层次安全措施,今天研究了一下基本的防止接口多次恶意请求的方法. 思路 1:设置同一IP,一个时间段内允许访问的最大次数 2:记录所有IP单位时间内访问的次数 3:将所有被限制IP存到存储器 4:通过IP过滤访问请求 该demo只有后台Java代码,没有前端 代码 首先是获取IP的工具类 public class Ipsettings { public static String getRemoteHost(HttpServletRequest request) {

  • SpringBoot实现接口幂等性的4种方案

    一.什么是幂等性 幂等是一个数学与计算机学概念,在数学中某一元运算为幂等时,其作用在任一元素两次后会和其作用一次的结果相同. 在计算机中编程中,一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.幂等函数或幂等方法是指可以使用相同参数重复执行,并能获得相同结果的函数.这些函数不会影响系统状态,也不用担心重复执行会对系统造成改变. 二.什么是接口幂等性 在HTTP/1.1中,对幂等性进行了定义.它描述了一次和多次请求某一个资源对于资源本身应该具有同样的结果(网络超时等问题除外),

  • Spring Boot 接口参数加密解密的实现方法

    因为有小伙伴刚好问到这个问题,松哥就抽空撸一篇文章和大家聊聊这个话题. 加密解密本身并不是难事,问题是在何时去处理?定义一个过滤器,将请求和响应分别拦截下来进行处理也是一个办法,这种方式虽然粗暴,但是灵活,因为可以拿到一手的请求参数和响应数据.不过 SpringMVC 中给我们提供了 ResponseBodyAdvice 和 RequestBodyAdvice,利用这两个工具可以对请求和响应进行预处理,非常方便. 所以今天这篇文章有两个目的: 分享参数/响应加解密的思路. 分享 Response

  • 使用SpringBoot跨系统调用接口的方案

    一.简介 项目开发中存在系统之间互调问题,又不想用dubbo,这里提供几种springboot方案: 1.使用Feign进行消费(推荐) 2.使用原始httpClient请求 3.使用RestTemplate方法 二.方案 方案一:使用Feign进行消费(推荐) 1.在maven中添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-st

  • spring BeanProcessor接口详解

    1. 简单认识BeanProcessor BeanProcessor的理解 BeanProcessor是spring中的一个重要接口,他有两个接口方法一个是postProcessBeforeInitialization前置初始化,另一个是postProcessAfterInitialization后置初始化.从名称上就可以大概清楚这个接口的作用:在一个业务流程的前后加入两个接口方法,当执行这个业务流程时,就会触发这两个接口方法的执行.简单的总结一下有两个要点: 在业务流程中,根据BeanProc

  • spring的几个重要类和接口(详解)

    1. datasource接口是javax.sql包下的接口,不是spring,是javax.sql下的 datasource接口有个重要的方法getConnection()方法 Connection getConnection(String username, String password) throws SQLException; 那些spring支持的数据库连接池,都是实现了Datasource接口 比如下面是阿里的DruidDatasource数据库连接池源码,它就是实现了dataso

  • Spring IOC原理详解

    最近,买了本Spring入门书:springInAction.大致浏览了下感觉还不错.就是入门了点.Manning的书还是不错的,我虽然不像哪些只看Manning书的人那样专注于Manning,但怀着崇敬的心情和激情通览了一遍.又一次接受了IOC.DI.AOP等Spring核心概念.先就IOC和DI谈一点我的看法. IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通

  • spring缓存代码详解

    本文研究的主要是spring缓存的相关内容,具体介绍如下. 这篇文章是根据谷歌翻译大致修改出来的,由于原文不知道是什么语,所以可能导致翻译的有错误和不准确的地方,但是大致的方向感觉还是蛮不错的,所以在这里整理了一下,希望能够有所帮助. 高速缓存一直是一个非常需要这两个提高应用程序性能并降低其工作量.此外,它的用处今天是特别明显,可以作出处理成千上万的游客concurrents.D'un架构上的Web应用,高速缓存管理正交于应用程序的业务逻辑和出于这个原因,应该对应用程序本身的发展产生的影响最小.

  • Java中Spring扩展点详解

    目录 如何在所有Bean创建完后做扩展 方式一 方式二 Spring通过initPropertySources扩展方法设置环境配置 @Import进行扩展 如何在所有Bean创建完后做扩展 方式一 Spring在容器刷新完成后会注册ContextRefreshedEvent. 所以可以自定义事件监听器监听该事件进行扩展. 监听器实现: @Component public class ContextRefreshedEventListener implements ApplicationListe

  • Docker(黑马spring cloud笔记)详解

    目录 介绍和安装 安装 启动 镜像加速 Docker基本操作 镜像操作 容器操作 数据卷操作 Dockerfile 镜像结构 Dockerfile Docker-Compose 安装 基本命令 Docker 私 服搭建 练习 练习1:save和load 练习2:docker部署nginx 练习3:部署MySQL 练习4:基于Ubuntu构建一个新镜像,运行一个java项目 练习5:部署微服务集群 问题:微服务注册失败 介绍和安装 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从

  • Spring组件自动扫描详解及实例代码

    Spring组件自动扫描详解及实例代码 问题描述 一个系统往往有成千上万的组件,如果需要手动将所有组件都纳入spring容器中管理,是一个浩大的工程. 解决方案 Spring 提供组件扫描(component scanning)功能.它能从classpath里自动扫描.侦测和实例化具有特定注解的组件.基本的注解是@Component,它标识一个受Spring管理的组件.其他特定的注解有@Repository.@Service和@Controller,它们分别标识了持久层.服务处和表现层的组件.

  • JSP 中spring事务配置详解

    JSP 中spring事务配置详解 前几天被问到,如何防止服务器宕机,造成的数据操作的不完全. 问了一下同事,是事务.哎,恍然大悟,迷糊一时了. 声明式的事务配置,这个是最推荐的,配置到service层. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context=&

  • 类似Object监视器方法的Condition接口(详解)

    在<基于线程.并发的基本概念(详解)>中,我们利用synchronized关键字.Queue队列.以及Object监视器方法实现了生产者消费者,介绍了有关线程的一些基本概念.Object类提供的wait的方法和notifyAll方法,与之对应的是Condition接口提供是await和signalAll.await(或wait)是让当前线程进入等待状态并释放锁,signalAll(或notifyAll)则是唤醒等待中的线程,使得等待中的线程有竞争锁的资格,注意只是资格,并不代表被唤醒的线程就一

  • Spring集成Redis详解代码示例

    本文章从头开始介绍Spring集成Redis的示例. Eclipse工程结构 如下图为我的示例工程的结构图,采用Maven构建.其中需要集成Spring,因此需要beans.xml文件配置spring的依赖注入,redis.properties配置连接服务器的配置信息. 其中工程中beans.xml和redis.properties文件直接放在了根目录,有需要的读者可以放到resource目录中. POM依赖 如下为示例POM依赖,Spring集成redis需要依赖的包为:jedis包,spri

随机推荐