上QQ阅读APP看书,第一时间看更新
Java versus XML configuration
With the advent of annotations in Java 5, there is widespread use of Java configuration for Spring based applications. What is the right choice to make if you have to choose between a Java-based configuration as opposed to an XML-based configuration?
Spring provides equally good support for Java and XML-based configuration. So, it's left to the programmer and their team to make the choice. Whichever choice is made, it is important to have consistency across teams and projects. Here are some things you might need to consider when making a choice:
- Annotations lead to shorter and simpler bean definitions.
- Annotations are closer to the code they are applicable on than the XML-based configuration.
- Classes using annotations are no longer simple POJOs because they are using framework-specific annotations.
- Autowiring problems when using annotations might be difficult to solve because the wiring is no longer centralized and is not explicitly declared.
- There might be advantages of more flexible wiring using Spring context XML if it is packaged outside the application packaging--WAR or EAR. This will enable us to have different setup for integration tests, for example.