http://tom-seed.iteye.com/blog/1584632
Spring annotation mode Bean container Management
1. By configuring spring component injection in the configuration file
XML code
- <context:component-scan base-package="com"/><!---This statement allows you to search for classes in COM and COM packages->
- <mvc:annotation-driven/>
2. Write a simple bean class for spring, a generic interface and a specific implementation class
For example:
A. Create the following interface under the COM package: Hessianhelloworld
The following implementation class is created under the B.com.impl package: Hessianhelloworldimpl
3. Use annotation form here to support bean management
Add annotations on the class name of the bean that needs to be hosted: @Controller
The complete code is as follows:
Java code
- @Controller
- Public class Hessianhelloworldimpl implements hessianhelloworld{}
4. Invoke the above managed bean in the business class and introduce it in the form of a dynamic proxy;
Private Hessianhelloworld Hello;
and add annotations on the attributes
@Autowired
The complete code is as follows:
Java code
- @Autowired
- Private Hessianhelloworld Hello;
End of annotation method
Spring Configuration file Form:
1. Remove all annotations above
To add a class's configuration in the spring configuration file
XML code
- <Bean id="Hessianhelloworldimpl" class="Com.impl.HessianHelloWorldImpl">< /Bean>
2. Add the configuration of the business class in the spring configuration file
The business class name is test, the configuration attribute in the Business Class (HelloWorld), and the attribute point to the ID of the managed bean, where the attribute HelloWorld in the business class must be consistent, and the Get/set method that has the property
XML code
- <Bean id="test" class="Com.test.Test">
- <property name= "helloWorld" ref="Hessianhelloworldimpl"></ Property>
- </Bean>
3. Add in Test.java
Private Hessianhelloworld HelloWorld;
and Get/set method
Java code
- Private Hessianhelloworld HelloWorld;
- Public Hessianhelloworld Gethelloworld () {
- return helloWorld;
- }
- Public void Sethelloworld (Hessianhelloworld helloWorld) {
- This.helloworld = HelloWorld;
- }
End of configuration file mode
Precautions
In this process, it is important to note how the annotations are mixed with the configuration file configuration (because of the different business requirements, such as when using a timer in the case of annotations), when using annotations, when the bean is in the Spring bean container, the ID of the first letter lowercase class name, The ID is customized by configuration file configuration.
The following example uses both annotation and configuration methods:
1. Add the following configuration in the spring configuration file:
XML code
- <Bean id= "helloWorld" class="Com.impl.HessianHelloWorldImpl" />
2. Add annotations on managed beans (HESSIANHELLOWORLDIMPL)
Java code
- @Controller
- Public class Hessianhelloworldimpl implements hessianhelloworld{}
This Hessianhelloworldimpl registers two instances in the Spring bean container. i.e. (HelloWorld and Hessianhelloworldimpl)
3. Add labels to the properties of the business class (The calling Class), i.e. do not automatically match by tags by configuring the class for Hello in the configuration file.
Java code
- @Autowired
- Private Hessianhelloworld Hello;
Will be reported at startup
Java code
- Nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
- No unique bean of type [COM. Hessianhelloworld] is defined:
- Expected single matching beans but found 2: [HelloWorld, Hessianhelloworldimpl]
Because the pointer here is named Hello, it is not possible to confirm exactly which instance is needed by auto-matching
In the case of mixing, you need to name the bean instance and manage the names of the beans.
This does not use @controller, which registers the bean directly in the configuration file (the bean's ID is not hello), that is, a bean is registered two times in the configuration file.
XML code
- <Bean id= "hessianhelloworldimpl" class="Com.remote.impl.HessianHelloWorldImpl"> </Bean>
The same effect can occur.
If you must use blending, the property name can be the same as the name in the Bean container in the business class (The calling Class)
Java code
- @Autowired
- Private Hessianhelloworld HelloWorld;
Or remove @autowired Specify the instance name of the business class attribute directly in spring's bean configuration file
XML code
- <Bean id="test" class="Com.test.Test">
- <property name="Hello" ref="Hessianhelloworldimpl"></ Property>
- </Bean>
Common errors:
When you configure the use of a bean through a configuration file
Project start times wrong:
Java code
- Cannot resolve reference to beans ' Hessianhelloworldimpl ' while setting beans property ' Hello ';
- Nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
- No Bean named ' Hessianhelloworldimpl ' is defined
Spring cannot find the class name and needs to check the configuration of the bean in the spring configuration file, and if you use annotations, you need to check
<context:component-scan base-package= "com"/>
<mvc:annotation-driven/>
Have you added
When a tag is used in a business call Class (@Autowired), it may not get an error at startup, but a null pointer exception will occur on the call, or it may be because the bean is not specified as in the above case
Spring annotation and configuration file type