Spring Autowire using annotation Example

@Autowire annotation

  • You can Marks a constructor, field, setter method or config method as to be autowired by Spring’s dependency injection facilities.
  • Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.
  • Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.
  • In the case of multiple argument methods, the ‘required’ parameter is applicable for all arguments.
    In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value
    type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean
    names.

Project Structure

Point.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.spy.javaforlearn;
 
public class Point 
{
 
	int x;
	int y;
 
	public int getX() 
	{
		return x;
	}
 
	public void setX(int x) 
	{
		this.x = x;
	}
 
	public int getY() 
	{
		return y;
	}
 
	public void setY(int y) 
	{
		this.y = y;
	}
}

Circle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spy.javaforlearn;
 
import org.springframework.beans.factory.annotation.Autowired;
 
public class Circle 
{
 
	Point center;
 
	public Point getCenter() 
	{
		return center;
	}
 
        @Autowired
	public void setCenter(Point center) 
	{
		this.center = center;
	}
 
 
	public void draw() 
	{
 
		System.out.println("circle drawn");
		System.out.println("("+center.getX()+","+center.getY()+")");
 
	}
 
}

Spring8.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
   <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="c" class="com.spy.javaforlearn.Circle" >
   </bean> 
 
 
   <bean id="center" class="com.spy.javaforlearn.Point">
   <property name="x" value="0"></property>
   <property name="y" value="0"></property>
   </bean>    
 
 
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
 
  </beans>

Testmain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.spy.javaforlearn;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestMain
{
 
	public static void main(String[] args) 
	{
 
		ApplicationContext context=new ClassPathXmlApplicationContext("spring8.xml");
 
		Circle c=(Circle)context.getBean("c");
 
		c.draw();
 
	}
 
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *