Java 我应该将我的 XML bean 放在 Spring Boot 应用程序中的什么位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31677553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where do I put my XML beans in a Spring Boot application?
提问by David Newcomb
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!
我要回到 Spring(目前是 v4)。现在有了@SpringBootApplication
其他注释,一切都很棒,但所有文档似乎都忘记提及我如何在 XML 中定义其他 bean!
For example I'd like to create an "SFTP Session Factory" as defined at: http://docs.spring.io/spring-integration/reference/html/sftp.html
例如,我想创建一个“SFTP 会话工厂”,定义如下:http: //docs.spring.io/spring-integration/reference/html/sftp.html
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:
有一个很好的 XML 来定义 bean,但是我到底把它放在哪里以及如何链接它?以前我做了一个:
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);
Where do I put the XML file? Is there a magic spring name to call it?
我把 XML 文件放在哪里?有没有神奇的春天名字来称呼它?
采纳答案by bvulaj
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.
只要您从一个基@Configuration
类开始(听起来好像是这样)@SpringBootApplication
,您就可以使用@ImportResource
注释来包含一个 XML 配置文件。
@SpringBootApplication
@ImportResource("classpath:spring-sftp-config.xml")
public class SpringConfiguration {
//
}
回答by dunni
You also can translate the XML config to a Java config. In your case it would look like:
您还可以将 XML 配置转换为 Java 配置。在你的情况下,它看起来像:
@Bean
public DefaultSftpSessionFactory sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost("localhost");
factory.setPrivateKey(new ClassPathResource("classpath:META-INF/keys/sftpTest"));
factory.setPrivateKeyPassphrase("springIntegration");
factory.setPort(22);
factory.setUser("kermit");
return factory;
}
You can put this method in the class with the @SpringBootApplication
annotation.
您可以将此方法放在带有@SpringBootApplication
注释的类中。
回答by Md Jalal
Spring boot ideal concept is avoid xml file. but if you want to keep xml bean, you can just add @ImportResource("classPath:beanFileName.xml")
.
Spring boot 的理想概念是避免 xml 文件。但是如果你想保留 xml bean,你可以添加@ImportResource("classPath:beanFileName.xml")
.
I would recommend remove the spring-sftp-config.xml
file. And, convert this file to spring annotation based bean. So, whatever class has been created as bean. Just write @Service
or @Component
annotation before class name. for example:
我建议删除该spring-sftp-config.xml
文件。并且,将此文件转换为基于 spring 注释的 bean。因此,任何类都已创建为 bean。只需在类名前写上@Service
或@Component
注释即可。例如:
XML based:
基于 XML:
<bean ID="id name" class="com.example.Employee">
Annotation:
注解:
@Service or @Component
class Employee{
}
And, add @ComponentScan("Give the package name")
. This is the best approach.
并且,添加@ComponentScan("Give the package name")
. 这是最好的方法。