How to avoid circular dependency in java? I am using webservice but i dont know how to inject a class. I heard about MyApplicationBinder for injecting the class but i dont know how to use it?
In my project structure is like this
1.Controller
-UserController
-TeamController
-TournamentController
private UserService userService = new UserServiceImpl();
//for accessing Service class I used this way how to inject this class in controller
2.Service
-UserServiceImpl
-TeamServiceImpl
-TournamentServiceImpl
private UserDAO userDAO = new UserDAOImpl();
//for accessing DAO class I used this way how to inject this class in service
And one more problem in TeamServiceImpl class i need TournamentServiceIml class resource and at the same time in TournamentServiceImlclass I need TeamServiceImpl class resource(means i need to call method).Obviously it may cause circular dependency.How to avoid this situation
3.DAO
-UserDAOImpl
-TeamDAOImpl
-TournamentDAOImpl
This is my ResourceConfig class
MyApplication.java
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig
{
public MyApplication()
{
register(JacksonJsonProvider.class);
register(MultiPartFeature.class);
registerClasses(UserController.class);
registerClasses(TeamController.class);
registerClasses(TournamentController.class);
registerClasses(FileStorageController.class);
register(new MyApplicationBinder());
}
}
MyApplicationBinder.java
public class MyApplicationBinder extends AbstractBinder {
@Override
protected void configure() {
}
}
public class TeamServiceImpl implements TeamService
{
private TeamDAO teamDAO = new TeamDAOImpl();
private UserService userService = new UserServiceImpl(); //for accesing UserService
private FileStorageService fileStorageService = new FileStorageServiceImpl();////for accesing FileService
private TournamentService userService = new TournamentServiceImpl();//for accesing TournamentService
..........
}
public class TournamentServiceImpl implements TournamentService
{
private TournamentDAO tournamentDAO = new TournamentDAOImpl();
private UserService userService = new UserServiceImpl();
private TeamService teamService = new TeamServiceImpl();
//cant get this TeamService when we call it occurs circular dependency
private FileStorageService fileStorageService = new FileStorageServiceImpl();
..........
}
Please help me I am new this technology now I am stuck in this project.I think you understood my situation
1.How to inject class in ControllerLayer,ServiceLayer,DAOLayer?
2.How to avoid circular dependency in this situation?
3.How to use Constructor injection?Some people told me thats the better way,how to use it
Please give me a proper guidance about this..