SyntaxHighlighter

Showing posts with label Active Profiles. Show all posts
Showing posts with label Active Profiles. Show all posts

Tuesday, August 20, 2013

Spring Context PropertyPlaceholder and ActiveProfiles

Spring Context PropertyPlaceholder and ActiveProfiles

so here's a little design requirement that i ran into; i want to use the < context:property-placeholder /> with different configurations in my application context dependent on the Active Profile.  that is, in "production" i want it to read from a properties file, but for testing, i want to set all the values via properties object defined in Spring.

normally, with ActiveProfiles you can declare a bean definition in the regular bean namespace then define it again (with whatever is required) in the beans (profile) tag.  this doesn't work with the < context:property-placeholder /> as it doesn't have an id attribute.  (there's a bunch more sophisticated/accurate/clever reasons as to why, but that's the most readily identifiable).  as such, the profile won't override it, but instead create a second one.  no good for the design.

so here's the solution; default profile.  essentially we create all our beans, then create a default profile and inside it, declare our production < context:property-placeholder />. to declare our test or alternative profile, we just do the same again.



 
  
 

 
  
 
 
 
  
  
  
   hello world
  
 





the default profile is the profile picked up by "default" when the application context is started.  as such, we don't need to specify it and it is akin to not wrapping anything in profiles.  it helps us in this case where we don't want a particular element to be referenced at all if we have a profile in play.

Friday, July 12, 2013

Exposing Active Profiles as a JNDI value in Tomcat

Exposing Active Profiles as a JNDI value in Tomcat

here's a quick one for exposing Active Profiles in JNDI for Tomcat.  the use case is when you don't want to set an Active Profile in web.xml (or webappcontextinitializer) but you need them for Tomcat.  (say you're developing against a Tomcat container, but deploying against something else)

so, here's the class

package de.incompleteco.spring.tomcat;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;


public class ActiveProfilesObjectFactory implements ObjectFactory {

	static final String VALUE = "value";
	static final String DELIMITER = ",";
	
	@Override
	public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable < ?, ? > environment) throws Exception {
		//init
		String values = null;
		//retrieve
		Reference reference = (Reference) obj;
		Enumeration < RefAddr > addresses = reference.getAll();
		while (addresses.hasMoreElements()) {
			RefAddr address = addresses.nextElement();
			String attributeName = address.getType();
			String attributeValue = address.getContent().toString();
			if (attributeName.equalsIgnoreCase(VALUE)) {
				values = attributeValue;
				break;
			}//end if
		}//end while
		if (values != null) {
			return values.split(DELIMITER);
		}//end if
		return null;//default --> no active profiles
	}

}


and here's how you would access it in Tomcat.  (remember, the location is [your app]/META-INF/context.xml)




	
		

Thursday, April 18, 2013

A quick note about Spring Profiles

A quick note about Spring Profiles


Spring Profiles are cool little tools to enable you to keep your configuration together and yet cater for different environments.  (such as testing datasources versus production datasources).  The neat thing to note is that the "" tag(s) are always at the bottom of the XML, forcing bean definitions inside them to overwrite any that came before.



    
    

    
    
            
        
    




in the example above, it allows you to define two beans with the same name, and have the "local" configuration automatically overwrite the production.

here's the stackoverflow question where i try and put this together http://stackoverflow.com/questions/16062922/using-jndi-datasource-with-spring-batch-admin/16080997