Spring Autowire By Type

By | | Updated : 2021-05-09 | Viewed : 26 times

Spring Autowire By Type

The current tutorial will exposure for Spring Autowire By Type with example. Let\'s dive into this tutorial to learn how to apply Spring Autowire By Type.

Spring Autowire By Type

Spring supports spring autowire by type for dependency injection. Using this Spring automatically wire the dependencies into dependent bean by dependency type.

Spring Autowire By Type Xml

Let\'s code for spring autowire by type. So please look at these below-given code blocks for the same.

ASpringBean.java
public class ASpringBean {

    private String aSpringBeanName;

    private BSpringBean bSpringBean;
	
	//other methods
}
BSpringBean.java
public class BSpringBean {

    private String bSpringBeanName;
	//other methods
}

Now we will configure these two beans in xml. Notice below given xml snippet.

beans.xml
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="aSpringBean" class="com.docsconsole.tutorials.bytype.xml.ASpringBean" autowire="byType">
		<property name="aSpringBeanName" value="aSpringBean"></property>
	</bean>

	<bean id="bSpringBean" class="com.docsconsole.tutorials.bytype.xml.BSpringBean" >
		<property name="bSpringBeanName" value="bSpringBean"></property>
	</bean>

</beans>

When you create the ASpringBean then BSpringBean will be identified by type and instantiated for dependency injection.

SpringMainClient.java
public class SpringMainClient {

	public static void main(String[] args) {
		

		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

		ASpringBean aSpringBean = context.getBean("aSpringBean", ASpringBean.class);

		aSpringBean.displayASpringBean();
		aSpringBean.getbSpringBean().displayBSpringBean();
		
	}

}

Use this Client for testing the spring autowire by type.

Spring Autowire By Type Annotation

Now we will try the spring autowire by type with annotations. let\'s get started with coding for the same.

ASpringBean.java
@Getter
@Setter
public class ASpringBean {

    private String aSpringBeanName;

    @Autowired
    private BSpringBean bSpringBean;
	
	//other methods
}
BSpringBean.java
@Getter
@Setter
public class BSpringBean {

    private String bSpringBeanName;
	//other methods
}

We will set up config for both beans as given below configuration class.

SpringAppConfig.java
@Configuration
public class SpringAppConfig {

    @Bean
    public ASpringBean aSpringBean() {
        return new ASpringBean("ASpringBean");
    }

    @Bean
    public BSpringBean bSpringBean() {
        return new BSpringBean("BSpringBean");
    }

}

In the time of instantiating ASpringBean Spring create the bean for BSpringBean and inject as a dependency into the ASpringBean.

SpringAppClient.java
public class SpringAppClient {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(SpringAppConfig.class);
        context.registerShutdownHook();

        //
        ASpringBean aSpringBean = context.getBean(ASpringBean.class);
        aSpringBean.displayASpringBean();
        aSpringBean.getBSpringBean().displayBSpringBean();


    }
}

Please use the client class for testing the spring autowire by type. Please refer GitHub repo Spring-Autowiring-Example-App

Leave A Reply