Spring 不使用自动装配的构造函数来加载 bean
Spring does not use autowired constructor for loading the bean
我已将 Address
bean 自动装配到 Employee
bean 的构造函数中。并期望在获取 Employee
bean 的实例时,我应该在其中获取 Address
的实例。但是 Spring 容器正在使用 Employee
的无参数构造函数到 return 实例。下面是我的代码
public class Address {
public void print(){
System.out.println("inside address");
}
}
public class Employee {
private Address address;
@Autowired
public Employee(Address address){
this.address = address;
}
public Employee(){}
public Address getAddress(){
return address;
}
}
@Configuration
@ComponentScan(basePackages={"com.spring"})
public class ApplicationConfig {
@Bean
public Employee employee(){
return new Employee();
}
@Bean
public Address address(){
return new Address();
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Employee employee = (Employee)context.getBean("employee");
// Here add is null !!
Address add = employee.getAddress();
}
}
@Component
public class Employee{
...
}
这应该有效。
正在使用无参数构造函数,因为这是您正在调用的构造函数 (new Employee()
):
@Bean
public Employee employee() {
return new Employee();
}
由于您是手动创建 Employee
实例,而不是让 Spring 为您创建它,因此您还必须自己传递 Address
:
@Bean
public Employee employee() {
return new Employee(address());
}
请注意,多次调用 address()
实际上会 ,而不是多个新实例,如果您担心的话。
否则,替代方法是用 @Component
注释 Employee
,之后 Spring 将自动为您创建 bean 并连接到 Address
依赖项。您可以免费获得它,因为您打开了组件扫描(假设 Employee
在您正在扫描的包中)。如果你走这条路,你可以从你的配置 class 中删除 employee()
bean 定义,否则一个可能最终会覆盖另一个。
如果您使用 spring mvc 项目,您必须在 applicationContext.xml 中启用注释,然后您必须添加此注释 <context:annotation-config>
你可以使用 @Autowired ,如果你只使用 spring Ioc 只需在 Employee[= 上添加 @Component 18=] 和 地址 类
我已将 Address
bean 自动装配到 Employee
bean 的构造函数中。并期望在获取 Employee
bean 的实例时,我应该在其中获取 Address
的实例。但是 Spring 容器正在使用 Employee
的无参数构造函数到 return 实例。下面是我的代码
public class Address {
public void print(){
System.out.println("inside address");
}
}
public class Employee {
private Address address;
@Autowired
public Employee(Address address){
this.address = address;
}
public Employee(){}
public Address getAddress(){
return address;
}
}
@Configuration
@ComponentScan(basePackages={"com.spring"})
public class ApplicationConfig {
@Bean
public Employee employee(){
return new Employee();
}
@Bean
public Address address(){
return new Address();
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Employee employee = (Employee)context.getBean("employee");
// Here add is null !!
Address add = employee.getAddress();
}
}
@Component
public class Employee{
...
}
这应该有效。
正在使用无参数构造函数,因为这是您正在调用的构造函数 (new Employee()
):
@Bean
public Employee employee() {
return new Employee();
}
由于您是手动创建 Employee
实例,而不是让 Spring 为您创建它,因此您还必须自己传递 Address
:
@Bean
public Employee employee() {
return new Employee(address());
}
请注意,多次调用 address()
实际上会
否则,替代方法是用 @Component
注释 Employee
,之后 Spring 将自动为您创建 bean 并连接到 Address
依赖项。您可以免费获得它,因为您打开了组件扫描(假设 Employee
在您正在扫描的包中)。如果你走这条路,你可以从你的配置 class 中删除 employee()
bean 定义,否则一个可能最终会覆盖另一个。
如果您使用 spring mvc 项目,您必须在 applicationContext.xml 中启用注释,然后您必须添加此注释 <context:annotation-config>
你可以使用 @Autowired ,如果你只使用 spring Ioc 只需在 Employee[= 上添加 @Component 18=] 和 地址 类