我应该将 XML bean 放在 Spring 引导应用程序的什么位置?
Where do I put my XML beans in a Spring Boot application?
我要回到 Spring(目前是 v4)。现在有了 @SpringBootApplication
和其他注释,一切都很棒,但所有文档似乎都忘记提及我如何在 XML!
中定义其他 bean
例如,我想创建一个 "SFTP Session Factory",定义如下:
http://docs.spring.io/spring-integration/reference/html/sftp.html
XML 可以很好地定义 bean,但是我究竟应该把它放在哪里以及如何 link 它呢?之前我做了一个:
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
指定文件名和位置,但现在我正在尝试使用:
ApplicationContext ctx = SpringApplication.run(Application.class);
XML 文件放在哪里?是否有一个神奇的 spring 名字来称呼它?
只要您从基础 @Configuration
class 开始,这可能听起来像是 @SpringBootApplication
,您就可以使用 @ImportResource
注释以包含 XML 配置文件。
@SpringBootApplication
@ImportResource("classpath:spring-sftp-config.xml")
public class SpringConfiguration {
//
}
您还可以将 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;
}
您可以将此方法放在 class 中,并带有 @SpringBootApplication
注释。
Spring 引导理想概念是避免 xml 文件。但是如果你想保留 xml bean,你可以只添加 @ImportResource("classPath:beanFileName.xml")
.
我建议删除 spring-sftp-config.xml
文件。并且,将此文件转换为 spring 基于注释的 bean。因此,任何 class 都已创建为 bean。只需在 class 名称前写上 @Service
或 @Component
注释即可。例如:
XML 基于:
<bean ID="id name" class="com.example.Employee">
注解:
@Service or @Component
class Employee{
}
并且,添加 @ComponentScan("Give the package name")
。这是最好的方法。
我要回到 Spring(目前是 v4)。现在有了 @SpringBootApplication
和其他注释,一切都很棒,但所有文档似乎都忘记提及我如何在 XML!
例如,我想创建一个 "SFTP Session Factory",定义如下: http://docs.spring.io/spring-integration/reference/html/sftp.html
XML 可以很好地定义 bean,但是我究竟应该把它放在哪里以及如何 link 它呢?之前我做了一个:
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
指定文件名和位置,但现在我正在尝试使用:
ApplicationContext ctx = SpringApplication.run(Application.class);
XML 文件放在哪里?是否有一个神奇的 spring 名字来称呼它?
只要您从基础 @Configuration
class 开始,这可能听起来像是 @SpringBootApplication
,您就可以使用 @ImportResource
注释以包含 XML 配置文件。
@SpringBootApplication
@ImportResource("classpath:spring-sftp-config.xml")
public class SpringConfiguration {
//
}
您还可以将 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;
}
您可以将此方法放在 class 中,并带有 @SpringBootApplication
注释。
Spring 引导理想概念是避免 xml 文件。但是如果你想保留 xml bean,你可以只添加 @ImportResource("classPath:beanFileName.xml")
.
我建议删除 spring-sftp-config.xml
文件。并且,将此文件转换为 spring 基于注释的 bean。因此,任何 class 都已创建为 bean。只需在 class 名称前写上 @Service
或 @Component
注释即可。例如:
XML 基于:
<bean ID="id name" class="com.example.Employee">
注解:
@Service or @Component
class Employee{
}
并且,添加 @ComponentScan("Give the package name")
。这是最好的方法。