Spring annotations mixed with XML file declaration bean

Source: Internet
Author: User

In spring, the use of XML files to configure beans is cumbersome, but with some flexibility, without changing the source code can change the program configuration or changes since the relationship, using annotation configuration is efficient, but lost the flexibility of the XML file.

In this case, we can configure the bean in a way that the XML file is mixed with the annotations, and can be configured using annotations without changing the properties. However, do not use automatic assembly annotations such as @component or @controller or @serverce on this class, or when you declare a bean of the same class in an XML file, there will be more than one bean of the same type in the Spring container. Errors may occur during automatic assembly (because there are multiple beans of the same type). The bean of this class is declared in the XML file, and annotations that use automatic assembly such as @resource in this class will also take effect.

For example:

/*** @ClassName: abstractxmlgenerator* @Description: Xmlgenerator abstract class, built in a XmlReader and pathmanager* @date December 12, 2015 PM 9    : 21:21**/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; }

In the XML file, configure:

<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 automatic assembly described above will also be automated, even if the attribute is not explicitly configured in XML.

If a field uses automatic assembly and the properties of the field are also set in the XML file, the bean declared in the XML file will be the primary configuration in the XML file, which means that the XML configuration file overrides the automatic assembly.

When a subclass inherits a parent class, there are auto-assembled properties in the parent class, and the parent class is not in the spring container, and the automatic assembly properties in the parent class are also in effect.

This article is from the "Flying Fish Technology" blog, please be sure to keep this source http://flyingfish.blog.51cto.com/9580339/1722362

Spring annotations mixed with XML file declaration 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.