在Spring引导应用程序中,我把XML bean放在哪里?

时间:2021-02-11 12:22:29

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文件放在哪里?有一个神奇的弹簧名字叫它吗?

3 个解决方案

#1


30  

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 {
  //
}

#2


3  

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注释将该方法放在类中。

#3


0  

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(“类路径: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(“给出包名”)。这是最好的方法。

#1


30  

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 {
  //
}

#2


3  

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注释将该方法放在类中。

#3


0  

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(“类路径: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(“给出包名”)。这是最好的方法。