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?
Using Java based configuration allows you to write your Spring configuration without using XML. These annotations will be explained to you with working example using Eclipse IDE. Some of these annotations are: @Configuration and @Bean annotations.
@Configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime. If you look at the @Configuration class, you will see that it is meta-annotated with @Component .
What is Spring Annotation Based Configuration? In Spring Framework annotation-based configuration instead of using XML for describing the bean wiring, you have the choice to move the bean configuration into component class. It is done by using annotations on the relevant class, method or the field declaration.
From my own experience annotations better than xml configuration. I think in any case you can override xmls and use annotations. Also Spring 4 give us a huge support for annotations, we can override security from xml to annotations e.t.c, so we will have not 100 lines xml but 10 lines Java Code.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With