Name of Bean in spring
1, each bean can have an id attribute, and according to the ID in the IOC container to find the bean, the id attribute value must be unique in the IOC container;
2, you can not specify the id attribute, only specify the fully qualified class name, such as:
<bean class= "Com.zyh.spring3.hello.StaticBeanFactory" ></bean>
The bean needs to be obtained through interface Getbean (Class<t> requiredtype).
Throw exception if the bean cannot be found: nosuchbeandefinitionexception
Throw exceptions if there are more than one bean of that type: nouniquebeandefinitionexception
3, if you do not specify the ID, only specify name, then name is the bean identifier, and need to be unique in the container;
4. Specify both the name and the ID, at which point the ID is an identifier, and name is the Bean alias, both of which can find the target bean;
5, you can specify more than one name, can be separated by semicolons (";"), A Space ("") or a comma (","), if no ID is specified, then the first name is an identifier, the rest is an alias, if the id attribute is specified, the ID is an identifier, all names are aliases. Such as:
<bean name= "alias1 alias2;alias3,alias4" id= "Hello1" class= "Com.zyh.spring3.hello.HelloWorld" > < Constructor-arg index= "0" value= "Rod" ></constructor-arg> </bean>
At this point, Hello1 is an identifier, and Alias1,alias2,alias3,alias4 is an alias, which can be used as the bean's key value;
6, you can use the <alias> tag to specify aliases, aliases must also be unique in the IOC container, such as:
<bean name= "Bean" class= "Com.zyh.spring3.hello.HelloWorld"/> <alias alias= "alias1" name= "Bean"/> <alias alias= "alias2" name= "Bean"/>
The difference between ref and IDREF
In spring, both the Idref property and the ref attribute can be injected into the constructor-arg element and the property element, so what's the difference between it?
Consider the following configuration:
<bean id= "Bea" class= "java.lang.String" > <constructor-arg index= "0" ><value>teststring</value ></constructor-arg> </bean> <bean id= "Beanid" class= "Com.zyh.spring3.hello.HelloWorld" > < Constructor-arg name= "name" ><idref bean= "Bea"/></constructor-arg> <property name= "id" > <ref Local= "Bea"/> </property> <property name= "age" value= "+" ></property> </bean>
In fact, IDREF injects the ID of the target bean instead of the instance of the target bean, while the IDREF container verifies that the name's bean is actually present when deployed. In fact, IDREF is just like value, simply injecting a string into a property or constructor, but injecting the id attribute value of a bean definition. So in the code above
<constructor-arg name= "name" ><idref bean= "Bea"/></constructor-arg>
is actually equivalent to
<constructor-arg name= "Name" ><value>bea</value></constructor-arg>
While ref is completely different, the ref element injects an instance of the target bean definition into a property or constructor, and the ref element has three attributes, the difference is as follows:
1, local can only specify the name of the bean defined in the same configuration file as the bean currently configured, and 2, the parent can only specify the object reference defined in the parent container of the current container; 3, the bean basically take all, that is, including the above two cases can be, so, all-in-one case, You can use the bean directly to specify the object reference. So, in the above configuration code, the parameter name of the constructor in this bean is Beanid the string "Bea", and its id attribute is injected with the teststring string.
How to configure Hibernate transactions in spring
STRUTS2 integrated Spring method and principle
Design and implement RESTful Web Services based on Spring
Spring-3.2.4 + Quartz-2.2.0 Integration Example
Using Spring for unit testing
Implementing Netty server-side UDP applications using spring annotations
Spring 3.x Enterprise App development PDF Full HD Scan version + source code
(RPM) The difference between a bean's naming problem (ID and name difference) and ref and IDREF in spring