Spring Bean Automatic assembly and annotation injection

Source: Internet
Author: User
Tags constructor xmlns
Problem Description

If a bean needs to inject a lot of attributes, each of the properties will show an injection, which can be cumbersome. Solution Solutions

Let the IOC container automatically specify a reference for the bean. Method---Automatic assembly in XML configuration file

Car.java

Package Com.zzj.bean;

public class Car {public
	void start () {
		System.out.println ("Starting Car ...");}
}

User.java

Package Com.zzj.bean;

public class User {
	private car car;
	
	public void Setcar (car car) {
		This.car = car;
	}

	public void Startcar () {
		car.start ();
	}
}
Configuration file

<?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.xsd ">
           
       <bean id= "car" class= "Com.zzj.bean.Car" ></bean>
       <bean id= "user" class= "Com.zzj.bean.User" Autowire= "ByName" ></bean>
</beans>
Test
public static void Main (string[] args) {
		ApplicationContext context = new Classpathxmlapplicationcontext (" Applicationcontext.xml ");
		User user = (user) Context.getbean ("user");
		User.startcar ();
	}

Note: To let the spring container automatically assemble the bean, you need to provide a reference, which is the Bean's Autowire property.

Automatic assembly is available in 3 ways:

1. Automatic assembly by name (ByName). Because the name of the bean in the spring container is unique, there is no ambiguity and it is recommended.

2. Automatic assembly according to type (Bytype). The spring container may find multiple beans of the same type as the bean that needs to be injected, so there is ambiguity, and the spring container does not know which bean to inject, and throws an exception.

3. Automatic assembly according to the constructor (constructor). This approach is more complex and does not make analysis. Method Two---Automatic assembly using annotations

It's OK to modify the User.java file and the XML configuration file.

User.java

Package Com.zzj.bean;

Import org.springframework.beans.factory.annotation.Autowired;

public class User {
	@Autowired
	private car car;

	public void Startcar () {
		car.start ();
	}
}
Note: The setter method can be omitted at this time.


XML configuration file

<?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:context= "http://www.springframework.org/ Schema/context "
       xsi:schemalocation=" Http://www.springframework.org/schema/beans/ 
           http Www.springframework.org/schema/beans/spring-beans.xsd
           Http://www.springframework.org/schema/context 
         Http://www.springframework.org/schema/context/spring-context.xsd ">
           
       <bean id=" car "class=" Com.zzj.bean.Car "></bean>
       <bean id=" user "class=" com.zzj.bean.User "></bean>
       < Context:annotation-config/>
</beans>
To use the annotation injection feature of spring IOC, you need to introduce a new namespace.
Note: @Autowired is a spring-brought annotation, or you can use Java Native annotations: @Resource. It is recommended to use @resource, which is more portable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.