Spring annotation and configuration file type

Source: Internet
Author: User

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
    1. <context:component-scan base-package="com"/><!---This statement allows you to search for classes in COM and COM packages->
    2. <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
    1. @Controller
    2. 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
    1. @Autowired
    2. 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
    1. <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
    1. <Bean id="test" class="Com.test.Test">
    2. <property name= "helloWorld" ref="Hessianhelloworldimpl"></ Property>
    3. </Bean>

3. Add in Test.java

Private Hessianhelloworld HelloWorld;

and Get/set method

Java code
    1. Private Hessianhelloworld HelloWorld;
    2. Public Hessianhelloworld Gethelloworld () {
    3. return helloWorld;
    4. }
    5. Public void Sethelloworld (Hessianhelloworld helloWorld) {
    6. This.helloworld = HelloWorld;
    7. }

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
    1. <Bean id= "helloWorld" class="Com.impl.HessianHelloWorldImpl" />

2. Add annotations on managed beans (HESSIANHELLOWORLDIMPL)

Java code
    1. @Controller
    2. 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
    1. @Autowired
    2. Private Hessianhelloworld Hello;

Will be reported at startup

Java code
    1. Nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
    2. No unique bean of type [COM. Hessianhelloworld] is defined:
    3. 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
    1. <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
    1. @Autowired
    2. Private Hessianhelloworld HelloWorld;

Or remove @autowired Specify the instance name of the business class attribute directly in spring's bean configuration file

XML code
    1. <Bean id="test" class="Com.test.Test">
    2. <property name="Hello" ref="Hessianhelloworldimpl"></ Property>
    3. </Bean>

Common errors:

When you configure the use of a bean through a configuration file

Project start times wrong:

Java code
    1. Cannot resolve reference to beans ' Hessianhelloworldimpl ' while setting beans property ' Hello ';
    2. Nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
    3. 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

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.