Convert Spring XML configuration to Java configuration

I would like to use Spring with Java configuration, but almost all examples are written in XML, and I do not know how to translate them into Java. Take a look at these examples from Spring Security 3 :

<http auto-config='true'> <intercept-url pattern="/**" access="ROLE_USER" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="bob" password="bobspassword" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> <password-encoder hash="sha"> <salt-source user-property="username"/> </password-encoder> 

How can this be translated into Java? Or, in general, how can I translate Spring XML config to Java? The Spring section has a small section on Java, but it is not so useful.

+6
java spring xml configuration
source share
3 answers

You cannot translate XML configurations with custom namespaces (e.g. http://www.springframework.org/schema/security ). However, you can mix XML configurations with Java based on @ImportResource

+1
source share

Follow the JavaConfig project documentation .

+3
source share

This is probably simpler than you think. Spring does no magic. The XML configuration parser simply creates the bean definitions and registers them with the bean factory. You can do the same by creating a DefaultListableBeanFactory and registering your bean definitions with it. The main mistake here is to think, "Gee, I'll just create beans and put them in the application context." This approach does not work, because Spring creates beans lazily, and the API is built around the idea of ​​a factory, which is called when necessary, and not a factory, which does all the work at startup.

Here is an example of code code . Note that this example requires much more lines of code, but by defining your own helper methods, you should be able to break it down into something that should be on par with XML.

Also check out examples for Spring tags .

+2
source share

All Articles