Spring @ImportResource Annotation Example

In this section we will learn about @ImportResource Annotation.

The @ImportResource annotation is used to load beans from an XML configuration file into an Application Context in Java configuration based applications. The class annotated with @Configuration can use @ImportResource annotation to load XML configurations.

For example, old application based on XML configuration is being upgraded to Java configuration based application, to keep bean definition at both places, in Java configuration as well as XML configuration, we can use @ImportResource annotation.  

@Configuration
@ImportResource("classpath:/app-config.xml")
public class AppConfig {

..............
}


The following example creates a Spring Boot application which uses @ImportResource annotation.

Project Directory


pom.xml

<?xml version="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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-importresource-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-importresource-annotation-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>


SMSService.java

package com.knf.dev.demo.service;

public class SMSService{

private String message;
private String type;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}


EmailService.java

package com.knf.dev.demo.service;

public class EmailService {

private String message;
private String type;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}


app-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="smsService" class="com.knf.dev.demo.service.SMSService">
<property name="message" value="SMS Message"/>
<property name="type" value="SMS"/>
</bean>
</beans>


AppConfig.java

package com.knf.dev.demo.config;

import com.knf.dev.demo.service.EmailService;
import com.knf.dev.demo.service.SMSService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Primary;

@Configuration
@ImportResource("classpath:/app-config.xml")
public class AppConfig {

@Bean
public EmailService emailService() {
EmailService emailService = new EmailService();
emailService.setMessage("Email message");
emailService.setType("Email");
return emailService;
}

}

We have one bean defined in XML configuration and one bean defined in Java configuration file. We imported XML configuration with the help of @ImportResource annotation in Java configuration class.


Run the application - Application.java

package com.knf.dev.demo;

import com.knf.dev.demo.service.EmailService;
import com.knf.dev.demo.service.SMSService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {


public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args);

EmailService emailService = context.
getBean(EmailService.class);
System.out.println(emailService.getMessage());
System.out.println(emailService.getType());

SMSService smsService = context.
getBean(SMSService.class);
System.out.println(smsService.getMessage());
System.out.println(smsService.getType());

context.close();
}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
Email message
Email
SMS Message
SMS


Download Source Code

More related topics,

Spring Web Annotations


Spring Core Annotations

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete