简体   繁体   English

将Spring配置存储在类与xml文件中

[英]Storing spring configuration in a class versus xml file

Currently my spring configurations are in a xml file (the traditionaly way). 目前,我的spring配置位于xml文件中(传统方式)。

One thing that i like about this is during deployment I can deploy a different version that has my production settings, or say in a test environment I can have test settings there. 我喜欢的一件事是在部署期间,我可以部署具有我的生产设置的其他版本,或者说在测试环境中,我可以在那里进行测试设置。

I like the idea of having things configured in a class, but that will get compiled into my war and then it won't be as flexible. 我喜欢在类中配置事物的想法,但是那样会被编译进我的战争中,然后它就不会那么灵活。

Is there a way around this? 有没有解决的办法?

Java configuration is great and it has several advantages: Java配置很棒,它有几个优点:

  • refactoring-friendly 重构友好

  • type-safety 类型安全

  • much more flexible (you can write any Java code, not being bound to XML semantics and capabilities). 更加灵活(您可以编写任何Java代码,而不受XML语义和功能的约束)。

I can deploy a different version that has my production settings, or say in a test environment I can have test settings there. 我可以部署具有生产设置的其他版本,或者说在测试环境中可以在其中进行测试设置。

Investigate Spring @Profile s . 研究Spring @Profile They are orthogonal to your question (work both in XML and @Configuration ) but are best suited in your situation. 它们与您的问题正交(在XML和@Configuration均可工作),但最适合您的情况。

Those are the only two ways available. 这些是仅有的两种方法。 If you don't want configuration baked in code, then you have to go with xml. 如果您不希望在代码中烘焙配置,则必须使用xml。

The spring reference manual includes a section on combining both Java and XML configuration. 春天的参考手册包括有关结合Java和XML配置的部分。 See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java-combining 参见http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java-combining

If you tend to be more XML centric you can bootstrap your app using XML and then introduce Java config as needed in an ad-hoc fashion. 如果您倾向于以XML为中心,则可以使用XML引导应用程序,然后根据需要以临时方式引入Java配置。 This might be a good way to ease into it. 这可能是缓解它的好方法。 You might decide to go Java config all the way. 您可能会决定一路进行Java配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM