Where do I put my XML beans in a Spring Boot application?

大兔子大兔子 提交于 2019-11-29 22:19:25

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

You also can translate the XML config to a Java config. 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.

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").

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:

XML based:

<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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!