Spring Config Bean

Source: Internet
Author: User

 A Bean is a class

    The code is parsed on the basis of the spring IOC and DI implementation case

  How the Bean is instantiated:

1. Non-parametric construction

<bean id= "UserService" class= "com.baidu.test.UserServiceImp" >        <property name= "name" value= "Zhang San" > </property>        </bean>

2. Static Factory method

Applicationcontext.xml

class= "Com.baidu.test.BeanFactory2" factory-method= "Createuserserviceimp" >              <property name= "name" value= "Zhang San" ></property>        </bean>

Creating the BeanFactory2 Class

public class BeanFactory2 {public static UserService Createuserserviceimp () {return new Userserviceimp ();}}

3. Instance Factory method

<bean name= "BeanFactory2" class= "Com.baidu.test.BeanFactory2" ></bean>       <bean id= "UserService" Factory-bean= "BeanFactory2" factory-method= "Createuserserviceimp" >        <property name= "name" value= "Zhang San" > </property>       </bean>

Creating the BeanFactory2 Class

public class BeanFactory2 {public  userservice Createuserserviceimp () {return new Userserviceimp ();}}

    Scope of the Bean

There is a scope in the Bean's properties to describe the scope of the bean.

Singleton Singleton spring IOC container with only one Bean real class (default scope)
Prototype A new instance is returned when multiple cases are fetched from the spring container
Request Store the Bean Object Request.setattribute () in the request domain
Session Store the Bean Object Session.setattribute () in the Session field

Common Singleton and prototype in development (singleton and multiple cases)

Bean's attribute injection

1. Construction Method Injection

Provides construction methods

Public Userserviceimp (String name) {super (); this.name = name;}

Modify the Applicationcontext.xml file

<bean id= "UserService" class= "com.baidu.test.UserServiceImp" >    <constructor-arg index= "0" value= "Zhang San" Type= "Java.lang.String" ></constructor-arg>
</bean>

2.Setter Injection

Modify Applicationcontext.xml

<bean id= "user" class= "com.baidu.test.User" >        <property name= "name" value= "UUU" ></property>        <property name= "age" value= "></property></bean>        <bean id=" UserService "class=" Com.baidu.test.UserServiceImp ">        <property name=" name "value=" UU "></property>        <property Name= "U" ref= "user" ></property>// introduce another bean        </bean>

3.map Injection

 <bean id= "user" class= "com.baidu.test.User" > <property name= "NA          Me "value=" UUU "></property> <property name=" age "value=" ></property></bean> "        <bean id= "UserService" class= "com.baidu.test.UserServiceImp" > <property name= "name" value= "UU" > </property> <property name= "list" > <list> <value>10</value> < Value> Zhang San </value> <ref bean= "user"/> </list> </property> <propert Y name= "set" > <set> <value> Zhang San </value> <ref bean= "user"/> </SET&G        T </property> <property name= "map" > <map> <entry key= "1" value-ref= "user" ></ Entry> <entry key= "2" value= "></entry>" </map> </property> </b Ean> 

 Space names P and C

Introduction of Schma

      Xmlns:c= "http://www.springframework.org/schema/c" xmlns:p= "http://www.springframework.org/schema/p"
First they are not real namespaces, they are virtual. It is embedded in the spring kernel. Using the P-namespace can solve our setter injection <property> simplify     using C namespace can solve our constructor injection when <constructor-arg> simplify

Applicationcontext.xml

P namespace---------Setter injection

<bean id= "user" class= "com.baidu.test.User" >        <property name= "name" value= "UUU" ></property>        p:u-ref= "user" >        </bean>

  

C namespace-------Constructor injection

<bean id= "UserService" class= "com.baidu.test.UserServiceImp" c:name= "UU" c:u-ref= "user" > </bean>

Spel (Spring expression Language) Spring 3.0 version

It is similar to the OGNL or El expression, which provides the ability to construct complex expressions to complete object property storage and method invocation when the program is run. Spel expression format #{expression}

Applicationcontext.xml

1. Complete the injection between the beans

<bean id= "user" class= "com.baidu.test.User" >        <property name= "name" value= "UUU" ></property>        <property name= "age" value= "></property></bean> <bean id=" UserService "class=" Com.baidu.test.UserServiceImp ">         <property name=" name "value=" UU "></property>
<!--<property name= "u" ref= "user" ></property>-> <property name= "u" value= "#{user}" > </property> overrides the above ref value (completes the injection between beans) </bean>

2. Support for property invocation and called method invocation

<bean id= "user" class= "com.baidu.test.User" >        <property name= "name" value= "UUU" ></property>        <property name= "age" value= "></property></bean><bean id=" user1 "class=" Com.baidu.test.User ">        <property name=" name "value=" UUU "></property>        <property name=" age "Value=" #{user.getage () +5} "></property></bean> <bean id=" UserService "class=" Com.baidu.test.UserServiceImp ">         <property name=" name "value=" UU "></property>        <property Name= "U" value= "#{user1}" ></property>        </bean>

  

  

Spring Config Bean

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.