我将XML bean放在Spring Boot应用程序的哪里? [英] Where do I put my XML beans in a Spring Boot application?
问题描述
我要回到Spring(当前是v4).使用@SpringBootApplication
和其他注释现在一切都很棒,但是所有文档似乎都忘了提到我如何用XML定义其他bean!
I'm getting back into Spring (currently v4). It's all wonderful now with @SpringBootApplication
and the other annotations but all the documentation seems to forget to mention how I define other beans in XML!
例如,我想创建一个"SFTP会话工厂",其定义如下: http://docs.spring.io/spring-integration/reference/html/sftp.html
For example I'd like to create an "SFTP Session Factory" as defined at: http://docs.spring.io/spring-integration/reference/html/sftp.html
有很多XML可以定义bean,但是我到底将它放在哪里以及如何链接它呢?以前我做过:
There is a nice bit of XML to define the bean but where on earth do I put it and how do I link it in? Previously I did a:
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
指定文件名和位置,但是现在我要使用:
to specify the file name and location but now that I'm trying to use:
ApplicationContext ctx = SpringApplication.run(Application.class);
我将XML文件放在哪里?有一个神奇的弹簧名字可以称呼它吗?
Where do I put the XML file? Is there a magic spring name to call it?
推荐答案
只要您从基类@Configuration
开始,这听起来像是在使用@SpringBootApplication
一样,则可以使用 @ImportResource
注释包括一个XML配置文件也是如此.
As long as you're starting with a base @Configuration
class to begin with, which it maybe sounds like you are with @SpringBootApplication
, you can use the @ImportResource
annotation to include an XML configuration file as well.
@SpringBootApplication
@ImportResource("classpath:spring-sftp-config.xml")
public class SpringConfiguration {
//
}
这篇关于我将XML bean放在Spring Boot应用程序的哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!