Spring @ImportResource注解示例

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

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

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

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

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

@SpringBootApplication
@ImportResource({"classpath*:applicationContext.xml"})
public class Springboot2XmlConfigApplication {

 public static void main(String[] args) {
  ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);

  MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
  userService.processMsg("twitter message sending ");
 }
}

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

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

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

创建和导入Spring Boot应用程序

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

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

pom.xml文件

<?xmlversion="1.0"encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>net.guides.springboot2</groupId>
 <artifactId>springboot2-xml-config</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springboot2-xml-config</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

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

Message.java

package net.guides.springboot2.springboot2xmlconfig.model;

public class Message {
 private int id;
 private String message;
 public Message(int id, String message) {
  super();
  this.id = id;
  this.message = message;
 }
}

MessageService.java

public interface MessageService {
 public void sendMsg(String message);
}

EmailService.java

import org.springframework.stereotype.Service;

public class EmailService implements MessageService{

 public void sendMsg(String message) {
  System.out.println(message);
 }

}

SMSService.java

import org.springframework.stereotype.Service;

public class SMSService implements MessageService{

 public void sendMsg(String message) {
  System.out.println(message);
 }

}

TwitterService.java

import org.springframework.stereotype.Service;

public class TwitterService implements MessageService{

 public void sendMsg(String message) {
  System.out.println(message);
 }

}

MessageProcessor.java

public interface MessageProcessor {
 public void processMsg(String message);
}

MessageProcessorImpl.java

package net.guides.springboot2.springboot2xmlconfig.service;

public class MessageProcessorImpl implements MessageProcessor {

 private MessageService messageService;

 public void setMessageService(MessageService messageService) {
  this.messageService = messageService;
 }

 public void processMsg(String message) {
  messageService.sendMsg(message);
 }
}

The applicationContext.xml File

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

<?xmlversion="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

 <bean id="emailService"
  class="net.guides.springboot2.springboot2xmlconfig.service.EmailService" />
 <bean id="sMSService"
  class="net.guides.springboot2.springboot2xmlconfig.service.SMSService" />
 <bean id="twitterService"
  class="net.guides.springboot2.springboot2xmlconfig.service.TwitterService" />
 <bean id="messageProcessor"
  class="net.guides.springboot2.springboot2xmlconfig.service.MessageProcessorImpl">
  <property name="messageService" ref="twitterService"></property>
 </bean>

</beans>

运行应用程序

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

package net.guides.springboot2.springboot2xmlconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

import net.guides.springboot2.springboot2xmlconfig.service.MessageProcessor;

@SpringBootApplication
@ImportResource({"classpath*:applicationContext.xml"})
public class Springboot2XmlConfigApplication {

 public static void main(String[] args) {
  ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);

  MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
  userService.processMsg("twitter message sending ");
 }
}

输出

相关文章