Home > Spring > Spring Autowired with Example

Spring Autowired with Example

Spring Autowired

Spring Autowired feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.Spring Autowire can’t be used to inject primitive and string values. It works with reference only.


Spring Autowired modes:

There are many autowiring modes:

No.ModeDescription
1)noIt is the default autowiring mode. It means no autowiring bydefault.
2)byNameThe byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
3)byTypeThe byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.
4)constructorThe constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
5)autodetectIt is deprecated since Spring 3.

Example of byName and byName:

<bean id="c2" class="beans.Class_2" autowire="byName"/>

We are using Ecplise IDE and some required jars to develop below application.

Ecplise IDE : Ecplise Luna

Jars: placed below jar in Referenced Libraries Folder

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE
  5. spring-expression-5.0.8.RELEASE

To create this example, we have created 4 files.

  1. Class_1.java
  2. Class_2.java
  3. spring.xml
  4. Client.java

Class_1.java

package beans;
public class Class_1 {
Class_1()
{
System.out.println("Class_1 is created");
}
public void print()
{
System.out.println("Print() from Class_1");
}
}

Class_2.java

package beans;
public class Class_2 {
private Class_1 c1;
Class_2()
{
System.out.println("Class_2 is created");
}
public Class_1 getC1() {
return c1;
}
public void setC1(Class_1 c1) {
this.c1 = c1;
}
public void print()
{
System.out.println("Print() from Class_2");
}
public void dipaly()
{
print();
c1.print();
}
}

spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="c1" class="beans.Class_1"/>
<bean id="c2" class="beans.Class_2" autowire="byName"/>
</beans>

Client.java

package client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Class_1;
import beans.Class_2;
final class Client {
public static void main(String[] args) {
ApplicationContext ap=new ClassPathXmlApplicationContext("resources/spring.xml");
Class_2 c2=(Class_2)ap.getBean("c2");
c2.dipaly();
}
}

Output:

Class_1 is created
Class_2 is created
Print() from Class_2
Print() from Class_1

Example of  byType module:

In case of byType autowiring mode, bean id and reference name may be different. But there must be only one bean of a type.It internally uses setter injection.

We need to mofify in spring.xml file as below instead of “byName”.

<bean id="c2" class="beans.Class_2" autowire="byType"/>

Related Topics:-

You may also like :-
Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

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