1

I would like to implement a RESTful web service using Jersey/Spring which switches between application contexts depending on the host name to which the request was made (similar to virtual hosting).

For example the requests made to site1.domain.com and site2.domain.com should have different sets of service beans working with different databases, users, etc.

What is the best way to do it?

EDIT: the application contexts are supposed to have identical bean classes. The difference is the database used. Also they have to be dynamic, i.e. defined/destroyed during application runtime.

Would be nice to know if this is possible with Spring at all and what is the starting point to search for. Most of the information I found is related to static configuration during webapp init.

dimitri
  • 874
  • 8
  • 11
  • 1
    Possible duplicate http://stackoverflow.com/questions/9470551/spring-property-substitution-for-test-and-production – ring bearer Jun 28 '15 at 19:43

1 Answers1

0

You need to use a Custom Spring bean scope depending on the request, to manage multiple EMF/SessionFactory, with multiple Units.

@Scope("dynamic")
@Bean(name ="erpEMF")   
public LocalContainerEntityManagerFactoryBean erpManagerFactory() {                     
    LocalContainerEntityManagerFactoryBean emf = buildEmf();
    return emf;
}   

@Scope("dynamic")
@Bean(name ="erpJPA")
public JpaTransactionManager erpTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();     
    transactionManager.setPersistenceUnitName("erpUnit");       
    return transactionManager;
}

@Scope("dynamic")
@Bean(name ="erpDataSource", destroyMethod=EmfHolder.DataSourceCloseMethod)
public DataSource erpDataSource() {                 
    return dynamicDataSource( DB_NAMES.AGI_ERP ); 
}
public class DynamicScope implements Scope{
  public Object get(String name, ObjectFactory<?> objectFactory) {
    //return Service depending on your request
}
}
@Scope("dynamic")
@Service
public class ActAccountService extends ErpGenericService<ActAccount> implements IActAccountService {
    @Transactional("erpJPA")
    public Account create(Account t){
    }
}
Nassim MOUALEK
  • 4,702
  • 4
  • 25
  • 44