Reputation: 62549
Right now i have a inherited project that is using annotation based spring dependency injection. So all classes are simply marked with @Component (or specific stereoTypes like @service, @Repository,@RestController, etc). This makes it a little hard to find where the dependency is located and i was thinking to change it so that each package has its own dependency configuration and then add each package to the @ComponentScan afterwards.
So for example if i had a package called com.mycoolpackage.login and mycoolpackage.networking then i'd have a Spring configuration like this in first package:
@Configuration
public class LoginDIConfig {
@Bean
public LoginServiceImpl loginServiceImpl() {
return new LoginServiceImpl();
}
}
and in the second package i'd have the following:
@Configuration
public class NetworkDIConfig {
@Bean
public NetworkServiceImpl networkServiceImpl() {
return new NetworkServiceImpl();
}
}
and my@ComponentScan would look like this:
@ComponentScan(basePackages = {"com.mycoolpackage.login","com.mycoolpackage.network"})
So i have two questions about this approach.
Upvotes: 0
Views: 187
Reputation: 99
If you want to configure some been properties manually then you should go for above configuration else you should stick with exiting one.
This makes it a little hard to find where the dependency is located
@Autowire Or @Inject annotation will always lead you to dependency class.
Upvotes: 1