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