Method of attribute injection in Spring (practical application)

Source: Internet
Author: User

Spring's control inversion and attribute injection characteristics can greatly help us manage the objects we need.

Spring annotations and the use of the first part: The instantiation of the bean (declaring a bean), let the bean be given to spring management, which is what we call the IOC control reversal of the spring feature

Usually we use <bean/> tags to define, next there are three ways:

1. Generic Instance Bean

<id=class="com.test.mytest.testAction/>

2. Common Instantiation Interface

<id=class=/>

3. Create an instance factory (not recommended) through a static plant

<id=class=factory-method="getBean2"></ Bean>
public class Bean2factory {

Static method that returns the Bean2 object
public static Bean2 getBean2 () {
return new Bean2 ();
}

}

4. Using instance factory to create (and not recommended)

public class Bean3factory {

Normal method, return Bean3 object
Public Bean3 getBean3 () {
return new Bean3 ();
}
}

<id=class="cn.ywq.bean.Bean3Factory"></Beans>
<id=factory-bean=factory-method=" GetBean3 "></Bean>


Note: The bean design has some rules:

Common Properties for bean Tags:

(1) ID attribute: Specification ... Class name first letter lowercase:
(2) Class attribute: Fully qualified name (full path of the same class)

(3) Name property (not used by default): The id attribute value cannot contain a special symbol, as is the function and id attribute, but it can contain special symbols in the Name property value
(4) Scope property
-Singleton: Default value, single case
-Prototype: Multiple cases
-Request: Create object Put object in Request field
-session: Creating objects to place objects in the session field
-Globalsession: Create object put object in Globalsession
Part Two: Injection properties (let spring inject values into your class at initialization time), which is what we call Spring's second big feature: Dependency Injection DI

Introduction to attribute injection (three ways)
(1) using the Set method to inject
(2) using parametric construction injection
(3) using interface injection

1) The first method is injected using the Set method, and the standard class must satisfy the Setter,getter method, because the class setter method that is used by the spring's Di bottom layer, so the property of the class injected by this method must implement the Setter method.

1.1 Normal attribute Injection

<id="testaction
      "class=" Com.test.mytest.testAction ">
    <name= value=  />
    <name=value="test"/>
</ Bean>

<id="testservice
      "class=" Com.test.mytest.service.impl.testServiceImpl ">
    <name=  value= "${test_url}"/>
    <name=value="${system_env}"/>
</Bean >

Readers please look at the former is an instance class, he adopted the instantiation of the use of direct injection, the latter is an interface instantiation using El expression injection.

1.2 Injected using reference method

id=class=>
    <name=ref=" UserService "/>
    <name=ref=" OrderService " />
</Bean>

Readers note that the reference method injected into UserService and OrderService on the upper interface must also be declared with the bean tag first. Before you can introduce

1.3 Injection of complex attributes (List,map type injection)

<  Bean   id=   "Testpropertybuilderfactory"   class=   "Com.test.testPropertyBuilderFactory"  > <  property   name=   "Builders"  > <  map  > <
  
   entry  
    key=  
    "user"  
    value-ref=  
    " Userpropertybuilder "/> < 
    entry  
    key=  
   " Order " strong>value-ref=  
    "Orderpropertybuilder" /> < 
    entry  
    key =  
    "Product"  
    value-ref=  
    "Productpropertybuilder" /> & lt;/
    Map  > </
    Property  > </
    bean ; 
  
<  Bean   id=   "Orderinterceptorproxycreator"     class=   " Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator " > <  property   name=   "Interceptornames"  > <  list  > & Lt  value  >orderinterceptor</ value  > </ list  > </  Property  > <,  Property   name=   "Beannames"  > <  list  > <  value  >orderservice</ Value> <  value  >userservice</ value ; 
        </list>
    </property >
</beans>

The above is the injection of complex objects.

Annotations and use of spring

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.