java - Example of xml config taking precedence over annotation config in Spring -
I've read that in a book, that XML configuration is a higher priority than annotation configuration.
But there is no example of it.
Can you show an example of this? Here is a simple example showing a mixture of XML-based spring config and Java based spring config.
There are 5 files in the example:
Main.Java AppConfig.java applicationContext.xml HelloWorld.Java HelloUniverse.java Try it already running < Code> Hello Bean Sam commented on an application contact file and you will see that Hello bean has been institute from Bean App Config Config Class. Then it is explained in the applicationContext.xml file with helobine beans and you will see that XML defined bean has taken priority over the bean defined in the AppConfig class.
Main. Java
package my.test; Import org.springframework.context.ApplicationContext; Import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public Sector Main {Public Stable Zero Main (String [] Args) {ApplicationContext ctx = New Entatation Conflict Application Contain (AppConfig.class); Ctx.getBean ("helloBean"); }}
AppConfig.java
package my.test; Import org.springframework.context.annotation. *; @ImportResource ({"my / test / applicationContext.xml"}) Public class AppConfig {@Bean (name = "helloBean") Public object hello () {return new hello (); }}
ApplicationContext.xml
Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: Xsi = "Http://www.w3.org/2001/XMLSchema-instance" xmlns: reference = "http://www.springframework.org/schema/context" xsi: schema location = "http: // www. Springframework .org / schema / beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/ Schema / context / spring-context.xsd "& gt; & Lt; Bean id = "halobiine" category = "my.test.HelloUniverse" /> & Lt; / Bean & gt;
HelloUniverse.java
package my.test; Public class HelloUniverse {Public Hello Universe ({{System.out.println ("Hello Universe !!!"); }}
HelloWorld.java
package my.test; Public category helowwald {public helowold () {System.out.println ("Hello World !!!"); }}
Comments
Post a Comment