Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迁移期间部分使用Spring依赖项注入_Spring_Spring Boot_Dependency Injection_Migrate - Fatal编程技术网

迁移期间部分使用Spring依赖项注入

迁移期间部分使用Spring依赖项注入,spring,spring-boot,dependency-injection,migrate,Spring,Spring Boot,Dependency Injection,Migrate,我目前正在将一个现有项目迁移到Spring。我已经开始在一些松散耦合的新组件上使用依赖注入,但是项目本身非常混乱,紧密耦合,我不可能完全使用DI,我需要一步一步地更改代码 使用依赖项注入的“起点”是一个组件,它本身也有一些不能注入的依赖项,必须通过构造函数传递参见此示例 @Component public class FromHellToDependencyInjectionHeaven extends ComplicatedMessyBaseComponent{ @Autowired

我目前正在将一个现有项目迁移到Spring。我已经开始在一些松散耦合的新组件上使用依赖注入,但是项目本身非常混乱,紧密耦合,我不可能完全使用DI,我需要一步一步地更改代码

使用依赖项注入的“起点”是一个组件,它本身也有一些不能注入的依赖项,必须通过构造函数传递参见此示例

@Component
public class FromHellToDependencyInjectionHeaven extends ComplicatedMessyBaseComponent{

    @Autowired
    private InjectedComponentFromHeavenOne componentOne;

    @Autowired
    private InjectedComponentFromHeavenTwo componentTwo;

    private UninjectableMess uninjectableMess;

    /*
     * Constructor that has to stay for now because of the ComplicatedMessyBaseComponent
     * clashes with @Component
     */
    public FromHellToDependencyInjectionHeaven(MessyDependencyOne dependencyOne, 
    MessyDependencyTwo dependencyTwo, UninjectableMess uninjectableMess){
        super(dependencyOne,dependencyTwo);
        this.uninjectableMess = uninjectableMess;
    }

}
问题来了:

要使用@Autowired,使用依赖项的组件必须是@Service@component,否则我会

必须在有效的Springbean中定义自动连线成员 (@Component |@Service |…)

但是当我这样做的时候,我遇到了现有构造函数的复杂问题,我无法删除它

类不包含自动关联的匹配构造函数

一个想法是告诉Spring对这个类禁用构造函数依赖注入,但是我还没有找到任何可能的解决方案


如何在我的项目中部分使用依赖项注入?

删除
@Autowired
,只在构造函数中赋值(这甚至是执行依赖项注入的推荐方法)。springbean也可以是
@bean
方法的结果。@M.Deinum谢谢。在构造函数中分配它们是什么意思?据我所知,为了使用bean,我还需要DI定义一个
@bean
方法来指定从HellToDependencyInjectionHeaven为
注入什么/如何注入,和/或只添加
InjectedComponentFromHeavenOne
InjectedComponentFromHeavenTwo
作为构造函数参数。@M.Deinum理解。人工注射的一种方法。我的例子非常简单,假设存在6种不同的“InjectedComponentFromHeaveOne”,它们彼此之间有依赖关系,可以独立使用。这需要我为每个依赖项创建一个bean,并手动创建它的共同依赖项,这是相当大的开销。但是我会试试的,谢谢