Spring dependency injection depending on request object -


i working on web application spring. application configured properties file. there multiple instances of application in different servers, each instance has different configuration file (each instance customized different customer) using controllers , services. this:

public class controller1 {     @autowired     service1 service1;      @requestmapping(value = "/page.htm", method = { requestmethod.get, requestmethod.post })     public modelandview serve(httpservletrequest request, httpservletresponse response) {         service1.dosomething();         return new modelandview("/something");     } }  @service public class service1 {     @autowired     service2 service2;     public void dosomething () {             …             service2.doanotherthing();             …         } }  @service public class service2 {     @value("${propertyvalue}")     private string propertyvalue;      //doanotherthing()  use propertyvalue     public void doanotherthing () {         …         //do propertyvalue         …         } } 

now have new requirement. there won’t multiple instances each customer, 1 instance multiple domains customers. application must decide configuration depending on host name of request object in controller. if customer points browser www.app1.com have use configuration file 1 if customer uses www.app2.com have use configuration 2 , on.

i moved configuration files database, realized not know how make dependency injection. services linked, service1 uses service2 , service2 1 must use value depends on configuration. service 2 has no knowledge of request object.

is there clean way solve this?

thanks,

one approach create configuration object customer singleton on spring config:

<bean id="customeraconfig"../> <bean id="customerbconfig"../> <bean id="customercconfig"../> 

and have session scoped configurationservice acts pointer configuartion active

public class configurationservice {     private customerconfig activeconfig;     // getters & setters.. } 

configure singleton proxy service on spring config can injected singleton components. need have cglib in classpath spring create proxy:

<bean class="com.mycompany.configurationservice" scope="session">   <aop:scoped-proxy/> </bean> 

and on login controller, select configuration should used virtual host name , store configurationservice later retrieval (remember configurationservice session scoped)

public class logincontroller {    @autowired private customerconfig[] custconfigs;   @autowired private configurationservice configservice;    @requestmapping(method = post)   public string login(httpservletrequest request, ..) {     ...     string host = request.getservername();     customerconfig activeconfig = // decide 1 based on host..     configservice.setactiveconfig(activeconfig);     ...   } } 

below sample foocontroller reads customer specific configuration

@controller @requestmapping("/foo") public class foocontroller {    @autowired private configurationservice configservice;    @requestmapping(method = "get")   public string get() {     ...     customerconfig config = configservice.getactiveconfig();     ...   }    ... } 

if program not have single entry point login page, can code similar logic filter. check if active configuration set on session, if not based on host name


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -