@Autowired is used on Properties, which means Spring Container detects the bean instance using the byType process but injection is not with Setter method.This is how byType auto-wiring process is different from Annotation based auto-wiring. Hence annotation tells no need to write any setter method or constructor for injecting the bean property.

Example showing the Annotation on Properties :
There are two bean class A and Hello and Hello is having dependency of A.

A.java
public class A {  } 

Hello.java
public class Hello {
  
 @Autowired           //Annotation on properties
 A a;
public Hello(){ //Default Constructor System.out.println("Hello instance "); } public Hello(A a){ //Constructor Injection System.out.println("Arg Cons"); this.a = a; } public void setA(A a) { //Setter Injection this.a = a; } public static void main(String[] args) { ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringCore.xml"); Hello hello =(Hello)ctx.getBean("hello"); } }

SpringCore.xml
<bean id="a" class="com.def.A"> 
</bean>

<bean id="hello" class="com.def.Hello" >
</bean>

Output
Def A cons
Hello instance

In the above example
1. Instance of bean A will be created using default constructor.
2. Instance of bean Hello will be created using default constructor.
3. Bean property will be injected but without setter and constructor injection.

Note : In the above example output doesn't involve any constructor or setter injection hence proved that it follows different approach for injecting the bean property rather using Constructor or Setter Injection.

Note : Annotation on properties rather follows different approach to inject the bean property.


Related Posts:

  • Cyclic Dependency Cyclic dependency is the one of the major limitation of the Injection process in Spring. Constructor Injection has chance of having Cyclic Dependency, while the same is not possible in case of Setter Injection. When both… Read More
  • Annotation Based Autowiring in Spring IoC Auto-wiring in spring can also be achieved through Annotation. Annotation is the simplest way to achieve wiring in Spring as it doesn't involve any extra code in XML file. It can be parsed by the Spring container at the ti… Read More
  • Spring Wiring Process In Spring wiring means configuration the bean dependency if any. It may be done explicitly by hard-coding the code in XMl file or implicitly by mentioning the required type of autowiring process in the XML file. Autowiring… Read More
  • Spring IoC: Annotation on Properties @Autowired is used on Properties, which means Spring Container detects the bean instance using the byType process but injection is not with Setter method.This is how byType auto-wiring process is different from Annotation … Read More
  • Spring IoC : Annotation on Constructor @Autowired can also be used with Constructor by which Spring Container detects the bean instance using byType process but inject the bean property using Constructor Injection. Using @Autowired with the Constructor, there is… Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic