[英]Where do I put my XML beans in a Spring Boot application?
I'm getting back into Spring (currently v4). 我要回到Spring(目前是第4版)。 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! 现在使用
@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 Session Factory”,如下所示: 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? 有一些很好的XML来定义bean,但我在哪里放置它以及如何将其链接? 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);
Where do I put the XML file? 我在哪里放置XML文件? Is there a magic spring name to call it?
是否有一个神奇的春天名称来称呼它?
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 {
//
}
You also can translate the XML config to a Java config. 您还可以将XML配置转换为Java配置。 In your case it would look like:
在你的情况下,它看起来像:
@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
批注将此方法放在类中。
Spring boot ideal concept is avoid xml file. Spring启动的理想概念是避免使用xml文件。 but if you want to keep xml bean, you can just add
@ImportResource("classPath:beanFileName.xml")
. 但是如果你想保留xml bean,你可以添加
@ImportResource("classPath:beanFileName.xml")
。
I would recommend remove the spring-sftp-config.xml
file. 我建议删除
spring-sftp-config.xml
文件。 And, convert this file to spring annotation based bean. 并且,将此文件转换为基于spring注释的bean。 So, whatever class has been created as bean.
所以,无论什么类都被创建为bean。 Just write
@Service
or @Component
annotation before class name. 只需在类名之前编写
@Service
或@Component
注释。 for example: 例如:
XML based: 基于XML:
<bean ID="id name" class="com.example.Employee">
Annotation: 注解:
@Service or @Component
class Employee{
}
And, add @ComponentScan("Give the package name")
. 并且,添加
@ComponentScan("Give the package name")
。 This is the best approach. 这是最好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.