gpt4 book ai didi

java - AnnotationConfigApplicationContext@4c0bc4 尚未刷新

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:08 32 4
gpt4 key购买 nike

我正在获取以下代码的堆栈跟踪,

public interface SequenceDAO {

public Sequence getSequence(String sequenceId);

public int getNextValue(String sequenceId);
}

````````````````````````````````````````````

public class Sequence {
private int initial;
private String prefix;
private String suffix;

public Sequence(int initial, String prefix, String suffix) {
this.initial = initial;
this.prefix = prefix;
this.suffix = suffix;
}

``````````````````````````````````````````````````

@Component("SequenceDAO")
public class SequenceDAOImpl implements SequenceDAO {

private Map<String, Sequence> sequences;
private Map<String, Integer> values;

public SequenceDAOImpl() {
sequences = new HashMap<>();
sequences.put("IT", new Sequence(30, "IT", "A"));
values = new HashMap<>();
values.put("IT", 10000);
}

@Override
public Sequence getSequence(String sequenceId) {
return sequences.get(sequenceId);
}

@Override
public int getNextValue(String sequenceId) {
int value = values.get(sequenceId);
values.put(sequenceId, value + 1);
return value;
}

}

````````````````````````````````````````````````

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.example");
SequenceDAO obj = context.getBean("SequenceDAO", SequenceDAO.class);
System.out.println(obj.getNextValue("IT"));
System.out.println(obj.getSequence("IT"));
}

````````````````````````````````````````````````````````````

Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@4c0bc4 has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1041)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1059)
at com.example.SpringAnnotationsSequenceGeneratorWithDaoIntroductionApplication.main(SpringAnnotationsSequenceGeneratorWithDaoIntroductionApplication.java:14)

我是 Spring 新手,我正在学习没有注释的 Spring,所以如果有人可以告诉我这里发生了什么问题

如有任何帮助,敬请谅解。

问候

最佳答案

你的上下文init应该像这样:

ApplicationContext aContext = new AnnotationConfigApplicationContext(ConcertConfig.class);
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {

}

关于java - AnnotationConfigApplicationContext@4c0bc4 尚未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540350/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com