Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make spring inject value into a static field

I know this may looks like a previously asked question but I'm facing a different problem here.

I have a utility class that has only static methods. I don't and I won't take an instance from it.

public class Utils{     private static Properties dataBaseAttr;     public static void methodA(){      }      public static void methodB(){      } } 

Now I need Spring to fill dataBaseAttr with database attributes Properties.Spring config is:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  <util:properties id="dataBaseAttr"         location="file:#{classPathVariable.path}/dataBaseAttr.properties" /> </beans> 

I already done it in other beans but the problem here in this class (Utils) isn't a bean, And if I make it a bean nothing changes I still can't use the variable since the class will not be instantiated and variable always equals null.

like image 394
Osama FelFel Avatar asked Jul 04 '12 07:07

Osama FelFel


People also ask

How do you inject a value to a static field?

Solution First, PropertyController, which is a RestController, is being initialized by Spring. Afterward, Spring searches for the Value annotated fields and methods. Spring uses dependency injection to populate the specific value when it finds the @Value annotation.

Can static field be Autowired?

In short, no. You cannot autowire or manually wire static fields in Spring.

How do you inject Spring values?

Method 3 : Using @Value annotation This method involves applying @Value annotation over bean properties whose values are to be injected. The string provided along with the annotation may either be the value of the bean field or it may refer to a property name from a properties file loaded earlier in Spring context.


1 Answers

You have two possibilities:

  1. non-static setter for static property/field;
  2. using org.springframework.beans.factory.config.MethodInvokingFactoryBean to invoke a static setter.

In the first option you have a bean with a regular setter but instead setting an instance property you set the static property/field.

public void setTheProperty(Object value) {     foo.bar.Class.STATIC_VALUE = value; } 

but in order to do this you need to have an instance of a bean that will expose this setter (its more like an workaround).

In the second case it would be done as follows:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">     <property name="staticMethod" value="foo.bar.Class.setTheProperty"/>     <property name="arguments">         <list>             <ref bean="theProperty"/>         </list>    </property> </bean> 

On you case you will add a new setter on the Utils class:

public static setDataBaseAttr(Properties p) 

and in your context you will configure it with the approach exemplified above, more or less like:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">     <property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/>     <property name="arguments">         <list>             <ref bean="dataBaseAttr"/>         </list>    </property> </bean> 
like image 169
Francisco Spaeth Avatar answered Sep 21 '22 07:09

Francisco Spaeth