Javaee--spring_ioc02

Source: Internet
Author: User

Three ways to instantiate a bean

  1, using the non-parametric construction of the class to create (emphasis)

<bean id= "User" class= "class full path" ></bean>

An exception is reported if the class does not have a parameterless construct

2. Create with Static factory

(1) Creating a static method in the factory class returns the class object

public class userfactory{

public static User GetUser () {

return new User ();

}

}

(2) Create an object in the spring configuration file

<bean id= "user" class= "factory class full path" factory-method= "GetUser" ></bean>

3. Create with Instance factory

(1) Create a method that is not static returns a class object

          public class userfactory{

Public User GetUser () {

return new User ();

}

}

(2)

<bean id= "Userfactory" class= "Full path to factory class" ></bean>

<bean id= "user" factory-bean= "userfactory" factory-method= "GetUser" ></bean>

Second, the bean label commonly used properties

    ID attribute: Name, can be arbitrarily named, cannot use special symbol, according to ID value can get configuration object

Class Property: The full path of the class in which the object is created

Name property: As with the ID property, special symbols can be used

Scope Property:

value:Singleton: Singleton (default value )

Prototype: multiple examples of

Request:spring create an object put it in the request domain

Session:spring create an object and put it in the session field

Globalsession: Application in Porlet environment, if there is no porlet environment, then the equivalent session

Third, attribute injection

    The first type: using the Set method to inject

public class user{

private String name;

public void SetName (String name) {

This.name=name;

}

}

The second type: using a parametric construction injection

public class user{

private String name;

User (String name) {

This.name=name;

}

}

The third type: interface injection

public interface userdao{

public void DeleteUser ();

}

          Public Userdaoimp implements userdao{

private String name;

public void DeleteUser (String name) {

This.name=name;

...

}

}

Only the first two types of injections are supported in spring

1. Set method injection configuration (emphasis)

<bean id= "User" class= "class full path" >

<property name= "Property name" Value= "Property value" ></property>

</bean>

2. Using a parametric structure injection

<bean id= "User" class= "class full path" >

<constructor-arg name= "Property name" Value= "Property value" ></constructor-arg>

</bean>

3. Inject the attributes of the object type (to inject DAO into the service as an example)

(1) Use DAO as attribute in service

(2) Set method to generate a DAO Type property

(3) <.bean id= "userdao" class= "DAO's full path" ></bean>

<bean id= "UserService" class= "full path to service" >

<property name= "Property name" ref= "Userdao" ></property>

</bean>

4, p name space injection

Add to constraint: <bean

xmlns:p= "http://www.springframework.org/schema/P"

>

<bean id= "User" class= "class full path" P: Property name = "attribute value"></bean>

5. Complex attribute Injection

Injected array type:

< Bean id= "user" class= "full Path to Class" >

<property name= "Property name" >

<list>

<value> attribute Values </value>

<value> attribute Values </value>

</list>

</property>

</bean>

Inject list type

         <bean id= "User" class= "class full path" >

<property name= "property name" >

<list>

<value> attribute Values </value>

<value> attribute Values </value>

</list>

</property>

</bean>

Inject map type

         <bean id= "User" class= "class full path" >

<property name= "property name" >

<map>

<entry key= "key" value= "value" ></entry>

<entry key= "key" value= "value" ></entry>

</map>

</property>

</bean>

Injection Properties Type

         <bean id= "User" class= "class full path" >

<property name= "property name" >

<props>

<prop key= "Key" > Value </prop>

<prop key= "Key" > Value </prop>

</props>

</property>

</bean>

Iv. differences between the IOC and DI

      (1) IOC: Control reversal, give the object to spring configuration

(2) Di: Dependency Injection: Setting a value to a property inside a class

(3) Relationship: Dependency injection cannot exist alone and needs to be done on an IOC basis

Javaee--spring_ioc02

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.