Spring @ImportResource注解示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(6.3k)|赞(0)|评价(0)|浏览(659)

在这篇文章中,我们将快速讨论如何在一个简单的Spring boot应用中使用@ImportResource注解。

Spring提供了一个@ImportResource注解,用于将applicationContext.xml文件中的bean加载到应用上下文中。

  1. @ImportResource({"classpath*:applicationContext.xml"})

在这个例子中,我们正在创建一个简单的消息处理Spring Boot应用程序。在这里,我们使用SMSService、TwitterService和EmailService等不同的服务来发送消息。我们将在applicationContext.xml文件中配置消息服务豆,我们将使用@ImportResource注解加载豆。

  1. @SpringBootApplication
  2. @ImportResource({"classpath*:applicationContext.xml"})
  3. public class Springboot2XmlConfigApplication {
  4. public static void main(String[] args) {
  5. ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);
  6. MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
  7. userService.processMsg("twitter message sending ");
  8. }
  9. }

虽然有多种方法,但推荐的方法是创建一个单独的配置类来加载这个XML bean定义文件。

  1. @Configuration
  2. @ImportResource({"classpath*:applicationContext.xml"})
  3. public class XmlConfiguration {
  4. }

该定义的关键部分是@ImportResource({" classpath*:applicationContext.xml"})applicationContext.xml将被从classpath中导入。
让我们创建一个完整的简单的spring boot例子来演示如何设置基于XML的配置。

创建和导入Spring Boot应用程序

让我们使用Spring Initializr在http://start.spring.io/快速创建一个Spring Boot应用程序,它是一个在线Spring Boot应用程序生成器。

请参考下图中的项目结构。

pom.xml文件

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.guides.springboot2</groupId>
  6. <artifactId>springboot2-xml-config</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>springboot2-xml-config</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

让我们创建Message POJO类和一些服务类作为示范。在这个例子中,我们使用不同的服务如SMSServiceTwitterServiceEmailService发送消息。

Message.java

  1. package net.guides.springboot2.springboot2xmlconfig.model;
  2. public class Message {
  3. private int id;
  4. private String message;
  5. public Message(int id, String message) {
  6. super();
  7. this.id = id;
  8. this.message = message;
  9. }
  10. }

MessageService.java

  1. public interface MessageService {
  2. public void sendMsg(String message);
  3. }

EmailService.java

  1. import org.springframework.stereotype.Service;
  2. public class EmailService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

SMSService.java

  1. import org.springframework.stereotype.Service;
  2. public class SMSService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

TwitterService.java

  1. import org.springframework.stereotype.Service;
  2. public class TwitterService implements MessageService{
  3. public void sendMsg(String message) {
  4. System.out.println(message);
  5. }
  6. }

MessageProcessor.java

  1. public interface MessageProcessor {
  2. public void processMsg(String message);
  3. }

MessageProcessorImpl.java

  1. package net.guides.springboot2.springboot2xmlconfig.service;
  2. public class MessageProcessorImpl implements MessageProcessor {
  3. private MessageService messageService;
  4. public void setMessageService(MessageService messageService) {
  5. this.messageService = messageService;
  6. }
  7. public void processMsg(String message) {
  8. messageService.sendMsg(message);
  9. }
  10. }

The applicationContext.xml File

让我们在applicationContext.xml文件中创建并配置Spring Bean。

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <bean id="emailService"
  10. class="net.guides.springboot2.springboot2xmlconfig.service.EmailService" />
  11. <bean id="sMSService"
  12. class="net.guides.springboot2.springboot2xmlconfig.service.SMSService" />
  13. <bean id="twitterService"
  14. class="net.guides.springboot2.springboot2xmlconfig.service.TwitterService" />
  15. <bean id="messageProcessor"
  16. class="net.guides.springboot2.springboot2xmlconfig.service.MessageProcessorImpl">
  17. <property name="messageService" ref="twitterService"></property>
  18. </bean>
  19. </beans>

运行应用程序

这个spring boot应用程序有一个名为Springboot2XmlConfigApplication.java的入口点Java类,其中有*public static void main(String[] args)*方法,你可以运行它来启动该应用程序。

  1. package net.guides.springboot2.springboot2xmlconfig;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.annotation.ImportResource;
  6. import net.guides.springboot2.springboot2xmlconfig.service.MessageProcessor;
  7. @SpringBootApplication
  8. @ImportResource({"classpath*:applicationContext.xml"})
  9. public class Springboot2XmlConfigApplication {
  10. public static void main(String[] args) {
  11. ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);
  12. MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
  13. userService.processMsg("twitter message sending ");
  14. }
  15. }

输出

相关文章