2016-07-23 6 views
1

I-Stack-Trace für folgenden Code bin immer,AnnotationConfigApplicationContext @ 4c0bc4 wurde noch nicht aktualisiert

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.spring[email protected]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) 

Iam neu zu springen und ich lerne Frühling ohne Anmerkungen, also wenn jemand kann mir sagen, was falsch hier passiert

jede Hilfe ist apperitiert.

Beat-Grüße

+1

Sie haben vergessen, 'context.refresh()' nach dem 'context.scan'. –

+0

Danke @ M.Deinum eigentlich lerne ich von Buch (Apress.Spring.3.Edition.), Die das folgende Beispiel haben, aber 'context.refresh()' wird dort nicht erwähnt. Danke nochmal für Hilfe –

Antwort

0

Ihr Kontext init sollte dies mag:

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

} 
Verwandte Themen