@ImportResource
annotation is used to load one or more xml configuration files.
How to specify the
configuration files using @ImportResource annotation?
Use
@ImportResource on a @Configuration class to load XML configuration files.
Example
@Configuration
@ImportResource("classpath:/configurations/myBeans.xml")
public
class MyConfigurations {
}
package com.sample.myApp.model; import org.springframework.stereotype.Component; @Component public class Employee { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; } }
EmployeeConfiguration.java
package com.sample.myApp.Configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.sample.myApp.model.Employee; @Configuration public class EmployeeConfiguration { @Bean public Employee getDefaultEmployee() { Employee emp = new Employee(); emp.setFirstName("NO_NAME"); emp.setId(-1); emp.setLastName("NO_NAME"); return emp; } }
MyConfigurations.java
package com.sample.myApp.Configuration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath:/configurations/myBeans.xml") public class MyConfigurations { }
I
created myBeans.xml file under src/main/resources/configurations folder.
myBeans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="krishna" class="com.sample.myApp.model.Employee"> <property name="id" value="123" /> <property name="lastName" value="Gurram" /> <property name="firstName" value="Krishna" /> </bean> </beans>
Application.java
package com.sample.myApp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.sample.myApp.model.Employee; @SpringBootApplication public class Application { public static void main(String args[]) { ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); Employee employee1 = (Employee) applicationContext.getBean("getDefaultEmployee"); Employee employee2 = (Employee) applicationContext.getBean("krishna"); System.out.println(employee1); System.out.println(employee2); applicationContext.close(); } }
Output
Employee [id=-1, firstName=NO_NAME, lastName=NO_NAME] Employee [id=123, firstName=Krishna, lastName=Gurram]
No comments:
Post a Comment