自动装配在 Bean class Spring 中不起作用
Autowired not working in Bean class Spring
我正在尝试创建一个 bean 并实例化 bean 属性以覆盖默认构造函数并使用环境对象 [org.springframework.core.env.Environment] 从 属性 文件中获取和分配属性。
下面是我的 属性 文件 [mi.properties]
mi.name=GB
mi.grade=13
下面是我的 Simple bean Class
public class School implements EnvironmentAware {
private String schoolName;
private int schoolGrade;
private int totalStudents;
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
public School() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}
//Getter Setters
}
下面是我的Java配置class
@Configuration
@PropertySource("classpath:/cross/mi.properties")
public class JavaConfig {
@Autowired
private Environment env;
@Bean
public School getSchool()
{
School obj = new School(env);
return obj;
}
}
这将正确创建 School bean。但是,当我尝试在 School bean 中自动装配环境时,它并没有创建 bean。
下面是我试过的
public class School implements EnvironmentAware {
private String schoolName;
private int schoolGrade;
private int totalStudents;
@Autowired
private Environment env;
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
public School() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}
//Getter Setters
}
和Java配置更改如下
@Configuration
@PropertySource("classpath:/cross/mi.properties")
@ComponentScan
public class JavaConfig {
@Bean
public School getSchool()
{
School obj = new School();
return obj;
}
}
这不是在 Spring 上下文中创建 School bean,当我调试时,覆盖的默认构造函数中的环境实例是 null 因此 getProperty() 方法失败。
我对此有点困惑。
根据我的理解,Spring 生命周期上下文将在构造函数调用之前自动装配所有 @autowired 属性。正确吗?
如果在 spring 生命周期中解析所有自动装配的属性时,如果上述陈述错误?
根据 JVM 体系结构,当 class 首次加载时,它会分配内存并为 class 的属性分配默认值。这是否意味着默认情况下,当 School class 加载时,其 属性 env 默认为空?
JavaConfig class 中的自动装配环境如何工作? Spring 如何自动装配此值以及在其生命周期的哪个阶段?
As suggested in some forums I have tried implementing EnvironmentAware interface in School class and added @Component annotation to School class.Still No result.
Spring 在执行构造函数并创建实例后使用反射注入 @Autowired
属性。这就是为什么您总是在构造函数中得到 null - class 的属性尚未由 Spring 设置。
您可以通过多种方式实现您的目标。一种是使用
afterPropertiesSet()
方法,如名称所示,在设置属性后执行 :) 您将拥有 env
我更喜欢的另一种解决方案是在构造函数中添加自动装配的 bean。有人称之为构造函数注入。
它将使用 Environment 的构造函数标记为像那样自动装配(或只有一个构造函数):
@Autowired
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
通过这种方式,您还可以将环境变量设置为最终变量(这是个好主意)。这将与其他人建议的@component 注释一起使用,以删除使用 new School()
手动创建 bean
查看此link了解更多
注入发生在构建之后,除非您明确使用构造函数注入:
@Autowired
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
另一种选择是使用 @PostConstruct 方法进行初始化:
@Autowired
private Environment env;
public School() {
}
@PostConstruct
public void initialise() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}
我正在尝试创建一个 bean 并实例化 bean 属性以覆盖默认构造函数并使用环境对象 [org.springframework.core.env.Environment] 从 属性 文件中获取和分配属性。
下面是我的 属性 文件 [mi.properties]
mi.name=GB
mi.grade=13
下面是我的 Simple bean Class
public class School implements EnvironmentAware {
private String schoolName;
private int schoolGrade;
private int totalStudents;
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
public School() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}
//Getter Setters
}
下面是我的Java配置class
@Configuration
@PropertySource("classpath:/cross/mi.properties")
public class JavaConfig {
@Autowired
private Environment env;
@Bean
public School getSchool()
{
School obj = new School(env);
return obj;
}
}
这将正确创建 School bean。但是,当我尝试在 School bean 中自动装配环境时,它并没有创建 bean。
下面是我试过的
public class School implements EnvironmentAware {
private String schoolName;
private int schoolGrade;
private int totalStudents;
@Autowired
private Environment env;
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
public School() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}
//Getter Setters
}
和Java配置更改如下
@Configuration
@PropertySource("classpath:/cross/mi.properties")
@ComponentScan
public class JavaConfig {
@Bean
public School getSchool()
{
School obj = new School();
return obj;
}
}
这不是在 Spring 上下文中创建 School bean,当我调试时,覆盖的默认构造函数中的环境实例是 null 因此 getProperty() 方法失败。
我对此有点困惑。
根据我的理解,Spring 生命周期上下文将在构造函数调用之前自动装配所有 @autowired 属性。正确吗?
如果在 spring 生命周期中解析所有自动装配的属性时,如果上述陈述错误?
根据 JVM 体系结构,当 class 首次加载时,它会分配内存并为 class 的属性分配默认值。这是否意味着默认情况下,当 School class 加载时,其 属性 env 默认为空?
JavaConfig class 中的自动装配环境如何工作? Spring 如何自动装配此值以及在其生命周期的哪个阶段?
As suggested in some forums I have tried implementing EnvironmentAware interface in School class and added @Component annotation to School class.Still No result.
Spring 在执行构造函数并创建实例后使用反射注入 @Autowired
属性。这就是为什么您总是在构造函数中得到 null - class 的属性尚未由 Spring 设置。
您可以通过多种方式实现您的目标。一种是使用
afterPropertiesSet()
方法,如名称所示,在设置属性后执行 :) 您将拥有 env
我更喜欢的另一种解决方案是在构造函数中添加自动装配的 bean。有人称之为构造函数注入。
它将使用 Environment 的构造函数标记为像那样自动装配(或只有一个构造函数):
@Autowired
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
通过这种方式,您还可以将环境变量设置为最终变量(这是个好主意)。这将与其他人建议的@component 注释一起使用,以删除使用 new School()
查看此link了解更多
注入发生在构建之后,除非您明确使用构造函数注入:
@Autowired
public School(Environment env) {
this.schoolName = env.getProperty("mi.name", "MT");
this.schoolGrade = env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = env.getProperty("mi.total", Integer.class, 1000);
}
另一种选择是使用 @PostConstruct 方法进行初始化:
@Autowired
private Environment env;
public School() {
}
@PostConstruct
public void initialise() {
this.schoolName = this.env.getProperty("mi.name", "MT");
this.schoolGrade = this.env.getProperty("mi.grade", Integer.class, 10);
this.totalStudents = this.env.getProperty("mi.total", Integer.class, 1000);
}