Home  >  Spring

Spring @ImportResource Example

By Arvind Rai, November 07, 2021
Spring @ImportResource annotation imports the XML configurations in JavaConfig based applications. The class annotated with @Configuration can use @ImportResource annotation to load XML configurations.
In JavaConfig based applications, it may also require to use some XML configurations for some purpose. Another scenario can be that the old application based on XML configuration is being upgraded to JavaConfig based application. To keep bean definition at both places, in JavaConfig as well as XML configuration, we can use @ImportResource annotation.
Find the code snippet to use @ImportResource annotation.
@Configuration
@ImportResource("classpath:/app-conf.xml")
public class AppConfig  {
   ------
} 




Using @ImportResource Annotation

Find the example where we have one bean defined in XML configuration and one bean defined in JavaConfig file. We will import XML configuration with the help of @ImportResource annotation in JavaConfig class and will fetch values of both beans.
app-conf.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="subscription" class="com.concretepage.Subscription">
	    <property name="name" value="Subscription"/>
	    <property name="type" value="Weekly"/>
    </bean>
</beans> 
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:/app-conf.xml")
public class AppConfig  {
	@Bean(name="entitlement")
	public Entitlement entitlement(){
		Entitlement ent= new Entitlement();
		ent.setName("Entitlement");
		ent.setTime(20);
		return ent;
	}
} 
Entitlement.java
package com.concretepage;
public class Entitlement {
	private String name;
	private int time;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
} 
Subscription.java
package com.concretepage;
public class Subscription {
	private String name;
	private String type;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}	
} 
AppTest.java
package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AppTest {
	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(AppConfig.class);
		ctx.refresh();
		Entitlement ent= (Entitlement)ctx.getBean("entitlement");
	        System.out.println(ent.getName());		
	        System.out.println(ent.getTime());
		Subscription sub= (Subscription)ctx.getBean("subscription");
	        System.out.println(sub.getName());		
	        System.out.println(sub.getType());
	}
} 
Output
Entitlement
20
Subscription
Weekly 

Reference

Configuration Class-centric Use of XML with @ImportResource
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us