Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about custom ParametersProvider (@CustomParameters) #165

Open
kkdrz opened this issue Jun 25, 2019 · 1 comment
Open

Question about custom ParametersProvider (@CustomParameters) #165

kkdrz opened this issue Jun 25, 2019 · 1 comment

Comments

@kkdrz
Copy link

kkdrz commented Jun 25, 2019

Hello,
I have my own annotation with some configuration

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CustomParameters(provider = MyCustomProvider.class)
public @interface MyCustomAnnotation {

    String[] namesOfFilesToSearch() default { "something.txt, something2.json" };
}

and provider:

public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {
    ...
    @Override
    public Object[] getParameters() {
      // a lot of business logic, searching for files and so on...
     return someArrayWithMyCustomObjects;
    }

I would love to split whole logic from getParameters() into many classes and then inject them via constructor. How can I do that? Is it even possible?

The result I want:

public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {
    
    public FunctionalTestsProvider(FilesManager filesManager, SomeBusinessExecutor business) {
        this.filesManager = filesManager;
        this.business = business;
    }
    ...
    @Override
    public Object[] getParameters() {
      ...
      filesManager.doSomething();
      business.doSomethingElse();
     return someArrayWithMyCustomObjects;
    }
@peterjurkovic
Copy link
Contributor

peterjurkovic commented Jul 21, 2019

It is, but not via constructor. What you could do is use some sort of Dependency injection (ServiceLocator) and pass your context via ThreadLocal.

Spring uses this technique in SpringBeanAutowiringSupport

(Spring way example):

public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {

    private FilesManager filesManager;

    public MyCustomProvider() {
       // this would inject your dependency 
       SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants