Spring Introductory Tutorial (iii) injection and automatic assembly

Source: Internet
Author: User


Spring injection refers to the assignment of a class variable when the spring container is started to load the bean configuration.

Two common injection methods: SetPoint injection and construction injection


Here's a look at the code and the annotations in the code:


1, first look at the Spring configuration file ( How to load, how to test, "Introduction series (a)" There is a description, not repeat here )

<?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 "> <!--below is the setpoint injection, note <property name=" Injectiondao "ref=" Injectiondao "></property> This line of code, whether it is the set value here or the following constructs must be in the class that need to refer to the object to be added to the spring container use, real In the development of the two types only need to choose one <!--<bean id= "Injectionservice" class= "Com.xidian.ioc.injection.service.InjectionSer    Viceimpl ">--><!--<property name=" Injectiondao "ref=" Injectiondao "></property>--><!-- </bean>--><!--Here is the construction injection--><bean id= "Injectionservice" class= " Com.xidian.ioc.injection.service.InjectionServiceImpl "> <constructor-arg name=" Injectiondao "ref=" Injectiondao "></constructor-arg> </bean> <bean id=" InjecTiondao "class=" Com.xidian.ioc.injection.dao.InjectionDAOImpl "></bean> </beans> 

2, look at the injected class:

Package Com.xidian.ioc.injection.dao;public interface Injectiondao {public void Save (String arg);

Package Com.xidian.ioc.injection.dao;import Com.xidian.ioc.injection.dao.injectiondao;public Class Injectiondaoimpl implements Injectiondao {public void Save (String arg) {///Simulation Database save Operation SYSTEM.OUT.PRINTLN ("Save data:" + arg);}

3. Service class

Package Com.xidian.ioc.injection.service;public interface Injectionservice {public void Save (String arg);

Import Com.xidian.ioc.injection.dao.injectiondao;public class Injectionserviceimpl implements Injectionservice { Private Injectiondao injectiondao;//"important" constructor injection, this method must have, if in the previous spring context is the method of construction injection public Injectionserviceimpl ( Injectiondao injectionDAO1) {This.injectiondao = injectionDAO1;} "Important" Set value injection, this method must have, and the name must be setxxx, otherwise the spring container cannot be loaded public void Setinjectiondao (Injectiondao Injectiondao) { This.injectiondao = Injectiondao;} public void Save (String arg) {//Simulate business Operation SYSTEM.OUT.PRINTLN ("service receive parameter:" + arg); arg = arg + ":" + This.hashcode ();//spring The image is automatically tuned to generate the corresponding object Injectiondao.save (ARG);}   }


Automatic assembly (autowiring):

ByName: Automatically assembled according to the attribute name;

Bytype: If there is a bean with the same name in the container, it will be automatically assembled, but if there are more than one, an exception is thrown, and if not found, no exception is reported, but nothing is done;

Constructor: Similar to Bytype, except that it applies to constructor parameters and throws an exception if the container does not find a bean that is consistent with the constructor type.

Automatic injection, the relevant operation of the Assembly into a specific class, to be passed in the corresponding method (the method can not be less), the details of the code to see the comments:


1. Spring 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 "default-autowire=" constructor "> <!--from Byname,bytype can also be used for dynamic injection configuration.                          ByName actually looks at the Bean's ID, while the bytype is the type, and if there are multiple identical types, the exception is reported, in fact, when the automatic injection, the following bean injection configuration is omitted; Automatic injection, put the relevant operation of the Assembly into the specific class, to be passed in the corresponding method.                --<bean id= "Autowiringservice" class= "Com.xidian.autowiring.AutoWiringService" ></bean> <!--If it is not the automatic injection method, this side must be artificially set, such as this way: <bean id= "Autowiringservice" class= "Com.xidia        N.autowiring.autowiringservice "> <property name=" Autowiringdao "ref=" autowiring "></property> </bean> <!--objects to be injected--<bean class= "Com.xidian.autowiring.AutoWiringDAO" ></bean> </beans> 


2, to be automatically injected class:

Package com.xidian.autowiring;//The class is configured in the appropriate configuration file Spring-autowiring.xml, which is the object class public class Autowiringdao to be injected {public void Say (String word) {System.out.println ("Autowiringdao:" + word);}}



3. Injection class:

Package com.xidian.autowiring;//The class is configured in the appropriate configuration file Spring-autowiring.xmlpublic class Autowiringservice {private Autowiringdao autowiringdao;//If the method is automatically injected using the constructor method, this method must be written on this side. The front is exempt from the settings in the Spring config file bean, but this side can't save! Public Autowiringservice (Autowiringdao Autowiringdao) {System.out.println ("Autowiringservice"); This.autowiringdao = Autowiringdao;} Ditto. Must have a method to pass in the Autowiringdao object. The front is exempt from the settings in the Spring config file bean, but this side can't save! public void Setautowiringdao (Autowiringdao Autowiringdao) {System.out.println ("Setautowiringdao"); This.autowiringdao = Autowiringdao;} public void Say (String word) {This.autoWiringDAO.say (Word);}}



Did you see it? Welcome to discuss Http://blog.csdn.net/code_7





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Introductory Tutorial (iii) injection and automatic assembly

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.