How to use provider for dependency injection using guice in playframework
I am using playframework 2.4 here is my code
trait UserRepositoryTrait {
val userRepo:UserRepository
val sessionRepo:SessionRepository
}
class UserRepositoryImpl extends UserRepositoryTrait {
@Inject @Named("userRepo")val userRepo:UserRepository= null
@Inject @Named("sessionRepo") val sessionRepo:SessionRepository = null
}
and here is module class
class UserDependencyModule extends AbstractModule {
@Override
protected def configure() {
bind(classOf[UserRepository]).annotatedWith(Names.named("userRepo")).toInstance(new UserRepo)
bind(classOf[SessionRepository]).annotatedWith(Names.named("sessionRepo")).toInstance(new SessionRepo)
bind(classOf[UserRepositoryTrait]).to(classOf[UserRepositoryImpl])
}
}
in application.conf
play.modules.enabled += "models.guice.UserDependencyModule"
everything works fine if I inject this trait in a controller but i want to inject this trait into a class here is the code
class StatusChange @Inject() (userRepository:UserRepositoryTrait) {
}
and i need to callStatusChange.scala in Service.scala class
how can i instantiate StatusChange.scala object
class ArtworkService() {
val status= new StatusChange(//what should i add here?)
}
i did read on providers but I am unable to understand how can I use it for my scenario ?please guide me
update
if i do it like this will be correct ?
class ArtworkService @inject (userRepository: UserRepositoryTrait) {
val status= new StatusChange(userRepository)
}
and in the controller
class MyController @inject (userRepository: UserRepositoryTrait)
{
val artworkService = new ArtworkService(userRepository)
}
java scala playframework guice playframework-2.4
|
show 1 more comment
I am using playframework 2.4 here is my code
trait UserRepositoryTrait {
val userRepo:UserRepository
val sessionRepo:SessionRepository
}
class UserRepositoryImpl extends UserRepositoryTrait {
@Inject @Named("userRepo")val userRepo:UserRepository= null
@Inject @Named("sessionRepo") val sessionRepo:SessionRepository = null
}
and here is module class
class UserDependencyModule extends AbstractModule {
@Override
protected def configure() {
bind(classOf[UserRepository]).annotatedWith(Names.named("userRepo")).toInstance(new UserRepo)
bind(classOf[SessionRepository]).annotatedWith(Names.named("sessionRepo")).toInstance(new SessionRepo)
bind(classOf[UserRepositoryTrait]).to(classOf[UserRepositoryImpl])
}
}
in application.conf
play.modules.enabled += "models.guice.UserDependencyModule"
everything works fine if I inject this trait in a controller but i want to inject this trait into a class here is the code
class StatusChange @Inject() (userRepository:UserRepositoryTrait) {
}
and i need to callStatusChange.scala in Service.scala class
how can i instantiate StatusChange.scala object
class ArtworkService() {
val status= new StatusChange(//what should i add here?)
}
i did read on providers but I am unable to understand how can I use it for my scenario ?please guide me
update
if i do it like this will be correct ?
class ArtworkService @inject (userRepository: UserRepositoryTrait) {
val status= new StatusChange(userRepository)
}
and in the controller
class MyController @inject (userRepository: UserRepositoryTrait)
{
val artworkService = new ArtworkService(userRepository)
}
java scala playframework guice playframework-2.4
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
Fundamentally the question is: what is the root of the stack trace of the thread that will create yourArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?
– SergGr
Nov 28 '18 at 2:08
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
@swaheed, It is still not clear what is the problem for you with a straightforward approach ofclass MyController @Inject() (artworkService : ArtworkService)
– SergGr
Nov 28 '18 at 13:03
|
show 1 more comment
I am using playframework 2.4 here is my code
trait UserRepositoryTrait {
val userRepo:UserRepository
val sessionRepo:SessionRepository
}
class UserRepositoryImpl extends UserRepositoryTrait {
@Inject @Named("userRepo")val userRepo:UserRepository= null
@Inject @Named("sessionRepo") val sessionRepo:SessionRepository = null
}
and here is module class
class UserDependencyModule extends AbstractModule {
@Override
protected def configure() {
bind(classOf[UserRepository]).annotatedWith(Names.named("userRepo")).toInstance(new UserRepo)
bind(classOf[SessionRepository]).annotatedWith(Names.named("sessionRepo")).toInstance(new SessionRepo)
bind(classOf[UserRepositoryTrait]).to(classOf[UserRepositoryImpl])
}
}
in application.conf
play.modules.enabled += "models.guice.UserDependencyModule"
everything works fine if I inject this trait in a controller but i want to inject this trait into a class here is the code
class StatusChange @Inject() (userRepository:UserRepositoryTrait) {
}
and i need to callStatusChange.scala in Service.scala class
how can i instantiate StatusChange.scala object
class ArtworkService() {
val status= new StatusChange(//what should i add here?)
}
i did read on providers but I am unable to understand how can I use it for my scenario ?please guide me
update
if i do it like this will be correct ?
class ArtworkService @inject (userRepository: UserRepositoryTrait) {
val status= new StatusChange(userRepository)
}
and in the controller
class MyController @inject (userRepository: UserRepositoryTrait)
{
val artworkService = new ArtworkService(userRepository)
}
java scala playframework guice playframework-2.4
I am using playframework 2.4 here is my code
trait UserRepositoryTrait {
val userRepo:UserRepository
val sessionRepo:SessionRepository
}
class UserRepositoryImpl extends UserRepositoryTrait {
@Inject @Named("userRepo")val userRepo:UserRepository= null
@Inject @Named("sessionRepo") val sessionRepo:SessionRepository = null
}
and here is module class
class UserDependencyModule extends AbstractModule {
@Override
protected def configure() {
bind(classOf[UserRepository]).annotatedWith(Names.named("userRepo")).toInstance(new UserRepo)
bind(classOf[SessionRepository]).annotatedWith(Names.named("sessionRepo")).toInstance(new SessionRepo)
bind(classOf[UserRepositoryTrait]).to(classOf[UserRepositoryImpl])
}
}
in application.conf
play.modules.enabled += "models.guice.UserDependencyModule"
everything works fine if I inject this trait in a controller but i want to inject this trait into a class here is the code
class StatusChange @Inject() (userRepository:UserRepositoryTrait) {
}
and i need to callStatusChange.scala in Service.scala class
how can i instantiate StatusChange.scala object
class ArtworkService() {
val status= new StatusChange(//what should i add here?)
}
i did read on providers but I am unable to understand how can I use it for my scenario ?please guide me
update
if i do it like this will be correct ?
class ArtworkService @inject (userRepository: UserRepositoryTrait) {
val status= new StatusChange(userRepository)
}
and in the controller
class MyController @inject (userRepository: UserRepositoryTrait)
{
val artworkService = new ArtworkService(userRepository)
}
java scala playframework guice playframework-2.4
java scala playframework guice playframework-2.4
edited Nov 28 '18 at 9:28
swaheed
asked Nov 24 '18 at 12:32
swaheedswaheed
1,40541648
1,40541648
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
Fundamentally the question is: what is the root of the stack trace of the thread that will create yourArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?
– SergGr
Nov 28 '18 at 2:08
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
@swaheed, It is still not clear what is the problem for you with a straightforward approach ofclass MyController @Inject() (artworkService : ArtworkService)
– SergGr
Nov 28 '18 at 13:03
|
show 1 more comment
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
Fundamentally the question is: what is the root of the stack trace of the thread that will create yourArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?
– SergGr
Nov 28 '18 at 2:08
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
@swaheed, It is still not clear what is the problem for you with a straightforward approach ofclass MyController @Inject() (artworkService : ArtworkService)
– SergGr
Nov 28 '18 at 13:03
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
Fundamentally the question is: what is the root of the stack trace of the thread that will create your
ArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?– SergGr
Nov 28 '18 at 2:08
Fundamentally the question is: what is the root of the stack trace of the thread that will create your
ArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?– SergGr
Nov 28 '18 at 2:08
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
@swaheed, It is still not clear what is the problem for you with a straightforward approach of
class MyController @Inject() (artworkService : ArtworkService)– SergGr
Nov 28 '18 at 13:03
@swaheed, It is still not clear what is the problem for you with a straightforward approach of
class MyController @Inject() (artworkService : ArtworkService)– SergGr
Nov 28 '18 at 13:03
|
show 1 more comment
1 Answer
1
active
oldest
votes
You don't need to use provider here. Provider is useful to resolve cyclic dependency references.
In your case, you can simply do:
class ArtworkService @Inject (status: StatusChange) {}
And then direct inject ArtworkService in your controller:
class MyController @Inject (artworkService: ArtworkService) {}
Provider would have come into the picture, if you had a cycle reference. For example:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
Here, we have cycle ArtworkService -> StatusChange && StatusChange -> ArtworkService So, provider comes in rescue in these situations. You can resolve this by:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458181%2fhow-to-use-provider-for-dependency-injection-using-guice-in-playframework%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need to use provider here. Provider is useful to resolve cyclic dependency references.
In your case, you can simply do:
class ArtworkService @Inject (status: StatusChange) {}
And then direct inject ArtworkService in your controller:
class MyController @Inject (artworkService: ArtworkService) {}
Provider would have come into the picture, if you had a cycle reference. For example:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
Here, we have cycle ArtworkService -> StatusChange && StatusChange -> ArtworkService So, provider comes in rescue in these situations. You can resolve this by:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
add a comment |
You don't need to use provider here. Provider is useful to resolve cyclic dependency references.
In your case, you can simply do:
class ArtworkService @Inject (status: StatusChange) {}
And then direct inject ArtworkService in your controller:
class MyController @Inject (artworkService: ArtworkService) {}
Provider would have come into the picture, if you had a cycle reference. For example:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
Here, we have cycle ArtworkService -> StatusChange && StatusChange -> ArtworkService So, provider comes in rescue in these situations. You can resolve this by:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
add a comment |
You don't need to use provider here. Provider is useful to resolve cyclic dependency references.
In your case, you can simply do:
class ArtworkService @Inject (status: StatusChange) {}
And then direct inject ArtworkService in your controller:
class MyController @Inject (artworkService: ArtworkService) {}
Provider would have come into the picture, if you had a cycle reference. For example:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
Here, we have cycle ArtworkService -> StatusChange && StatusChange -> ArtworkService So, provider comes in rescue in these situations. You can resolve this by:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
You don't need to use provider here. Provider is useful to resolve cyclic dependency references.
In your case, you can simply do:
class ArtworkService @Inject (status: StatusChange) {}
And then direct inject ArtworkService in your controller:
class MyController @Inject (artworkService: ArtworkService) {}
Provider would have come into the picture, if you had a cycle reference. For example:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkService: ArtworkService) {}
Here, we have cycle ArtworkService -> StatusChange && StatusChange -> ArtworkService So, provider comes in rescue in these situations. You can resolve this by:
class ArtworkService @Inject (status: StatusChange) {}
class StatusChange @Inject (artworkServiceProvider: Provider[ArtworkService]) {}
answered Dec 4 '18 at 6:03
Tarang BhalodiaTarang Bhalodia
964417
964417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458181%2fhow-to-use-provider-for-dependency-injection-using-guice-in-playframework%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your ArtworkService will also need to inject a UserRepositoryTrait.
– James Whiteley
Nov 26 '18 at 14:52
then how can i pass UserRepostiryTrait argument to ArtworkService class the problem still persists (the class will change only) i need to know how to pass these arguments which requires the @injected instances
– swaheed
Nov 27 '18 at 9:32
Fundamentally the question is: what is the root of the stack trace of the thread that will create your
ArtworkService? In other words what makes that code run? If this is a chain of calls that originates from some Controller, than you can have a chain of relevant @Inject along the way (in the reverse direction i.e. every time injecting the component that will be called). If the code is run by some other means, how exactly is it run?– SergGr
Nov 28 '18 at 2:08
its run by a controller please see the question again i have edited it
– swaheed
Nov 28 '18 at 9:28
@swaheed, It is still not clear what is the problem for you with a straightforward approach of
class MyController @Inject() (artworkService : ArtworkService)– SergGr
Nov 28 '18 at 13:03