参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大

问题: 
spring的Configuration使用@Bean注册一个BeanFactoryPostProcessor Bean,发现使用@PropertySource,并注入@Resource private Environment env;发现env为null.我调试的大概一个过程,BeanFactoryPostProcessor Bean创建得比较早,创建它之前先创建它的依赖Bean Configuration,而这时发现创建的Configuration的env就是null了.深入的就没去追究了!

解决: 
让此Configuration类实现EnvironmentAware接口,这个接口只有一个void setEnvironment(Environment environment);方法.这里的回调能得到Environment,问题解决!

修改前的代码:

package org.exam.config;
import org.exam.service.TestBeanFactoryPostProcessor;
import org.exam.service.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import javax.annotation.Resource;
@Configuration
@ComponentScan(basePackages = {"org.exam.service"})
@PropertySource("classpath:config.properties")
public class AppConfigOld {
@Resource
private Environment env;
@Bean
public UserServiceImpl userService(){
System.out.println(env);
return new UserServiceImpl();
}
/*
未加入这个BeanFactoryPostProcessor一切都很正常,一旦加入这个@Bean,env就变为null
@Bean
public TestBeanFactoryPostProcessor testBeanFactoryPostProcessor(){
System.out.println(env);
return new TestBeanFactoryPostProcessor();
}
*/
}

修改后的代码:

package org.exam.config;
import org.exam.service.TestBeanFactoryPostProcessor;
import org.exam.service.UserServiceImpl;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@ComponentScan(basePackages = {"org.exam.service"})
@PropertySource("classpath:config.properties")
public class AppConfig implements EnvironmentAware {
private Environment env;
@Override
public void setEnvironment(Environment environment) {
this.env=environment;
}
@Bean
public UserServiceImpl userService(){
System.out.println(env);
return new UserServiceImpl();
}
@Bean
public TestBeanFactoryPostProcessor testBeanFactoryPostProcessor(){
System.out.println(env);
return new TestBeanFactoryPostProcessor();
}
}

值得一提的是:wilkinsona的回答是不建议使用ApplicationContext.虽然可以从ApplicationContext得到Environment,但是这样会在BeanFactoryPostProcessor的创建过程中会引起application context的所有依赖Bean都创建过早,这不是期望的一个结果.

参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大

问题: 
spring的Configuration使用@Bean注册一个BeanFactoryPostProcessor Bean,发现使用@PropertySource,并注入@Resource private Environment env;发现env为null.我调试的大概一个过程,BeanFactoryPostProcessor Bean创建得比较早,创建它之前先创建它的依赖Bean Configuration,而这时发现创建的Configuration的env就是null了.深入的就没去追究了!

解决: 
让此Configuration类实现EnvironmentAware接口,这个接口只有一个void setEnvironment(Environment environment);方法.这里的回调能得到Environment,问题解决!

修改前的代码:

package org.exam.config;
import org.exam.service.TestBeanFactoryPostProcessor;
import org.exam.service.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import javax.annotation.Resource;
@Configuration
@ComponentScan(basePackages = {"org.exam.service"})
@PropertySource("classpath:config.properties")
public class AppConfigOld {
@Resource
private Environment env;
@Bean
public UserServiceImpl userService(){
System.out.println(env);
return new UserServiceImpl();
}
/*
未加入这个BeanFactoryPostProcessor一切都很正常,一旦加入这个@Bean,env就变为null
@Bean
public TestBeanFactoryPostProcessor testBeanFactoryPostProcessor(){
System.out.println(env);
return new TestBeanFactoryPostProcessor();
}
*/
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

修改后的代码:

package org.exam.config;
import org.exam.service.TestBeanFactoryPostProcessor;
import org.exam.service.UserServiceImpl;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@ComponentScan(basePackages = {"org.exam.service"})
@PropertySource("classpath:config.properties")
public class AppConfig implements EnvironmentAware {
private Environment env;
@Override
public void setEnvironment(Environment environment) {
this.env=environment;
}
@Bean
public UserServiceImpl userService(){
System.out.println(env);
return new UserServiceImpl();
}
@Bean
public TestBeanFactoryPostProcessor testBeanFactoryPostProcessor(){
System.out.println(env);
return new TestBeanFactoryPostProcessor();
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

值得一提的是:wilkinsona的回答是不建议使用ApplicationContext.虽然可以从ApplicationContext得到Environment,但是这样会在BeanFactoryPostProcessor的创建过程中会引起application context的所有依赖Bean都创建过早,这不是期望的一个结果.

Sping Environment为Null的原因和解决方法的更多相关文章

  1. PHP解码Json(json_decode)字符串返回NULL的原因及解决方法(转载)

    本文主要为大家讲解了php在使用json_decode函数解码json字符串时,解码不成功返回NULL的问题原因分析和解决方法,感兴趣的同学参考下. 一般来说,php对json字符串解码使用json_ ...

  2. sharepoint 2010项目中,ashx页面获取SPContext.Current 为null的原因和解决方法

    //错误的写法 public void ProcessRequest(HttpContext context) { SPSecurity.RunWithElevatedPrivileges(deleg ...

  3. php数组使用json_encode函数中文被编码成null的原因和解决办法

    大写的囧,提客户处理问题,前端的APP一直在叽叽咂咂,说收到的值是null,弄了半天原来是这个问题,记录下吧 json格式在开发中用的十分广泛.在php中json_encode函数可以直接将数组转成 ...

  4. Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  5. oracle 索引失效原因及解决方法

    oracle 索引失效原因及解决方法 2010年11月26日 星期五 17:10 一.以下的方法会引起索引失效 ‍1,<>2,单独的>,<,(有时会用到,有时不会)3,like ...

  6. Java并发编程:Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  7. "ORA-00942: 表或视图不存在 "的原因和解决方法

    "ORA-00942: 表或视图不存在 "的原因和解决方法   采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或 ...

  8. 【转】Java ConcurrentModificationException异常原因和解决方法

    原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 ...

  9. jsp出现getOutputStream() has already been called for this response异常的原因和解决方法

    jsp出现getOutputStream() has already been called for this response异常的原因和解决方法 在tomcat5下jsp中出现此错误一般都是在js ...

随机推荐

  1. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  2. 证明tmult_ok的正确性

    csapp page124. practice problem 2.35 /* Determine whether arguments can be multiplied without overfl ...

  3. Socket编程注意接收缓冲区大小

    转自:http://www.cnblogs.com/ITBread/p/3900254.html 最近在做一个udp升级程序,因文件有点大,需要将程序分成多个包发送,每次发送一个包,收到回复后发送下一 ...

  4. C/C++面试题

    第一部分:基本概念及其它问答题 1.   关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量 ...

  5. Intent传递类实例

    发送方: Intent intent = new Intent(); intent.setClass(mContext, HomeDetailReportActivity.class); intent ...

  6. WINDOWS系统Eclipse+NDK+Android + OpenCv

    WINDOWS系统Eclipse+NDK+Android + OpenCv 参考文档博客 1 NDK环境搭建 http://jingyan.baidu.com/article/5d6edee22d90 ...

  7. codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树

    B2. Shave Beaver!   The Smart Beaver has recently designed and built an innovative nanotechnologic a ...

  8. linux下SVN忽略文件/文件夹的方法

    linux下SVN忽略文件/文件夹的方法 假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录) ...

  9. loadrunner中lr_save_searched_string函数的使用

    控制abc后面的第几个字符: 控制字符长度: 总结: 实际应用: Action() { int i =0,j=0; char *tt_url = lr_eval_string("{tt_ur ...

  10. JQ学习(三)-ajax

    jQuery - AJAX jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法 ...