Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Java 在Spring中,bean是先创建的还是先通过构造函数创建实例 @Autowired @限定符(“StringMatchedBasedAnswersSuggestion”) 私人建议评估人stringMatchBasedEval; 私人名单评估员; 公共应答建议服务IMPL(){ if(listEvaluators==null){ listEvaluators=新的ArrayList(); //要添加的所有其他对象。 添加(stringMatchBasedEval); Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority()); } }_Java_Spring - Fatal编程技术网

Java 在Spring中,bean是先创建的还是先通过构造函数创建实例 @Autowired @限定符(“StringMatchedBasedAnswersSuggestion”) 私人建议评估人stringMatchBasedEval; 私人名单评估员; 公共应答建议服务IMPL(){ if(listEvaluators==null){ listEvaluators=新的ArrayList(); //要添加的所有其他对象。 添加(stringMatchBasedEval); Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority()); } }

Java 在Spring中,bean是先创建的还是先通过构造函数创建实例 @Autowired @限定符(“StringMatchedBasedAnswersSuggestion”) 私人建议评估人stringMatchBasedEval; 私人名单评估员; 公共应答建议服务IMPL(){ if(listEvaluators==null){ listEvaluators=新的ArrayList(); //要添加的所有其他对象。 添加(stringMatchBasedEval); Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority()); } },java,spring,Java,Spring,在这种情况下,将首先执行构造函数中的代码,还是创建bean。stringMatchBasedEval是否为null?在注入方法中,为了注入bean,必须已经创建了注入对象。在此之后,您必须设置它的bean。我认为很明显,首先创建对象,然后注入bean 因此,首先执行构造函数,然后注入bean。在注入方法中,为了注入bean,必须已经创建了注入对象。在此之后,您必须设置它的bean。我认为很明显,首先创建对象,然后注入bean 因此,首先执行构造函数,然后注入bean。要向对象注入内容,sprin

在这种情况下,将首先执行构造函数中的代码,还是创建bean。stringMatchBasedEval是否为null?

在注入方法中,为了注入bean,必须已经创建了注入对象。在此之后,您必须设置它的bean。我认为很明显,首先创建对象,然后注入bean


因此,首先执行构造函数,然后注入bean。

在注入方法中,为了注入bean,必须已经创建了注入对象。在此之后,您必须设置它的bean。我认为很明显,首先创建对象,然后注入bean


因此,首先执行构造函数,然后注入bean。

要向对象注入内容,spring应该首先创建对象

您可以在您的案例中使用基于构造函数的注入:

@Autowired
    @Qualifier("stringMatchedBasedAnswerSuggestion")
    private SuggestionEvaluator stringMatchBasedEval;

    private List<SuggestionEvaluator> listEvaluators;


    public AnswerSuggestionServiceImpl() {
        if (listEvaluators == null) {
            listEvaluators = new ArrayList<SuggestionEvaluator>();
            // All the additional objects to be added.
            listEvaluators.add(stringMatchBasedEval);
            Collections.sort(listEvaluators, SuggestionEvaluator.compareByPriority());

        }
    }
@Autowired
公共应答建议服务Impl(@Qualifier(“StringMatchedBaseDanSwersSuggestion”)建议评估器StringMatchedBaseDeval){
if(listEvaluators==null){
listEvaluators=新的ArrayList();
//要添加的所有其他对象。
添加(stringMatchBasedEval);
Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority());
}
}

要向对象中注入一些东西,spring应该首先创建对象

您可以在您的案例中使用基于构造函数的注入:

@Autowired
    @Qualifier("stringMatchedBasedAnswerSuggestion")
    private SuggestionEvaluator stringMatchBasedEval;

    private List<SuggestionEvaluator> listEvaluators;


    public AnswerSuggestionServiceImpl() {
        if (listEvaluators == null) {
            listEvaluators = new ArrayList<SuggestionEvaluator>();
            // All the additional objects to be added.
            listEvaluators.add(stringMatchBasedEval);
            Collections.sort(listEvaluators, SuggestionEvaluator.compareByPriority());

        }
    }
@Autowired
公共应答建议服务Impl(@Qualifier(“StringMatchedBaseDanSwersSuggestion”)建议评估器StringMatchedBaseDeval){
if(listEvaluators==null){
listEvaluators=新的ArrayList();
//要添加的所有其他对象。
添加(stringMatchBasedEval);
Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority());
}
}

构造函数将首先被调用,因此您的
stringMatchBasedEval
将在此时为空。这个问题非常普遍,有一个非常普遍的解决方案。通常,构造函数应该是空的,初始化逻辑应该移到单独的方法(通常称为
init()
)中,用
@PostConstruct
注释标记该方法,Spring将在构造函数和所有注入完成后立即调用它。因此,您的
stringMatchBasedEval
将已经初始化

@Autowired
public AnswerSuggestionServiceImpl(@Qualifier("stringMatchedBasedAnswerSuggestion") SuggestionEvaluator stringMatchBasedEval) {
    if (listEvaluators == null) {
        listEvaluators = new ArrayList<SuggestionEvaluator>();
        // All the additional objects to be added.
        listEvaluators.add(stringMatchBasedEval);
        Collections.sort(listEvaluators, SuggestionEvaluator.compareByPriority());

    }
}
@Autowired
@限定符(“StringMatchedBasedAnswersSuggestion”)
私人建议评估人stringMatchBasedEval;
私人名单评估员;
公共应答建议服务IMPL(){
}
@施工后
私有void init(){
if(listEvaluators==null){
listEvaluators=新的ArrayList();
//要添加的所有其他对象。
添加(stringMatchBasedEval);
Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority());
}
}

构造函数将首先被调用,因此您的
stringMatchBasedEval
将在此时为空。这个问题非常普遍,有一个非常普遍的解决方案。通常,构造函数应该是空的,初始化逻辑应该移到单独的方法(通常称为
init()
)中,用
@PostConstruct
注释标记该方法,Spring将在构造函数和所有注入完成后立即调用它。因此,您的
stringMatchBasedEval
将已经初始化

@Autowired
public AnswerSuggestionServiceImpl(@Qualifier("stringMatchedBasedAnswerSuggestion") SuggestionEvaluator stringMatchBasedEval) {
    if (listEvaluators == null) {
        listEvaluators = new ArrayList<SuggestionEvaluator>();
        // All the additional objects to be added.
        listEvaluators.add(stringMatchBasedEval);
        Collections.sort(listEvaluators, SuggestionEvaluator.compareByPriority());

    }
}
@Autowired
@限定符(“StringMatchedBasedAnswersSuggestion”)
私人建议评估人stringMatchBasedEval;
私人名单评估员;
公共应答建议服务IMPL(){
}
@施工后
私有void init(){
if(listEvaluators==null){
listEvaluators=新的ArrayList();
//要添加的所有其他对象。
添加(stringMatchBasedEval);
Collections.sort(listEvaluators、SuggestionEvaluator.compareByPriority());
}
}

首先调用构造函数,然后注入bean。我敢打赌你得到了NPE.Craig Otis和SMA你的观点中有冲突。Anjava对构造函数的调用在对象实例中是隐式的,在newInstance()返回之前,无法对新实例执行任何操作。。。因此,不可能进行注入……首先调用构造函数,然后注入bean。我敢打赌你得到了NPE.Craig Otis和SMA你的观点中有冲突。Anjava对构造函数的调用在对象实例中是隐式的,在newInstance()返回之前,无法对新实例执行任何操作。。。因此,不可能进行注入…值得一提的是,具有@PostConstruct的方法可用于在注入所有内容后执行所需的任何逻辑…值得一提的是,具有@PostConstruct的方法可用于在注入所有内容后执行所需的任何逻辑…对于多个依赖项,这是不可伸缩的。。。我推荐@PostConstruct方法…对于多个依赖项,这是不可伸缩的。。。我推荐@PostConstruct方法。。。