user1008893
user1008893

Reputation:

Getting rid of @Autowired (removing Spring dependency)

I was recently given a Java project to work on that contains a bit of Spring, and I was asked to get rid of it. I don't know anything about Spring and would rather not spend a day learning about it.

Most of the dependency I can see is with @Autowired. Do you have any advice for how to remove @Autowired? Is it as simple as making a singleton and setting the @Autowired fields manually?

Upvotes: 2

Views: 3179

Answers (3)

Chenhai-胡晨海
Chenhai-胡晨海

Reputation: 1385

If you use constructor dependency injection, then it is ok to remove @autowired annotation. System will determine it as default

Upvotes: 0

Kees van Dieren
Kees van Dieren

Reputation: 1290

Do you know the standard Java javax.inject.Inject dependency injection annotation? Spring also supports that one for resolving dependencies, if that class is on the classpath. In most situations you could simply replace the Spring @Autowired with @Inject. See the Javadoc of @Inject and the Spring reference on @Inject support.

Upvotes: 2

Simon Forsberg
Simon Forsberg

Reputation: 13331

Spring detects the @Autowired fields and sets the field to one of the "beans" that exists and can be configured with Spring.

Even though the beans are mostly singletons, I believe it is possible for Spring to use non-singleton autowirings as well.

Since you have not provided any code (which I can understand why), I suggest you look at the fields that are autowired and determine if they are used as singletons or not. If they are, then yes it can be as simple as making a singleton and setting the fields manually.

Upvotes: 3

Related Questions