Spring (ii) Assembly of __bean

Source: Internet
Author: User
Tags ming throw exception

Assembly of the Bean:

Piecing a bean inside a spring container is called assembly. When you assemble a bean, you need to tell the container which beans and how the container uses dependency injection to tie them together.

The root element of the context definition file is <beans>.<beans> has multiple <bean> child elements. Each <bean> element defines how a bean is assembled into a spring container.

<beans>

<bean id= "foo" class= "... Foo "/>

<bean id= "bar" class= "... Bar "/>

</beans>

The most basic configuration for a bean consists of the bean's ID and his full name Class name (the whole path of the class). The ID of the <bean id= "foo" class= "Com.xidian.Foo"/> Bean is foo.

Description of the Bean's scope:

Beans in spring are singleton mode by default. Always returns an instance. If you want to return different instances, you need to define the prototype pattern.

? Try to use the scope= "singleton", do not use prototype, because this has a greater impact on our performance.

The set method injects values into the bean.

(1) injecting dependence, referencing other beans

(2) Internal bean

This method is used less.

(3) Inheritance configuration

public class Student

public class Gradate extends Student

Configuring in the beans.xml file

<!-- Configure a Student object --

<bean id= "Student" class= "com.hsp.inherit.Student" >

<property name= "name" value= " Shunping "/>

<property name= "age" value= "/>"

</bean>

<!-- configuring grdate objects --

<bean id= "Grdate" parent= "Student" class= "Com.hsp.inherit.Gradate" >

<!-- If you configure property name,age yourself, the data that inherits from the parent object is replaced-

<!--<property name= "name" value= " Xiao Ming " /> --

<property name= "degree" value= " Bachelor "/>

</bean>

Special Note : The parent= "student" attribute should be the Bean ID name , not the class name student

1. Configure the Bean's simple properties , basic data type, and string

<bean id= "foo" class= "... Foo ">

<property name= "Name" >

<value>tom</value>

</property>

</bean>

2. Injecting values into the collection type

<!--inject values into the array --><property name= "EmpName" > <list> <value> xiaoming </value> <value> Ming Mingxiao </value> <value> Mingming </value> </list></property>
<!--inject a value into the list can have quite an object--><property name= "Emplist" > <list> <ref bean= "em P2 "/> <ref bean=" emp1 "/> <ref bean=" emp1 "/> </list></property>
<!--set injection value set cannot have the same object--><property name= "Empsets" > <set> <ref bean= "EMP1"/ > <ref bean= "emp2"/> <ref bean= "emp2"/> </set></property>
<!--map injection value map Only key is different, you can assemble value--><property name= "Empmaps" > <map> <entry key= "11" value-ref= "Emp1"/> <entry key= "" "value-ref=" emp2 "/> <entry key=" "value-ref= [Emp1]/> </map></property>
<!--configuring--><property name= "pp" > <props> <prop key= "PP1" for a collection of properties >abcd</prop& Gt <prop key= "pp2" >hello</prop> </props></property></bean>
<bean id= "EMP1" class= "Com.hsp.collection.Employee" ><property name= "name" value= "Beijing"/> <property name= "id" value= "1"/></bean><bean id= "EMP2" class= "Com.hsp.collection.Employee" ><property name= "name" value= "Tianjin"/><property name= "id" value= "2"/></bean></beans>

Second, the constructor function injection

Public Employee (String name, Integer age) {
THIS.name = name;
This.age = age;
}

<bean id= "Employee" class= "Com.hsp.constructor.Employee" >
<!--inject property values by constructor--
<constructor-arg index= "0" type= "java.lang.String" value= "Daming"/>
<constructor-arg index= "1" type= "Java.lang.Integer" value= "All"/>
</bean>

Note: Automatic boxing and unpacking is not supported in spring reflection mechanism. Type must be strictly specified.

String, integer such wrapper class can only be used type= "Java.lang.Integer" Can not be type= "Integer" or other.

Change the type of age in the constructor to the int basic data type, only type= "int" Cannot use type= "Java.lang.Integer"

Comparison:

The disadvantage of set injection is that it is not possible to clearly express which properties are required and which are optional, and the advantage of construct injection is that it is impossible to instantiate incomplete or unusable beans by constructing a forced dependency relationship.

Even so, the method of set injection is common.

Third, automatic assembly attribute value

Emphasis : Automatic assembly is automatically assembled according to the matching rules when the setting property value is not displayed. If it is already set, press the display settings.

<bean id= "foo" class= "Com.xidian.Foo" autowire= "Autowire type" >

1.byName looks for a bean with the same property name and cannot be found if it is not.

Be sure to Bean ID and property name settings are consistent

public class Master {

private String name;
Private dog dog;

class= "Com.hsp.autowire.Master" autowire= "byname" ><property name= "name" value= "xkj"/></bean ><!--Configuring the Dog object--><bean id= "dog"  class= "Com.hsp.autowire.Dog" ><property Name= "name" value= "Small yellow"/><property name= "Age" value= "3"/>

2.byType: Look for a bean with the same type of attribute, cannot find it, cannot fit, and finds multiple throw exceptions.

public class Master {

private String name;
Private dog dog;

class= "Com.hsp.autowire.Master" autowire= "Bytype" ><property name= "name" value= "xkj"/></bean ><!--Configure the Dog object--><bean id= "Dog2"  class= "Com.hsp.autowire.  Dog"><property name=" name "value=" Small yellow "/><property name=" Age "value=" 3 "/></bean>

3.constructor: Find one or more beans consistent with the bean's construction parameters, if not found or multiple, throw exception. Assemble according to the type of parameter

Autowire= "Constructor"

public class Master {

private String name;
Private dog Dog;
Constructor injected into dog
Public Master (dog dog) {
This.dog=dog;
}

4.autodetect

Description : autowire= "AutoDetect"

Choose a way between 3 and 2. Preference 3, no then select 2.

5.defualt

this needs to be "specified" in <beans defualt-autorwire=/>

When you specify default-atuowrite in <beans > , the default for all Beans Autowire is the specified assembly method ;

If there is no defualt-autorwire="specified" in <beans defualt-autorwire= "specify"/> , the default is Defualt-autorwire= "No"

<bean autorwire= "Default" > is specified according to Defualt-autorwire.

6. No: no automatic assembly

discussion: Automatic assembly can not use, relative set way, not clear and clear, too around. Unless there is a special requirement.

Iv. reading information in subordinate files

if different beans need to read the same information, you can use a decentralized configuration. Use placeholder variables instead of hard-coded configurations in Bean assembly files.

beans.xml Description: When passing Context:property-placeholder The introduction of the properties file, there are multiple need to use, number interval.<!--introduce our db.properties file--><context:property-placeholder location= "classpath:com/hsp/dispatch/ Db.properties,classpath:com/hsp/dispatch/db2.properties "/>-<!--Configuring a Dbutil object $ placeholder--><bean id=" Dbutil "class= "Com.hsp.dispatch.DBUtil" ><property name= "name" value= "${name}"/><property name= "drivername" value= "$ {drivername} "/><property name=" url "value=" ${url} "/><property name=" pwd "value=" ${pwd} "/></bean ><!--Configuring a Dbutil object--><bean id= "Dbutil2"class= "Com.hsp.dispatch.DBUtil" ><property name= "name" value= "${db2.name}"/><property name= "drivername" Value= "${db2.drivername}"/><property name= "url" value= "${db2.url}"/><property name= "pwd" value= "${ DB2.PWD} "/></bean>
db.properties:name=scottdrivername=oracle:jdbc:driver:OracleDirverurl=jdbc:oracle: thin:@127.0.0.1:1521: Hsppwd=tiger

Spring (ii) Assembly of __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.