Using with Spring Boot and other DI - mhewedy/spwrap GitHub Wiki

The page idea is mainly from stored-procedure-proxy framework 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.