Rishabh Agrawal
Rishabh Agrawal

Reputation: 25

Inject a bean which is filled in a different class

Suppose i have 2 files

A.java

@RequiredArgsConstructor(onConstructor =  @__(@Autowired))
@Component
public class A {
    private bean1 bean1_name1;
    bean1_name.property1 = "Value1"
}


B.java

public class B {
    private bean1 bean1_name2;

}

I want to autoinject the bean1 object which was modified in A.java to be injected in class B. So basically when i call bean1_name2 it should already have the bean1_name.property1 = "Value1" in it. Obviously this assumes that class A runs before class B.

Please do let me know if you need more context.

Thank You.

Upvotes: 1

Views: 488

Answers (1)

Laugslander
Laugslander

Reputation: 398

Marking property1 of bean1 as static solves the problem. The static keyword indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class.

Upvotes: 1

Related Questions