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:

@Configuration
public class SpringConfig {

    @Bean(name="poem")
    public Poem poem(){
        return new SimplePoem();
    }
}

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

<dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

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");
        poem.recite();
    }
}