I am trying to implement an application for different profiles using Spring 3 but i am not sure how it develop using Spring 3. I have couple of confusions
- When you develop application using PropertyPlaceholderConfigurer, then how do you create session factor object that pass to the DAO classes?
- How can i create profiles for each property file. Let say i want to pass an "Dev" parameter then application ready property files from dev.proertie file, and then pass it to the application.
- I just read some tutorials on google, most of the tutorials follow the hard coded profile name in every class like @Profile("dev") in every DAO class. Is there any way to do it automatically means how to inject into DAO classes? i dont want to add profile name in every class
Here are the List of my application
This is Simple Model Object.
** Employee.Java**
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This Class deals with Database. So i want to have session object in this class rather than Datasource object.
Employee DAO class
public class EmployeeDao {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(Employee employee){
String sql = "insert into employee(name) VALUES (?)";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, employee.getName());
ps.executeUpdate();
ps.close();
conn.close();
}
catch (SQLException sqle) {
System.out.println("Sql Exception " + sqle.getMessage());
}
}
This class actually reads the data from property file but not sure how to build up session object that inject in my DAO Class
Config.java
public class Config {
@Bean
public static PropertyPlaceholderConfigurer properties(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ClassPathResource[] resources = new ClassPathResource[ ]
{ new ClassPathResource( "db.properties" ) };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
@Value( "${jdbc.url}" ) private String jdbcUrl;
@Value( "${jdbc.driverClassName}" ) private String driverClassName;
@Value( "${jdbc.username}" ) private String username;
@Value( "${jdbc.password}" ) private String password;
@Bean(name="datasource")
public DataSource datasource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(jdbcUrl);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
@Bean(name="employee")
public Employee employee() {
Employee employee = new Employee();
employee.setName("Sahil");
return employee;
}
@Bean(name="employeeDao")
public EmployeeDao employeeDao() {
EmployeeDao employeeDao = new EmployeeDao();
employeeDao.setDataSource(datasource());
return employeeDao;
}