Using Spring’s Java based configuration is fairly straightforward and more flexible. In order to do it, you will have to annotate your java class, that is going to contain your beans, with @Configuration and as we have added the component-scan in our one post here. It will also register the beans annotated with certain stereotype annotations. For example, let’s configure the SimplePoem in our previous post and register the SimplePoem bean through spring’s java config like:

public class SpringConfig {

    public Poem poem(){
        return new SimplePoem();

And, you will have to include the following dependency for @Configuration to work:


Finally, you can load the application context like:

public class Main {
    public static void main(String... args){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Poem poem = (Poem)ctx.getBean("poem");