Tran Thien Chien
Tran Thien Chien

Reputation: 681

Java-based vs annotation-based configuration/autowiring

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?

Upvotes: 25

Views: 20718

Answers (5)

Shivi
Shivi

Reputation: 161

Java based:

-@Bean annotation is used for creating bean using *new* keyword.
-Developer is responsible for Bean creation and injecting to dependent bean.
e.g. 

    @Bean
    public Account saving(){
    return new Saving(); // creating new object using new keyword with @Bean 
      annotation
    }

//injecting saving account manually in user bean

    @Bean
    public User user(){
    return new User(saving());
    }


-Though it is called as java based configuration, it is also used annotations like @Configuration and @bean.

Annotation based:

-Stereotype annotation e.g. @componant , @Service, @Repository @Controller are used for bean creation. 
-@Autowired is used for dependency injection.

-Spring IOC is responsible for bean creation and dependency injection. 
-If we want to provide any external configuration like component scan we can use class with @configuration annotation which will override default configs.

e.g.

@Component
public class Saving implements Account{
// Required code
}

@Component
public class User {
@Autowired
private Account account; //only one implementation available [saving], so that will be injected;

// Required code
}

Upvotes: 0

Shadyar
Shadyar

Reputation: 845

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.

Upvotes: 18

Abhinav Vishak
Abhinav Vishak

Reputation: 371

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

Upvotes: 7

Vicky
Vicky

Reputation: 1225

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.

Upvotes: 5

user1717259
user1717259

Reputation: 2883

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

Upvotes: -1

Related Questions