Wicket Dependency Injection - java

I've got a page with a form in Wicket where the form requires a collaborator to get its job done. The collaborator is injected (for which I'm using Guice) and looks something like:
public class RegistrationPage extends WebPage {
#Inject
public RegistrationPage(RegistrationService service) {
this.service = service;
add(new RegistrationForm());
}
private class RegistrationForm extends Form {
public RegistrationForm() {
// setup
}
protected void onSubmit() {
service.doSomething();
}
}
}
I don't like the idea that the RegistrationService is injected into the RegistrationPage when it's just the RegistrationForm that needs it. I could change the RegistrationForm to receive the RegistrationService:
public RegistrationForm(RegistrationService service) {
this.service = service;
}
and remove the field from the RegistrationPage, but the RegistrationPage is still being used to do the pass-through.
I guess what I'm asking is what the best-practise is for doing this? Is this ok to do, or would it perhaps be better to inject the RegistrationForm itself into the Page:
public class RegistrationPage extends WebPage {
#Inject
public RegistrationPage(RegistrationForm form) {
add(form);
}
}
---
private class RegistrationForm extends Form {
private RegistrationService service;
#Inject
public RegistrationForm(RegistrationService service) {
this.service = service;
}
protected void onSubmit() {
service.doSomething();
}
}
I'd prefer this as I'd like to have the RegistrationForm in a separate class/file. I'm quite new to Wicket so unsure of what the norm is - can someone show me the guiding light? :)

the basic paradigm with wicket+ioc is: most dependencies should be injected via setter injection. constructor injection is impossible for WebPages.
components/panels/forms/pages should only be on the recieving end.
so, inject the dependency to RegistrationService happily into the RegistrationForm , then create it in the RegistrationPage with add(new RegistrationForm());
wicket has IComponentInstantiationListener - one of them is guice. they get notified during the constructor of each component/webpage. so your RegistrationForm will have its dependencies injected before any part of your code can execute.
the way i would do it:
(of course RegistrationForm can be in another file)
public class RegistrationPage extends WebPage {
#Inject
public RegistrationPage() {
add(new RegistrationForm());
}
---
private static class RegistrationForm extends Form {
RegistrationService service;
#Inject
public void setRegistrationService (RegistrationService service){
this.service = service;
}
public RegistrationForm() {
// setup
}
protected void onSubmit() {
service.doSomething();
}
}
}
if you decide to put the RegistrationForm inside the Page as inner class, remember to declare it static! you will most likely not need any references to the enclosing class.

In my opinion DI on Wicket has one confusing element, is done automagically on Components or Pages but not in Models. In my opinion exactly Model (but not page) should have dependency to JPA etc.
Official doc say to use InjectorHolder.getInjector().inject(this)

Related

How to Mock an injected object that is not declared in Module?

