Spring Bean Learn to create and use < two >

Source: Internet
Author: User

transferred from: http://blessht.iteye.com/blog/1162131In the normal Java development, programmers in a class need to rely on other classes of methods, it is usually a new dependent class to invoke the class instance method, the problem is that the new class instance is not a unified management, spring put forward the idea of dependency injection, that is, the dependency class is not instantiated by the programmer, Instead, the spring container helps us to specify the instance and inject the instance into the class that needs the object. Another argument for dependency injection is "inversion of control", the popular understanding is that we are usually the new instance, the control of this instance is our programmer, and control inversion means that the new instance work is not done by our programmers but by the spring container. Spring has several forms of dependency injection, and the following is a description of how Spring uses XML for IOC configuration:
    • Set injection
This is the simplest way to inject, assuming that there is a springaction, the class needs to instantiate a Springdao object, then you can define a private Springdao member variable, Then create the Springdao set method (which is the input entry for the IOC): Java code
  1. Package com.bless.springdemo.action;
  2. Public class Springaction {
  3. //Inject object Springdao
  4. private Springdao Springdao;
  5. //Be sure to write the set method of the injected object
  6. public void Setspringdao (Springdao springdao) {
  7. This.springdao = Springdao;
  8. }
  9. public void ok () {
  10. Springdao.ok ();
  11. }
  12. }
The name attribute in the XML file,<bean>, which is then written in spring, is an alias for the Class property, which refers to the full name of the classes, because there is a public property Springdao in Springaction, so the <bean > Create a <property> tag in the label to specify Springdao. The name in the <property> tag is the Springdao attribute name in the Springaction class, and ref refers to the following <bean name= "Springdao" ...; This is in fact the spring to instantiate the Springdaoimpl object and call Springaction's Setspringdao method to inject Springdao: Java code
    1. <!--configuration Bean, which is configured with spring management
    2. <bean name="springaction" class="com.bless.springdemo.action.SpringAction" >
    3. <!--(1) Dependency injection, configuring the corresponding property in the current class --
    4. <property name="Springdao" ref="Springdao" ></property>
    5. </bean>
    6. <bean name="Springdao" class="Com.bless.springdemo.dao.impl.SpringDaoImpl" ></bean>
    • Constructor injection
This method of injection refers to the constructor injection with parameters, see the following example, I created two member variables Springdao and user, but did not set the object's set method, so it cannot support the first injection method, The injection method here is injected into the springaction constructor, meaning that the Springdao and user two parameter values are passed in when the Springaction object is created: Java code
  1. Public class Springaction {
  2. //Inject object Springdao
  3. private Springdao Springdao;
  4. Private User User;
  5. Public springaction (Springdao springdao,user User) {
  6. This.springdao = Springdao;
  7. this.user = user;
  8. System.out.println ("construct method calls Springdao and user");
  9. }
  10. public Void Save () {
  11. User.setname ("Kaka");
  12. Springdao.save (user);
  13. }
  14. }
In the XML file is also not in the form of <property>, but using the <constructor-arg> tag, the ref attribute also points to the name attribute of the other <bean> tag: XML code
  1. <!--configuration Bean, which is configured with spring management
  2. <Bean name= "springaction" class="Com.bless.springdemo.action.SpringAction">
  3. <!--(2) to create a constructor injection, if the main class has a constructor that has parameters, you need to add this configuration --
  4. <constructor-arg ref="Springdao"></constructor-arg>
  5. <constructor-arg ref="user"></constructor-arg>
  6. </Bean>
  7. <Bean name= "Springdao" class="Com.bless.springdemo.dao.impl.SpringDaoImpl"> </Bean>
  8. <Bean name="user" class="Com.bless.springdemo.vo.User"></ Bean>
To solve the uncertainty of the constructor method parameters, you may encounter the constructor method passed in the two parameters are the same type, in order to distinguish which to assign the corresponding value, you need to do some small processing: The following is set index, is the parameter location: XML code
  1. <Bean name= "springaction" class="com.bless.springdemo.action.SpringAction">
  2. <constructor-arg index="0" ref="Springdao"></constructor-arg >
  3. <constructor-arg index="1" ref="user"></constructor-arg>
  4. </Bean>
The other is to set the parameter type: XML code
    1. <constructor-arg type="java.lang.String" ref= ""/>
    • Method injection of static factory
Static factory as the name implies, is to call the static factory method to obtain their own objects, in order to let spring management of all objects, we can not directly through the "Engineering class. Static method ()" To get the object, but still through the form of spring injection to get: Java code
  1. Package com.bless.springdemo.factory;
  2. Import Com.bless.springdemo.dao.FactoryDao;
  3. Import Com.bless.springdemo.dao.impl.FactoryDaoImpl;
  4. Import Com.bless.springdemo.dao.impl.StaticFacotryDaoImpl;
  5. Public class Daofactory {
  6. //Static factory
  7. public static final Factorydao Getstaticfactorydaoimpl () {
  8. return new Staticfacotrydaoimpl ();
  9. }
  10. }
Also look at the key class, where I need to inject a Factorydao object, which looks exactly like the first injection, but looking at the subsequent XML will find a big difference: Java code
  1. public class Springaction {
  2. //Inject object
  3. private Factorydao Staticfactorydao;
  4. public void Staticfactoryok () {
  5. Staticfactorydao.savefactory ();
  6. }
  7. the Set method of the//injected object
  8. public void Setstaticfactorydao (Factorydao staticfactorydao) {
  9. This.staticfactorydao = Staticfactorydao;
  10. }
  11. }
Spring's IOC configuration file, note that <bean name= "Staticfactorydao" > points to the class is not a Factorydao implementation class, but instead points to the static factory daofactory, and configures Factory-method= "Getstaticfactorydaoimpl" specifies which factory method to call: XML code
  1. <!--configuration Bean, which is configured with spring management
  2. <Bean name= "springaction" class="Com.bless.springdemo.action.SpringAction" >
  3. <!--(3) Use the static factory method to inject the object, corresponding to the following configuration file (3) --
  4. < name= "Staticfactorydao" ref="Staticfactorydao"></Property >
  5. </Property>
  6. </Bean>
  7. <!--(3) The way to get objects here is to get the static method from the factory class--
  8. <Bean name= "Staticfactorydao" class="Com.bless.springdemo.factory.DaoFactory " factory-method="Getstaticfactorydaoimpl"></beans>
    • Method injection of the instance factory
The instance factory means that the method of getting an object instance is not static, so you need to first new factory class and then call the normal instance method: Java code
    1. Public class Daofactory {
    2. //Instance factory
    3. Public Factorydao Getfactorydaoimpl () {
    4. return new Factorydaoimpl ();
    5. }
    6. }
So the following class has nothing to say, similar to the previous one, but we need to create a Factorydao object from the instance factory class: Java code
  1. Public class Springaction {
  2. //Inject object
  3. private Factorydao Factorydao;
  4. public void Factoryok () {
  5. Factorydao.savefactory ();
  6. }
  7. public void Setfactorydao (Factorydao factorydao) {
  8. This.factorydao = Factorydao;
  9. }
  10. }
Last look at spring configuration file: XML code
  1. <!--configuration Bean, which is configured with spring management
  2. <Bean name= "springaction" class="Com.bless.springdemo.action.SpringAction">
  3. <!--(4) Use the method of the instance factory to inject the object, corresponding to the following configuration file (4) --
  4. < name= "Factorydao" ref="Factorydao"> </Property>
  5. </Bean>
  6. <!--(4) The way to get an object here is to get the instance method from the factory class--
  7. <Bean name= "daofactory" class="Com.bless.springdemo.factory.DaoFactory"> </Bean>
  8. <Bean name="Factorydao" factory-bean="daofactory" factory-method=" Getfactorydaoimpl "></bean>
    • Summarize
The Spring IOC injection method uses the most of (1) (2) species, thanks to more skilled will be very proficient. Also note: Objects created by spring are singleton by default, and if you need to create a multi-instance object, you can add a property after the <bean> tag: Java code
    1. <bean name="..." class= "..." scope="prototype" >

Spring Bean Learn to create and use < two >

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.