Java Spring:如何在没有构造函数的情况下初始化属性?
我有一个配置了构造函数的旧类Java Spring:如何在没有构造函数的情况下初始化属性?,java,spring,Java,Spring,我有一个配置了构造函数的旧类 public class Outer { ... public Outer(OldService oldService) { this.oldService = oldService;} } 我需要用新服务添加新字段,但不能更改构造函数(太多的遗留代码依赖于它)。所以,我想得到这样的东西 public class Outer { private NewService newService; // Need injection here public
public class Outer
{
...
public Outer(OldService oldService) { this.oldService = oldService;}
}
我需要用新服务添加新字段,但不能更改构造函数(太多的遗留代码依赖于它)。所以,我想得到这样的东西
public class Outer
{
private NewService newService; // Need injection here
public Outer(OldService oldService) { this.oldService = oldService;}
}
@Component
public class NewService
{
public NewService(Dependency dependency){this.dependency = dependency;}
}
我已经尝试为Outer.newService应用@Autowired和@Inject属性,但没有帮助。我可以创建Outer.Initiate(NewService NewService)方法,但这将为已经闻到的项目添加一些s**t
那个么,我能在春天注入磁场吗
Upd1外部构造函数现在手动执行(如var Outer=new Outer(service);)。您可以使用字段级注入:
public class Outer{
@Autowired
private NewService newService;
public Outer(OldService oldService) { this.oldService = oldService;}
}
我不喜欢这种方法,因为它使测试更加繁琐,并且隐藏了依赖关系
与此相反,只需使用setter并使用Autowired
对其进行注释,其作用与此相同:
public class Outer{
private NewService newService;
public Outer(OldService oldService) { this.oldService = oldService;}
@Autowired
public setNewService(NewService newService){
this.newService = newService;
}
}
您可以使用字段级注入:
public class Outer{
@Autowired
private NewService newService;
public Outer(OldService oldService) { this.oldService = oldService;}
}
我不喜欢这种方法,因为它使测试更加繁琐,并且隐藏了依赖关系
与此相反,只需使用setter并使用Autowired
对其进行注释,其作用与此相同:
public class Outer{
private NewService newService;
public Outer(OldService oldService) { this.oldService = oldService;}
@Autowired
public setNewService(NewService newService){
this.newService = newService;
}
}
由于Outer.class不是由Spring容器实例化的,而是由
new
实例化的,因此Spring容器无法知道它。这就是为什么Spring不能为NewService
执行依赖项注入
现在,如果您将
Outer
实例化给Spring,除了自动连接NewService
,您还需要自动连接或使用任何Spring连接将OldService
连接到Outer
,因为Outer.class不是由Spring容器实例化的,而是由new
实例化的,Spring容器无法识别它。这就是为什么Spring不能为NewService
执行依赖项注入
现在,如果您将
Outer
的实例化交给Spring,除了自动连接NewService
,您还需要自动连接或使用任何Spring连接将OldService
连接到Outer
,如果您有大量的遗留代码直接调用旧构造函数,您希望遗留代码如何填充newService
字段?您的Outer
类需要是Spring组件。如果没有,自动连接将不起作用。如果您想要一个新的必需的NewService
,您是否也要中断旧代码。为什么不使用构造函数(OldService OldService,NewService NewService){super(OldService);}
?扩展类Outer
像NewOuter
一样?我已尝试为类的字段和组件应用自动连线-NewService为空。John,我想SpringDI容器将填充newService,即使手动调用构造函数也是如此。我错了吗?不,这样不行,因为您正在创建一个实例,而没有使用Springbean。您应该像这样创建一个@Bean
:@Bean public Outer(){return new Outer(oldService);}
,然后您可以使用@Autowired来获取该Bean,剩下的由Spring来完成。如果您有很多遗留代码直接调用旧构造函数,您希望遗留代码如何填充newService
字段?您的Outer
类需要是Spring组件。如果没有,自动连接将不起作用。如果您想要一个新的必需的NewService
,您是否也要中断旧代码。为什么不使用构造函数(OldService OldService,NewService NewService){super(OldService);}
?扩展类Outer
像NewOuter
一样?我已尝试为类的字段和组件应用自动连线-NewService为空。John,我想SpringDI容器将填充newService,即使手动调用构造函数也是如此。我错了吗?不,这样不行,因为您正在创建一个实例,而没有使用Springbean。您应该像这样创建一个@Bean
:@Bean public Outer(){return new Outer(oldService);}
然后您可以使用@Autowired获取该Bean,其余的由Spring完成。