Spring Injection précis-writers

Source: Internet
Author: User

We know that objects are given to the container to manage.

Init () Destroy (): can be set in the bean configuration before the object is first identified and executed after it is destroyed

Int-delay= "" Indicates whether to defer instantiation of the container instance or when the bean is instantiated

The scope= "" Range property in the bean represents a singleton or multiple

Used to specify the scope of the bean

Singleton---Singleton creates only one object.

Prototype---The number of prototypes you want to create.

Request---Create a separate bean object for the Web project, with different requests, and share a single bean with the same request.

Session---Create separate bean objects for a Web project, different sessions, and share a bean with the same session.

One: Using XML injection configuration

1 Object injection <bean id= "" class= "" ></bean>

There are two ways to inject objects in class 2

2.1set Injection Bean

<bean ...>

<property name= "" ref= "corresponding Bean" >//The object reference corresponding to the attribute in the class requires a set () method;

</bean>

2.2 Constructor parameter injection bean ( class in which the constructor takes parameters to inject related content )

<bean...>

<constructor index=0 type= The injected type ref= the location of the injected bean/>//parameter, corresponding to the package name Class name, ID

<constructor .../>//Parameters if the basic type is directly used with index= ' value= ' "

3 injected is the base type, change ref to Value= ""

4 set injection has corresponding set tag as set List Pro map

<property name= "Set" >

<set>, ..... Different set assignments have differences (value value Pro:key entry:key/value ... </set>

</property>

Second, use annotation injection (@resource default by name not found by type @autowired default type)

First configure the XML document namespace, and add tags <context:annotatiion-config/> so implicitly register the interpreter that handles the parsing annotations, and add a note package.

1. Types of annotations Compare JDK supported annotations spring also has supported annotations that can be used by the JDK without relying on framework low-coupling annotations @resource

2. Annotations can be before a property or before the Set method. You can find beans in XML by type or by name. (It seems that the code is more concise and the XML configuration does not have to be nested configuration again )

The principle (presumably: first the annotations can define the existence cycle, such as the running period, you can annotate the label position such as the field properties on the method, by traversing all the beans, and then traverse all the attributes and methods in the bean, find the corresponding annotation and do the corresponding processing, and find the matching bean implementation injection into the XML file)

More than three are part of the manual assembly another is automatic assembly: (auotwire= "" There is a manual assembly by type annotation autowired somewhat similar)

(You only need to add attribute parameters to the bean, the code does not need to be processed)

In <bean id= "" class= "" autowire= "Bytype" ...>

Bytype: Multiple throw exceptions found by type, no injected null found

ByName: By name, not found is also null

Constructor: Injected bean by constructor parameter, no cannon exception found.

AutoDetect: Automatic detection If the default constructor is Bytype, the parameter constructor takes the constructor approach.

(I heard that implementing the same interface Bean will be treated as if the same type cannot be injected by type to be tested)

IV with automatic Scan injection: (This way is very good and strong)

1. First you need to add a namespace in the XML, and add <context:componet-scan basebage= "class under package including child package" >

A class that will scan the package under the class and have @service @repository @controller @conponent annotations. (seemingly just a sort of readability, with no actual difference between them)

2. The default bean name is called the first lowercase name of the class name.

3. Add annotations @scope ("prototype") on the class to modify the scope

4. On the initialization method, add

@postConstruct

Init ()

@preDestroy

Destroy ()

Spring Injection précis-writers

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.