#Spring Bean Creation Order

37 messages · Page 1 of 1 (latest)

tawdry monolith
#

Hey folks, I'm working on a project that's using Spring Framework.

It's using a library X which has a class A with a method B.
I've another class C with a method D which is a part of my project.
Now, classes A & C are annotated with @Component and methods B & D are annotated with @PostConstruct.

During the start of my application, I need to ensure that bean of A is created before bean of C and essentially, method B gets executed before D.
Note that since A is coming from a library, I cannot modify it.

Can someone help with this?

past oreBOT
#

This post has been reserved for your question.

Hey @tawdry monolith! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

short belfry
#

make A a dependency of C?

tawdry monolith
#

How to do that? (sorry, a little new to this) 😓

#

Using parameterized constructor?
But there already is a constructor in C that has been marked with @Autowired and is being used somewhere else, can't change it

short belfry
#

and you shouldn't call constructors of Spring components by yourself

tawdry monolith
#

but it's being used somewhere, that I don't think if I can change

short belfry
#

Alternatively you could use an @Autowired field but these things just get you into other issues

tawdry monolith
# short belfry Where is it used?

Actually, I might be able to modify that call
Another thing is, this class C is a aggregated into another class where that field is annotated as @Autowired, like this:

public class SomeClass {
  @Autowired
  C c;
}

will Spring handle that if I add another parameter into @Autowired marked constructor?

short belfry
tawdry monolith
#

Great, let me try

tawdry monolith
past oreBOT
# tawdry monolith Thanks, this works!!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

tawdry monolith
#

Is this the best way? considering code practices and all

short belfry
tawdry monolith
#

for making sure of one bean being created before another

short belfry
#

It's recommended to use constructor injection instead of @Autowired on fields

short belfry
tawdry monolith
#

Yeah, that I know, but the project I'm working on is flooded with @Autowired, it'll take some time to remove all that

short belfry
#

Alternatively, @Order should also do it

short belfry
tawdry monolith
#

How do I use @Order

short belfry
short belfry
tawdry monolith
#

I mean, the bean I wanna priortize, i.e. A, is coming from a library, can't change order of that. And to depriortize, I need to have an order value for C greater than that of A

short belfry
short belfry
#

that tells Spring that A is required for B

tawdry monolith
tawdry monolith
short belfry