@Import :- In Spring 3.0 we can also use the Java based Configuration instead of large XML files. We can define the bean with the help of Java files and we can also load these multiple JavaConfig file using @Import annotation in single JavaConfig file.
The first approach to use import resource in spring 2.5 is:-
<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-2.5.xsd">
<import resource="config/customer.xml"/>
<import resource="config/scheduler.xml"/>
</beans>
but in Spring 3.0 we can also use with the help of JavaConfig files or Java Based Configuration.
Example:-
Student.java
package com.chandan;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentConfig.java
package com.chandan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StudentConfig {
@Bean(name="student")
public Student getStudent()
{
return new Student();
}
}
Course.java
package com.chandan;
public class Course {
private int course_code;
private String course_name;
public int getCourse_code() {
return course_code;
}
public void setCourse_code(int course_code) {
this.course_code = course_code;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
}
CourseConfig.java
package com.chandan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CourseConfig {
@Bean(name="course")
public Course getCourse()
{
return new Course();
}
}
AppConfig.java
package com.chandan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({CourseConfig.class,StudentConfig.class})
public class AppConfig {
}
MainApp.java
package com.chandan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Student student=(Student)context.getBean("student");
Course course=(Course)context.getBean("course");
student.setName("Manish");
course.setCourse_code(101);
course.setCourse_name("MCA");
System.out.println("Name:-"+student.getName());
System.out.println("Course Code:-"+course.getCourse_code());
System.out.println("Course Name:-"+course.getCourse_name());
}
}
In Above Example we can see that the both JavaConfig files(StudentConfig.java,CourseConfig.java) can be used by another JavaConfig file(AppConfig.java) by @Import Annotation.
0 Comment(s)