For a dagger2 module
#Module
public class MyModule {
#Provides #Singleton public RestService provideRestService() {
return new RestService();
}
#Provides #Singleton public MyPrinter provideMyPrinter() {
return new MyPrinter();
}
}
We could have the test module as Test
public class TestModule extends MyModule {
#Override public MyPrinter provideMyPrinter() {
return Mockito.mock(MyPrinter.class);
}
#Override public RestService provideRestService() {
return Mockito.mock(RestService.class);
}
}
However if for a class as below that is not declared in the dagger module...
public class MainService {
#Inject MyPrinter myPrinter;
#Inject public MainService(RestService restService) {
this.restService = restService;
}
}
How do I create a mock of MainService as above.
Note, I'm not planning to perform test for MainService as per share in https://medium.com/#fabioCollini/android-testing-using-dagger-2-mockito-and-a-custom-junit-rule-c8487ed01b56#.9aky15kke, but instead, my MainService is used in another normal class that I wanted to test. e.g.
public class MyClassDoingSomething() {
#Inject MainService mainService;
public MyClassDoingSomething() {
//...
}
// ...
public void myPublicFunction() {
// This function uses mainService
}
}
This is definitely not answering your question, but in my honest opinion it is related, it's helpful and too big for a comment.
I'm often facing this question and I end always doing "Constructor dependency injection". What this means is that I no longer do field injection by annotating the field with #Inject but pass the dependencies in the constructor like so:
public class MyClassDoingSomething implements DoSomethig {
private final Service mainService;
#Inject
public MyClassDoingSomething(Service mainService) {
this.mainService = mainService;
}
}
Notice how the constructor now receives the parameter and sets the field to it and is also annotated with #Inject? I also like to make these classes implement an interface (also for MyService) - Amongst several other benefits I find it makes the dagger module easier to write:
#Module
public class DoSomethingModule {
#Provides #Singleton public RestService provideRestService() {
return new RestService();
}
#Provides #Singleton public MyPrinter provideMyPrinter() {
return new MyPrinter();
}
#Provides #Singleton public Service provideMyPrinter(MyService service) {
return service;
}
#Provides #Singleton public DoSomethig provideMyPrinter(MyClassDoingSomething something) {
return something;
}
}
(This assumes that MyService implements or extends Service)
By now it seems you already know that dagger is able to figure out the dependency graph by itself and build all the objects for you. So what about unit testing the class MyClassDoingSomething? I don't even use dagger here. I simply provide the dependencies manually:
public class MyClassDoingSomethingTest {
#Mock
Service service;
private MyClassDoingSomething something;
#Before
public void setUp() throws Exception {
MockitoAnnotations.init(this);
something = new MyClassDoingSomething(service);
}
// ...
}
As you see, the dependency is passed through the constructor manually.
Obviously this doesn't work if you're coding something that doesn't have a constructor that can be invoked by you. Classical examples are android activities, fragments or views. There are ways to achieve that, but personally I still think you can somehow overcome this without dagger. If you are unit testing a view that has a field #Inject MyPresenter myPresenter, usually this field will have package access that works fine in the tests:
public class MyViewTest {
#Mock MyPresenter presenter;
private MyView view;
#Before
public void setUp() throws Exception {
MockitoAnnotations.init(this);
view.myPresenter = presenter;
}
}
Note that this only works if both MyViewTest and MyView are in the same package (which often is the case in android projects).
At the end of the day if you still want to use dagger for the tests, you can always create "test" modules and components that can inject by declaring methods in the component like:
#Inject
public interface MyTestComponent {
void inject(MyClassDoingSomething something);
}
I find this approach ok-ish, but throughout my development years I prefer the first approach. This also has reported issues with Robolectric that some setup in the build.gradle file is required to actually make the dagger-compiler run for the tests so the classes are actually generated.

Autowiring service in self instantiated HorizontalLayout

Is there a common way to pass an autowired service to a self instantiated view? Let's say we have a VerticalLayout managed by Spring (#Autowired works) and a HorizontalLayout a buttons inside. We need to create multiple instances of the HorizontalLayout and the button performs some action of the service (#Autowired doesn't work, Service is null). What is the best practice to solve this problem? Just pass the service as a parameter to the constructor of the HorizontalLayout is an option, but perhaps there is a better way.
#Service
public class SomeService {
public void someMethod(){
// do something
}
}
#SpringView(name = "SomeView")
#ViewScope
public class SomeView extends VerticalLayout {
#Autowired
private SomeService service;
public SomeView(){
addComponent(new Subview());
addComponent(new Subview());
}
}
public class SubView extends HorizontalLayout {
#Autowired
private SomeService service;
public SubView(){
Button btn = new Button("Test");
btn.addClickListener(e->service.someMethod());
addComponent(btn);
}
}
EDIT: I added a code example. Objects of SubView get null for service. A solution would be to pass the service to the constructor. But I wanted to know, if there is a better solution.
Do you mean a Vaadin View or just a regular Component? A View must implement the View interface (com.vaadin.navigator.View) and thus you can use the constructor to Autowire the Service and the enter() method to create your ui:
#SpringView(name = "SomeView")
#ViewScope
public class SomeView extends VerticalLayout implements View {
private SomeService service;
#Autowired
public SomeView(SomeService service){
this.service = service;
}
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
//service is available here
removeAllComponents();
addComponent(new Subview());
addComponent(new Subview());
}
}
The way it works is that Spring will instantiate SomeView and in the process it will autowire your service through the constructor. Then, whenever the View is called it will use the enter() method to draw its components.
If it's just a regular component (I think that is your case), then instead of enter() use the #PostConstruct annotation in a method. For example:
#Autowired
public SomeView(SomeService service){
this.service = service;
}
#PostConstruct
public void init() {
//service is available here
addComponent(...);
}
I haven't tested this annotation but it's widely documented in Vaadin. Please let me know if it works for you.

Jersey HK2 injection in manually created objects

Is there any way to inject dependencies into manually created objects?
public class MyCommand {
#Inject Repository repository;
}
public Repository {
#Inject EntityManager em;
}
MyCommand command = new MyCommand();
Repository is properly registered the jersey ResourceConfig and can be injected in objects that are created through the CDI container for example a resource class.
But since I create the Command myself the #Inject annotation gets ignored.
Is there a way to get a registered class beside #Inject and #Context?
Something like Application.get(Repository.class)
public class MyCommand {
Repository repository;
public MyCommand() {
repository = Application.get(Repository.class);
}
}
----- EDIT -----
Thanks to your help and some rethinking I found a solution for my problem.
The first thing is that it's possible to inject the ServiceLocator without any preperation into you objects.
The second thing is that I moved from normal commands with a execute method to a a command bus system.
The reason for that is I have no controle over the creation of commands so there clean way to get dependencies injected.
The new approach looks like this:
class CommandBus {
private final ServiceLocator serviceLocator;
#Inject
public CommandBus(ServiceLocator serviceLocator) {
this.serviceLocator = serviceLocator;
}
public void dispatch(Command command) {
Class handlerClass = findHandlerClassForCommand(command);
CommandHandler handler = (CommandHandler) serviceLocator.getService(handlerClass);
handler.handle(command);
}
}
interface CommandHandler {
void handle(Command command);
}
interface Command {
}
class ConcreteCommand implements Command {
// I'm just a dto with getters and setters
}
class ConcreteHandler implements CommandHandler {
private final SomeDependency dependency;
#Inject
public ConcreteHandler(SomeDependency dependency) {
this.dependency = dependency;
}
#Override
public void handle(ConcreteCommand command) {
// do some things
}
}
And in my resources I have something like this:
#Path("/some-resource")
class Resource {
#Context
private CommandBus bus;
#POST
#Consumes(MediaType.APPLICATION_JSON)
public void runCommand(ConcreteCommand command) {
bus.dispatch(command);
}
}
As pointed out by jwells - HK2 is an injection framework :)
I spent some time looking into it - I have to say, I find it much more complicated than say guice or spring. Maybe this is due to the fact that I use Dropwizard and it makes it not as easy to access the Service locators.
However, here is how you can do that.
First, you will have to get a reference to your ServiceLocator. It must be the same ServiceLocator that jersey is using as well. You can access it for example like:
How to get HK2 ServiceLocator in Jersey 2.12?
In my example code I will use an event listener, which is due to my Dropwizard Setup.
You now have 2 choices: Register your command with your Service Locator and have the injection framework handle creation, or pass the ServiceLocator to your command in order to use it.
I wrote up a quick example using Dropwizard and jersey:
public class ViewApplication extends io.dropwizard.Application<Configuration> {
#Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(new ApplicationEventListener() {
#Override
public void onEvent(ApplicationEvent event) {
if (event.getType() == ApplicationEvent.Type.INITIALIZATION_FINISHED) {
ServiceLocator serviceLocator = ((ServletContainer) environment.getJerseyServletContainer())
.getApplicationHandler().getServiceLocator();
ServiceLocatorUtilities.bind(serviceLocator, new AbstractBinder() {
#Override
protected void configure() {
bind(new Repository("test")).to(Repository.class);
bind(MyCommandInjected.class).to(MyCommandInjected.class);
}
});
MyCommandInjected service = serviceLocator.getService(MyCommandInjected.class);
MyCommandManual tmp = new MyCommandManual(serviceLocator);
}
}
#Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
return null;
}
});
}
#Override
public void initialize(Bootstrap<Configuration> bootstrap) {
super.initialize(bootstrap);
}
public static void main(String[] args) throws Exception {
new ViewApplication().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml");
}
#Path("test")
#Produces(MediaType.APPLICATION_JSON)
public static class HelloResource {
#GET
#Path("asd")
public String test(String x) {
return "Hello";
}
}
public static class Repository {
#Inject
public Repository(String something) {
}
}
public static class MyCommandInjected {
#Inject
public MyCommandInjected(final Repository repo) {
System.out.println("Repo injected " + repo);
}
}
public static class MyCommandManual {
public MyCommandManual(final ServiceLocator sl) {
Repository service = sl.getService(Repository.class);
System.out.println("Repo found: " + service);
}
}
}
In the Run method, i get access to my ServiceLocator. I bind my classes in there (so there is an example of how to do that). You can alternatively also register Binders with jersey directly - they will use the correct ServiceLocator.
The 2 classes MyCommandInjected and MyCommandManual are examples of how you can create this command.
The relevant line for you is probably:
Repository service = sl.getService(Repository.class);
This asks the service locator for a new instance of the Repository.
Now, this is just a quick example. I am much more fond of the guice bridge than using HK2 directly :) I find it much easier to use and much clearer. Using the guice-jersey-bridge you can do everything through guice and it will automatically do the right thing.
Hope that brings some inside,
Artur
You can use the inject method of ServiceLocator in order to inject already created objects. ServiceLocator is the basic registry of HK2 and should be available in your resource.

Is it common to have dependency between facade layer?

My application have the follow layer:
- Facade
- Business Object
- Repository (Spring JPA Data)
Let's suppose the follow classes:
#Component
public class MessageFacade implements MessageService {
#Autowired
private GarageBO garageBO;
#Autowired
private MessageBO messageBO;
public void createFeedbackMessage(...) {
messageBO.createFeedbackMessage(...);
garageBO.createFeedback(...);
}
}
#Component
public class ServiceOrderFacade implements ServiceOrderService {
#Autowired
private ServiceOrderBO serviceOrderBO;
#Autowired
private MessageBO messageBO;
#Autowired
private GarageBO garageBO;
public void createServiceOrder(...) {
serviceOrderBO.createServiceOrder(...);
messageBO.createFeedbackMessage(...);
garageBO.createFeedback(...);
}
}
Observing the createFeedbackMessage method in MessageFacade i have:
1) The createFeedbackMessage method in MessageBO it's responsible to create an email with the feedback survey LINK;
2) The createFeedbackMessage method in garageBO creates the Feedback ENTITY with the questions and responses;
On createServiceOrder method in ServiceOrderFacade i need to call an method of ServiceOrderBO and after i need to have the same behavior of createFeedbackMessage method in MessageFacade.
Is it a bad idea to create a dependency between ServiceOrderFacade -> MessageFacade ?
The code would be:
#Component
public class ServiceOrderFacade implements ServiceOrderService {
#Autowired
private ServiceOrderBO serviceOrderBO;
#Autowired
private MessageBO messageBO;
#Autowired
private GarageBO garageBO;
public void createServiceOrder(...) {
serviceOrderBO.createServiceOrder(...);
getMessageService().createFeedbackMessage(...);
}
}
If you think in DRY(don't repeat yourself) you have an problem. You can create another layer like a common that is dependency of Business Object layer and will be created once and will be used in your entire BO if U want

Basic Google Guice injection. Playframework 2.1

I'm very new to Google Guice and I'm having troubles to get UserService instanced.
In my Playframework application, I have a service called UserService which looks like this:
public class UserService { // Note it doesn't implement an interface
private UserDao userDao;
private Email email;
#Inject
public UserService(UserDao userDao, Email email) {
this.userDao = userDao;
this.email = email;
}
...
}
I have this controller:
public class UserController extends Controller {
#Inject
private UserService userService;
...
}
I have this configure() definition for my UserModule:
protected void configure() {
bind(UserDao.class).to(UserDaoJpa.class);
bind(Email.class).to(EmailHtml.class);
bind(UserController.class);
}
I get the injector on Playframework Global object which looks similar to the example provided by Guillaume Bort about getting a controller instance, which in this case fits perfectly for getting the injector (the method getControllerInstance is a new feature in Play 2.1, but this is not relevant here). See here if interested:
public class Global extends GlobalSettings {
private static final Injector injector = createInjector();
#Override
public <A> A getControllerInstance(Class<A> controllerClass) throws Exception {
return injector.getInstance(controllerClass);
}
private static Injector createInjector() {
return Guice.createInjector(new UsuarioModule());
}
}
At this point, everything works perfectly, the service is correctly instantiated with its parameters resolved. The controller gets the object graph as expected.
But, when I try to do #Inject UserService userService somewhere else in the application I get null for userService. For example:
public class EmailAvailableValidator {
#Inject
private static UserService userService; // This is not resolved :(
public static Map<String, List<ValidationError>> validateEmailAndGetErrorsIfAny(String email, String emailField) {
Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>();
if (!userService.isEmailAvailable(email)) {
List<ValidationError> list = new ArrayList<ValidationError>();
list.add(new ValidationError("", UsuarioResource.getMessageEmailTaken()));
errors.put(emailField, list);
}
return errors;
}
}
The question is, what is correct way to get an instance of an object that doesn't implement an interface? Is it always necessary to implement an interface?
Isn't it supposed that guice knows how to solved UserDao and Email? Why it is not able to instantiate it except in UserController?
I need this instance with its dependencies resolved, I mean with UserDao and Email into it.
The official guice docs were not very helpful for me.
Thanks for your help!
Two things:
You need to use a Guice Injector to instantiate EmailAvailableValidator. Your "Global" class is doing that already. Using the injector creates an instance and then sets all of its injected fields.
You might have to use the static injection to fill a static field. For example, in your configure() method add:
requestStaticInjection(UserService.class);
However, I haven't personally used it so YMMV.
Reference:
http://code.google.com/p/google-guice/wiki/Injections

Categories

Resources