-
Notifications
You must be signed in to change notification settings - Fork 3
Using with Spring Boot and other DI
Muhammad Hewedy edited this page Jun 8, 2019
·
8 revisions
The page idea is mainly from
stored-procedure-proxyframework here: https://github.com/marschall/stored-procedure-proxy/wiki/Object-Lifetime
When use with spring and other DI frameworks, it is suggested to cache the DAO interfaces, for example in Spring boot, register the DAO interface as a Spring Bean.
The following code snippet from demo project of spwrap with spring-boot
@Configuration
public class Config {
@Autowired
private DataSource dataSource;
@Bean
public DAO dao(){
return new DAO.Builder(dataSource)
.config(new spwrap.Config().useStatusFields(false))
.build();
}
@Bean
public CustomerDAO customerDAO(DAO dao){
return dao.create(CustomerDAO.class);
}
}You can use the spring boot starter
@Configuration
public class Config {
/*DAO bean will be defined by the starter
https://github.com/mhewedy/spwrap-spring-boot-starter
*/
@Bean
public CustomerDAO customerDAO(DAO dao){
return dao.create(CustomerDAO.class);
}
}The following code snippet from demo project of spwrap with CDI
public class Config {
//DataSource is defined in the application server
@Resource(mappedName = "java:jboss/datasources/TestDS")
private DataSource dataSource;
@Produces
@Singleton
public DAO dao(){
System.out.println("init DAO");
return new DAO.Builder(dataSource)
.config(new spwrap.Config().useStatusFields(false))
.build();
}
@Produces
@Singleton
public CoffeeDAO coffeeDAO(DAO dao){
System.out.println("init CoffeeDAO");
return dao.create(CoffeeDAO.class);
}
}staring from 0.0.18, calls to spwrap DAO methods inside Spring Transactions, will be affected by that transaction, read more.