Spring provides two types of configuration, XML based which came in market first and then Java based. Since we already know what are the basics behind Spring configuration, anything and everything is bean. So how does spring supports that.

Let’s see some code first.

@Configuration //1
public class AppConfig {
    @Bean  // 2
    public SomeService SomeService() {
        return new SomeServiceImpl();
    }

    @Bean(name = "bar")  // 3
    public SomeService SomeService() {
        return new SomeServiceImpl();
    }

    @Bean(initMethodName = "init") //4
    public Foo foo() {
        return new Foo();
    }
    @Bean(destroyMethodName="cleanup") //5
    public Bar bar() {
        return new Bar();
    }

    @Bean
    @Scope("prototype")  //6
    public Encryptor encryptor() {
        // ...
    }

}    

@Configuration annotation

This can be related to a XML file, not exactly though but it defines a class annotation with @Configuration will mainly be used to configured spring bean. Even a configuration class will have a singleton instance in spring container.

@Bean

@Bean mostly used over getter methods which returns a object of a class. This you can relate to tag of XML based configuration. Bean attribute with name property will override the object name and later you can ‘@Autowire’ it with ‘@qualifier(name=). initMethodName and destroyMethodName provides the feature what we use to have in Spring XML based configuration init-method and destroy-method.

@Scope

Scope attribute helps us to define the scope of the bean which have learnt in previous blog singleton, prototype, request, session, global and global session.

Pros of Java Based Configuration.

Pros of XML Based Configuration.