1

I'm trying to initialize Spring bean with request scope, and lazy initialization from another object (attributes) that are not managed by Spring. Here is the bean definition :

@Component
@Scope(value = "request")
@Lazy
public class LazyClass {

 protected String name;

}

How do I init the 'name' attribute at runtime ?

sharon182
  • 533
  • 1
  • 4
  • 19

1 Answers1

-1

It may not be the best solution but you can set the value of name into a System property and get it where you need it in your class.

Setting the value somewhere in a non-spring class:

// myProperty is the name of the property
// name is the value you want to store
System.setProperty("myProperty",name);

Getting inside your class:

// Caution: the name of the property must be the same as it was when it was set
String name = System.getProperty("myProperty);

The getting method can be called in the constructor of your bean class as it is marked as @Lazy.

EDIT:

Another way is to create a setter for the field name and set its value when you need it.

thunder197
  • 37
  • 4