package com.config.inheritance.annotation;
public class Department {
private String departmentId;
private String departmentName;
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
Step 02:
public class Department {
private String departmentId;
private String departmentName;
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
package com.config.inheritance.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
// spring config that loads the properties file
@ImportResource(\\\"classpath:/properties-config.xml\\\")
public class DepartmentBeanApp {
/**
* Using property \\\'EL\\\' syntax to load values from the department.properties value
*/
private @Value(\\\"#{departmentProperties[\\\'departmentBean.id\\\']}\\\")
String departmentId;
private @Value(\\\"#{departmentProperties[\\\'departmentBean.departmentname\\\']}\\\")
String departmentName;
@Bean(name = \\\"departmentBean\\\")
public Department addDepartment() {
Department department = new Department();
department.setDepartmentId(departmentId);
department.setDepartmentName(departmentName);
return department;
}
}
Step 03:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
// spring config that loads the properties file
@ImportResource(\\\"classpath:/properties-config.xml\\\")
public class DepartmentBeanApp {
/**
* Using property \\\'EL\\\' syntax to load values from the department.properties value
*/
private @Value(\\\"#{departmentProperties[\\\'departmentBean.id\\\']}\\\")
String departmentId;
private @Value(\\\"#{departmentProperties[\\\'departmentBean.departmentname\\\']}\\\")
String departmentName;
@Bean(name = \\\"departmentBean\\\")
public Department addDepartment() {
Department department = new Department();
department.setDepartmentId(departmentId);
department.setDepartmentName(departmentName);
return department;
}
}
package com.config.inheritance.annotation;
public class User {
private String userName;
private Long salary;
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
public class User {
private String userName;
private Long salary;
public Long getSalary() {
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Step 04
package com.config.inheritance.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanApp {
/**
* Using property \'EL\' syntax to load values from the user.properties value
*/
private @Value(\"#{userProperties[\'userBean.username\']}\")
String userName;
private @Value(\"#{userProperties[\'userBean.salary\']}\")
Long salary;
/*
* Injected bean
*/
@Bean(name = \"userBean\")
public User addUser() {
User user = new User();
user.setUserName(userName);
user.setSalary(salary);
return user;
}
}
Step 05:
package com.config.inheritance.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanApp {
/**
* Using property \'EL\' syntax to load values from the user.properties value
*/
private @Value(\"#{userProperties[\'userBean.username\']}\")
String userName;
private @Value(\"#{userProperties[\'userBean.salary\']}\")
Long salary;
/*
* Injected bean
*/
@Bean(name = \"userBean\")
public User addUser() {
User user = new User();
user.setUserName(userName);
user.setSalary(salary);
return user;
}
}
package com.config.inheritance.annotation.TEST;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.config.inheritance.annotation.User;
import com.config.inheritance.annotation.UserBeanApp;
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from UserBeanApp
@ContextConfiguration(classes = { UserBeanApp.class })
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanTest {
/**
* Using property \'EL\' syntax to load values from the user.properties value
*/
private @Value(\"#{userProperties[\'userBean.username\']}\")
String userName;
private @Value(\"#{userProperties[\'userBean.salary\']}\")
Long salary;
@Autowired
User userBean;
@Test
public void testAddUser() {
assertThat(userBean.getUserName(), equalTo(userName));
assertThat(userBean.getSalary(), equalTo(salary));
}
}
Step 06:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.config.inheritance.annotation.User;
import com.config.inheritance.annotation.UserBeanApp;
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from UserBeanApp
@ContextConfiguration(classes = { UserBeanApp.class })
// spring config that loads the properties file
@ImportResource(\"classpath:/properties-config.xml\")
public class UserBeanTest {
/**
* Using property \'EL\' syntax to load values from the user.properties value
*/
private @Value(\"#{userProperties[\'userBean.username\']}\")
String userName;
private @Value(\"#{userProperties[\'userBean.salary\']}\")
Long salary;
@Autowired
User userBean;
@Test
public void testAddUser() {
assertThat(userBean.getUserName(), equalTo(userName));
assertThat(userBean.getSalary(), equalTo(salary));
}
}
package com.config.inheritance.annotation.TEST;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import com.config.inheritance.annotation.Department;
import com.config.inheritance.annotation.DepartmentBeanApp;
@ContextConfiguration(classes = { DepartmentBeanApp.class })
@ImportResource(\"classpath:/properties-config.xml\")
public class DepartmentBeanTest extends UserBeanTest {
/**
* Using property \'EL\' syntax to load values from the department.properties
* value
*/
private @Value(\"#{departmentProperties[\'departmentBean.id\']}\")
String departmentId;
private @Value(\"#{departmentProperties[\'departmentBean.departmentname\']}\")
String departmentName;
@Autowired
Department departmentBean;
@Test
public void testAddDepartment() {
assertThat(departmentBean.getDepartmentId(), equalTo(departmentId));
assertThat(departmentBean.getDepartmentName(), equalTo(departmentName));
}
}
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import com.config.inheritance.annotation.Department;
import com.config.inheritance.annotation.DepartmentBeanApp;
@ContextConfiguration(classes = { DepartmentBeanApp.class })
@ImportResource(\"classpath:/properties-config.xml\")
public class DepartmentBeanTest extends UserBeanTest {
/**
* Using property \'EL\' syntax to load values from the department.properties
* value
*/
private @Value(\"#{departmentProperties[\'departmentBean.id\']}\")
String departmentId;
private @Value(\"#{departmentProperties[\'departmentBean.departmentname\']}\")
String departmentName;
@Autowired
Department departmentBean;
@Test
public void testAddDepartment() {
assertThat(departmentBean.getDepartmentId(), equalTo(departmentId));
assertThat(departmentBean.getDepartmentName(), equalTo(departmentName));
}
}
No comments:
Post a Comment