22

While i'm working with Spring framework, i often see 2 terminology java-based and annotation-based configuration/autowiring.

Is Java-based different with annotation-based configuration/autowiring or they are one?

If they are different, can you tell me what is the different between them?

Tran Thien Chien
  • 643
  • 2
  • 6
  • 17
  • Possible duplicate of [Spring: @Component versus @Bean](http://stackoverflow.com/questions/10604298/spring-component-versus-bean) – Andrew Tobilko Jan 12 '17 at 14:32

4 Answers4

16

Java based Configuration:

The official Spring documentation refers to configuring your beans using a Java class annotated with @Configuration and containing @Bean methods as 'Java Configuration'. This allows you to be absolutely free of all XML in your application (at least as far as Spring goes). This support was added in Spring 3.0, and has gotten more powerful.

Source

In other words, there is no configuration file required. If everything is fine with your application.

Annotation based Configuration:

Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

Source

In other words, there are XML configuration files yet but bean wiring, is configured using annotations.
Note: Annotation injection is performed before XML injection. Thus, the latter configuration will override the former for properties wired through both approaches.
Note: Annotation wiring is not turned on in the Spring container by default. So, we can use annotation-based wiring, we will need to enable it in our Spring configuration file.

Shadyar
  • 709
  • 8
  • 16
7

They are similar, but have subtle differences.

Instead of having an @Component annotation on your class( which is annotation-based configuration ), you can skip the @Component and instead have a @Bean annotated method which returns a new instance of this class.( this is Java-based configuration).

For the simplest of applications, it doesn't make a difference but it affects stuff like "Dynamic subclassing during lookup method injection" where if you had the @Lookup annotation on a abstract method, Spring automatically does magic and overrides this abstract method to return a @Component annotated bean.

Spring cannot do this automatic subclassing for methods which reference @Bean beans. In this case, you need to manually override the method using your own subclass.

You can find basic code examples in the Spring reference. https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-lookup-method-injection

spongecaptain
  • 121
  • 1
  • 11
Abhinav Vishak
  • 361
  • 1
  • 5
  • 17
3

Here is the difference between annotation based vs java based configuration, may be it will help you to find the answer.

1.Xml based configuration :

a) In xml based configuration you can define you bean definition/dependency-injections/auto-wiring inside a xml file.

b)If you want to give only the bean definitions inside your XML file and for rest, you want to use annotations then you have to define context:annotation-config in your XML file.

c)If you want to use only annotations for all the things then you have to define context:component-scan in your XML file.

2.Java based configuration: If you want to use a java based configuration then you have to use @Configuration annotation over your class which you will use to load the container.Now for the bean definition, you can do with in two.

a)In your configuration class @Bean annotations over the factory methods which you give you your bean objects by using the new keyword.

example-

 @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }

b) @Component/@Service/@Repository/@Controller/@RestController over the class which you want to act as a bean.

Community
  • 1
  • 1
Vicky
  • 1,135
  • 1
  • 17
  • 37
  • When using Java configuration, the @Component annotations become unnecessary. Is it really advisable to mix Java configuration and component scanning? – John O'Conner Dec 04 '17 at 19:55
  • When we use `@Bean` on a method, is there any way we can register a bean without `new` keyword? – Nisarg Patil Oct 24 '18 at 06:47
  • @JohnO'Conner, if you are using java based configuration still you have to tell spring container "which classes you want to instantiate by the container" and for that either you declare your classes by `@Component` or you can create an instance by yourself using `@Bean`.And by using component scanning you are telling your container which packages to look for the bean creation. i.e. it is needed while using java configuration. – Vicky Oct 25 '18 at 16:02
  • 1
    @NisargPatil, while using `@Bean` annotation you are telling spring container "you are creating the instance for it". i.e. in this case creation of instance is your responsiblity. – Vicky Oct 25 '18 at 16:06
  • @JohnO'Conner, when using '@Configuration' on a class, '@Component' annotation become unnecessary for that particular class. '@Configuration' is super set of '@Component'. But you need to annotate the classes with '@Component', which will be used inside '@Bean' annotated methods of '@Configuration' annotated class. This way Spring will be able to find them during Component-Scan. – Dexter Feb 04 '21 at 17:52
-1

There are two ways to configure Spring. One is to add annotations to your Java code. This is java-based, this is annotation-based, and it is autowiring. So java-based and annotation based are the same thing.

The other way is to configure Spring is by using an xml file.

Which to use is a matter of discussion Xml configuration versus Annotation based configuration

Community
  • 1
  • 1
user1717259
  • 2,717
  • 6
  • 30
  • 44