Spring Beginner Tutorial (iii) Injection and self-assembly

Source: Internet
Author: User


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

Two commonly used 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)" is described. Not to 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.O Rg/schema/beans/spring-beans.xsd "> <!--the following is SetPoint injection. Note that <property name= "Injectiondao" ref= "Injectiondao" ></property> This line of code, whether it is the set value here or the following constructs must be in need to reference the object Class with a corresponding method for use in the spring container, the actual development of two only need to choose one can <!--<bean id= "Injectionservice" class= "Com.xidian.ioc.inject Ion.service.InjectionServiceImpl ">--><!--<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, assuming in the preceding 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 Received:" + arg); arg = arg + ":" + This.hashcode ();//spring The image will take its own initiative to generate the corresponding object Injectiondao.save (ARG);}   }


Own active assembly (autowiring):

ByName: Self-assembling according to the attribute name.

Bytype: Suppose there is a bean in the container that has the same name as the attribute, so that it is actively assembled. But. Assume that there are multiple. An exception is thrown, assuming no exception is reported if it is not found. But not doing anything.

Constructor: Similar to the bytype approach, except that it applies to constructor parameters, assuming that the container does not find a bean that is consistent with the constructor type. Then an exception is thrown.

Self-injection, the assembly of related operations into the detailed class, to be passed in the corresponding method (the method can not be less), detailed look at the code gaze:


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.O Rg/schema/beans/spring-beans.xsd "default-autowire=" constructor "> <!--own active injection configuration. can also have byname,bytype. ByName actually looks at the Bean's ID, while the bytype is the type. Suppose there are multiple identical type to report an exception, and in fact, when you inject yourself actively, the following bean injection configuration is omitted. Self-injection, the assembly of related operations into the detailed class, to be passed in the corresponding method. --<bean id= "Autowiringservice" class= "Com.xidian.autowiring.AutoWiringService" ></bean> <!--hypothesis is not to use their own initiative injection method, this side must be artificially set, for example this way: <bean id= "Autowiringservice" class= "Com.xid Ian.autowiring.AutoWiringService "> <property name=" Autowiringdao "ref=" autowiring "></property> </bean> <!--objects to be injected--<bean class= "Com.xidian.autowiring.AuTowiringdao "></bean> </beans>



2, to their own initiative injection class:

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



3. Injection class:

Package com.xidian.autowiring;//The class is configured to the corresponding configuration file where Spring-autowiring.xmlpublic class Autowiringservice {private Autowiringdao autowiringdao;//assumes that the method is actively injected using the construction method, and this method must be written on this side. Although the settings in the Spring config file bean are waived, this is not the way to save the front. Public Autowiringservice (Autowiringdao Autowiringdao) {System.out.println ("Autowiringservice"); This.autowiringdao = Autowiringdao;} Ditto. Must have a method to pass in the Autowiringdao object.

The settings in the spring configuration file bean are waived in the front although. But this is not going to 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





Spring Beginner Tutorial (iii) Injection and self-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.