Spring annotation and xml file hybrid declaration bean

Source: Internet
Author: User

Spring annotation and xml file hybrid declaration bean
In spring, bean configuration using xml files is cumbersome, but flexible. You can change the program configuration or the relationship between bean configuration without changing the source code; the annotation configuration method is efficient, but the flexibility of xml files is lost. In this case, you can configure the bean by mixing the xml file with the annotation. You can configure the bean by using the annotation without modifying the attribute, however, do not use automatically assembled annotations such as @ Component or @ Controller or @ Serverce in this class. Otherwise, when the bean of the same class is declared in the xml file, multiple beans of the same type will appear in the spring container, and errors may occur during the automatic assembly process (because there are multiple beans of the same type ). Declare the bean of this class in the xml file. The annotation automatically assembled such as @ Resource in this class will also take effect. For example:

/*** @ ClassName: AbstractXmlGenerator * @ Description: XmlGenerator abstract class, built-in XmlReader and PathManager * @ date 9:21:21 on April 9, December 12, 2015 **/public abstract class AbstractXmlGenerator implements Generator {@ Resource private XmlReader reader; private String xmlFilePath; private String query; @ Resource private PathManager pathManager; public XmlReader getReader () {return reader;} public void setReader (XmlReader reader) {this. reader = reader ;}

 

Configure in the xml file:
<bean id="forcedSeedsGenerator" class="collector.generator.AbstractXmlGenerator "><property name="query" value="lagou keyword"></property><property name="xmlFilePath" value=""></property></bean>

 

@Resource    private XmlReader reader;

 

The preceding automatic assembly is automatically performed, even if the property is not explicitly configured in xml. If a field uses automatic assembly and the attribute of this field is also set in the xml file, the bean declared in the xml file will be mainly configured in the xml file, that is to say, the xml configuration file overwrites automatic assembly. When the child class inherits the parent class, the parent class has the automatic assembly attribute, and the parent class is not in the Spring container, the automatic assembly attribute in the parent class also takes effect.

Related Article

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.