If you are using JavaConfig in Spring to configure the bean definitions then, in order to modularize your configurations you can use @Import annotation.
@Import annotation in Spring JavaConfig allows for loading @Bean definitions from another configuration class so you can group your configuration by modules or functionality which makes your code easy to maintain.
@Import annotation is similar to <import/> element which is used to divide the large Spring XML configuration file into smaller XMLs and then import those resources.
How @Import annotation works
Let’s say you have two configuration classes ConfigA and ConfigB then you can import ConfigA into ConfigB as shown below.
Then you don’t need to specify both ConfigA.class and ConfigB.class when instantiating the context, so this is not required.
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
As bean definitions of ConfigA are already loaded by using @Import annotation with ConfigB bean, only ConfigB needs to be specified explicitly.
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigA.class, ConfigB.class);
ApplicationContext ctx = new AnnotationConfigApplicationContext( ConfigB.class);
@Import annotation example
Let’s see a proper example using @Import annotation and Spring JavaConfig. The objective is to insert a record into DB using NamedParameterJDBCTemplate. For that you need a DataSource, NamedParameterJDBCTemplate configured with that DataSource and a Class where NamedParameterJDBCTemplate is used to insert a record in DB. We’ll use separate config classes in order to have a modular code.
- Refer Spring Example Program Using JavaConfig And Annotations to see an example of using JavaConfig in Spring.
- Refer Insert\Update Using NamedParameterJDBCTemplate in Spring Framework to see an example of using NamedParameterJDBCTemplate in Spring Framework.
DataSource Configuration
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource(value="classpath:config/db.properties", ignoreResourceNotFound=true)
@Import({EmpConfig.class, JDBCConfig.class})
public class DBConfig {
@Autowired
private Environment env;
@Bean
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
System.out.println("User " + env.getProperty("db.username"));
ds.setDriverClassName(env.getProperty("db.driverClassName"));
ds.setUrl(env.getProperty("db.url"));
ds.setUsername(env.getProperty("db.username"));
ds.setPassword(env.getProperty("db.password"));
return ds;
}
}
Here note that DB properties are read from a properties file in Spring. DataSource used is Apache BasicDataSource.
NamedParameterJDBCTemplate configuration
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
public class JDBCConfig {
private final BasicDataSource dataSource;
@Autowired
public JDBCConfig(BasicDataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public NamedParameterJdbcTemplate namedJdbcTemplate() {
return new NamedParameterJdbcTemplate(dataSource);
}
}
EmpConfig Class
import org.netjs.dao.EmployeeDAO;
import org.netjs.daoimpl.EmployeeDAOImpl1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
public class EmpConfig {
@Autowired
private NamedParameterJdbcTemplate namedJdbcTemplate;
@Bean
public EmployeeDAO empService() {
return new EmployeeDAOImpl(namedJdbcTemplate);
}
}
EmployeeDAO class
public interface EmployeeDAO {
public int save(Employee employee);
}
EmployeeDAOImpl class
public class EmployeeDAOImpl1 implements EmployeeDAO {
private NamedParameterJdbcTemplate namedJdbcTemplate;
final String INSERT_QUERY = "insert into employee (name, age) values (:name, :age)";
public EmployeeDAOImpl1(NamedParameterJdbcTemplate namedJdbcTemplate){
this.namedJdbcTemplate = namedJdbcTemplate;
}
@Override
public int save(Employee employee) {
// Creating map with all required params
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("name", employee.getEmpName());
paramMap.put("age", employee.getAge());
// Passing map containing named params
return namedJdbcTemplate.update(INSERT_QUERY, paramMap);
}
}
Employee Bean
You can run this example using the following code.
public class Employee {
private int empId;
private String empName;
private int age;
public void setEmpId(int empId) {
this.empId = empId;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public void setAge(int age) {
this.age = age;
}
public int getEmpId() {
return empId;
}
public String getEmpName() {
return empName;
}
public int getAge() {
return age;
}
}
As you can see only DBConfig.class is specified here not all the three config classes, still you can get empService bean which is defined in EmpConfig.
public class App {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext
(DBConfig.class);
EmployeeDAO empBean = (EmployeeDAO)context.getBean("empService");
Employee emp = new Employee();
emp.setEmpName("Jacko");
emp.setAge(27);
int status = empBean.save(emp);
context.close();
}
}
That's all for this topic @Import Annotation in Spring JavaConfig. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like -