Results 1 to 5 of 5

Thread: Initialising static property of class through injection on startup

  1. #1
    Join Date
    Sep 2007
    Posts
    2

    Default Initialising static property of class through injection on startup

    Hi all,i am new to spring.Recently we have started using spring in the project.
    Below is my code that i am using


    Code:
    public class DSManager {
      private static DataSource dataSource = null;   
      public static void setDataSource(DataSource dataSource) {
        DSManager.dataSource = dataSource;
      }
      public DataSourceManager() {
    
      }
    
       public static DataSource getDataSource() {
        return  dataSource ;}

    And this is the construct from my applicationContext.xml


    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:comp/env/jdbc/abc</value>
    </property>
    </bean>

    <bean id="initialiseDataSource" class="com.abc.DSManager " >
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    </bean>

    I have one requirement.First i want to initialise the static datasource property at the start of the application.Whenever i try to run this application
    i get this Exception


    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'initialiseDataSource' defined in URL classes/springApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'dataSource' of bean class :
    Bean property 'dataSource' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
    org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'dataSource' of bean class :
    Bean property 'dataSource' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:670)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:572)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:737)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:764)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:753)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1057)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:857)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:378)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:233)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:145)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:283)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:313)
    at org.springframework.web.context.support.AbstractRe freshableWebApplicationContext.refresh(AbstractRef reshableWebApplicationContext.java:139)
    at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:246)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:184)
    at org.springframework.web.context.ContextLoaderServl et.init(ContextLoaderServlet.java:83)
    at javax.servlet.GenericServlet.init(GenericServlet.j ava:211)
    at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1091)
    at org.apache.catalina.core.StandardWrapper.load(Stan dardWrapper.java:925)
    at org.apache.catalina.core.StandardContext.loadOnSta rtup(StandardContext.java:3857)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4118)
    at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:759)
    at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:739)
    at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:524)
    at org.apache.catalina.startup.HostConfig.deployDescr iptor(HostConfig.java:589)
    at org.apache.catalina.startup.HostConfig.deployDescr iptors(HostConfig.java:536)
    at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:471)
    at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1102)
    at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1020)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:718)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1012)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:442)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:450)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:683)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:537)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:271)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:409)

  2. #2
    Join Date
    Aug 2005
    Location
    Sussex, UK
    Posts
    92

    Default

    Maybe worth reviewing comments on the following JIRA relating to the injection of statics through Spring.
    http://opensource.atlassian.com/proj...rowse/SPR-3845

  3. #3
    Join Date
    Sep 2004
    Posts
    602

    Default

    A couple of questions:

    Why are you using a datasource in one of your own classes DSManager ? The Spring framework provides you with classes like JdbcDaoSupport and HibernateDaoSupport into which you inject datasources. I've never had to do anything with a datasource in one of my own classes.

    If you do need to have your own DSManager why do you need to statically initialize the datasource ? What is wrong with a pojo DSManager that you inject the datasource into ?

  4. #4
    Join Date
    Feb 2010
    Posts
    1

    Wink Use factory-method and cheat spring.

    Well, may be this option is not quite right in srping terms but, this work for me, and at last i have got only one instance in my context.

    Set static properties in a static factory method.

    i mean set in appContext.xml as follows

    Code:
    <bean id="properties"
    		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    		<property name="location"
    			value="classpath:/a/property/file/Utils.properties" />
    	</bean>
    
    	<bean class="your.class.UtilMessages" factory-method="setInstance" autowire="autodetect" scope="singleton">
    	 	<constructor-arg ref="properties" />
    	</bean>
    The interest thing is in factory method and the argument.

    So the class looks like...

    Code:
    public class UtilMessages {
    
    	public static Properties properties;
    		
    	// Cheating spring to set a static property.
    	public static Properties setInstance(Properties propertiesArgs) {
    		properties = propertiesArgs;
    		return properties;
    	}
    
    	public static String getMsg(String key) {
    		return properties.getProperty(key);
    	}
    }
    I did call it "setInstance" because i'm setting the value, even when it's a cosntructor.

    So you can set any static property in context instantantion and use them after as any static value.

    For me was a property file, so i can read it by calling static UtilsMessages.getMsg("key") method. No need to set dependency in xml file and no need to create a new instance each time i need it.

    Regadrs.

  5. #5

    Default Static Factory Methods & DI

    I found similar thread here but I haven't seen a clear response so I figured to post my question specifically.

    Assume you have a factory class that it requires other objects to be injected to utilize its static methods.

    Code:
    public class TestUtilFactory{
    	private static IAddressDao addressDao;
    	
    	private static IUserDao userDao;
    	
    	public static Address createAddress(){
    // do some magic here utilizing the static instances addressDao
    	}
    the addressDao & userDao are already managed by container and available to be injected but none of@Autowired, @Resource nor the static-factory injection works.

    Any suggestion would be greatly appreciated.

    Thank,
    Arash

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •