1

I have checked this link How to make spring inject value into a static field

But , my requirement is little different. I know it is against Spring's IoC , but I need to do this workaround.

package foo.package

@Controller  
public class SomeController{

 public static int var = -1;

 private String someStringField = null;
 // n- number of non-static fields
 /**
 * Constructor
 */
 public SomeController(){
    someStringField  = "some Value" ; // This is just for example.
 }


}

Now , I need to add a static block and set the static field var with some value from Spring's context xml.

According to the code segment given in the link , can I call clinit in place of method name ?

Thanks in advance.

Community
  • 1
  • 1
DukeLover
  • 2,184
  • 2
  • 19
  • 27
  • 4
    I don't quite understand your case, but I suggest you to look at `@PostConstruct` annotation - init method is probably a better choice to configure your bean with Spring context – Vladimir Nov 10 '15 at 16:06
  • @Vladimir : Thanks a lot for your reply. My requirement is to initialize the static variable even before the constructor is called. `init-method` might be a solution , but won't that mess up `@Controller` annotation ? I mean , spring context might be messed up . – DukeLover Nov 11 '15 at 03:44
  • @Vladimir : Thanks a lot for reply. As you can see , there is constructor defined in the class. If I use `@PostConstuct` , the constructor will be called first , right ? But , I want to set the static variable even before that. So , I have no other option left than calling it in static block . Can I do that ? – DukeLover Nov 11 '15 at 03:51
  • 1
    well, yes, first the no-args constructor is called, injection by spring next, and finally init-method is called. if you need a variable in constructor, then yes, you need to set it in static block. however, chances are there might not be spring context yet when static block is called. but you can think of init-method as a sort of constructor, and move your initialization logic there, if it's possible. – Vladimir Nov 11 '15 at 08:13
  • @Vladimir : Thanks a lot once again , for clear and concise reply. Can you please put this as reply to the question , so that I can select it as best answer. Also , if you feel my question has little bit of research effort , can you please upvote it. – DukeLover Nov 11 '15 at 10:04
  • I just don't feel like i've properly answered your question. looks more like an advice to me =) – Vladimir Nov 11 '15 at 10:08
  • @Vladimir : Thank you :) , you are really humble and generous :) – DukeLover Nov 11 '15 at 10:19

2 Answers2

1

You can also add an autowired setter that will initialize the var.

package foo.package

@Controller  
public class SomeController{

 public static int var = -1;

 private String someStringField = null;
 // n- number of non-static fields
 /**
 * Constructor
 */
 public SomeController(){
    someStringField  = "some Value" ; // This is just for example.
 }

 @Value("${your.property}")
 public void setVar(Integer var) {
     SomeController.var = var;
 }


}
0

@Vladimir :

I think I found out a workaround though it is not elegant . It is done in context xml.

I found that the root cause of the problem was coming due to <context:component-scan>

I was calling the static method after the component scan , i.e I was doing

<bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 

after the <component scan>. So , I just changed the order , and it is setting properly.

I guess , spring context was scanning the annotations and instantiating them. Reversing the order did the trick.

Thanks a lot for your support. :-)

DukeLover
  • 2,184
  • 2
  • 19
  • 27