3

I was wondering, what is the best way to get beans in helper classes since you cannot use @Autowired there. What I'm doing now is that I autowire in @Service/@Component/... and then pass that bean in constructor to the helper class. I don't think that it is the proper way of doing it since there might be too many things to pass.

What is the best way of doing this ?

Robert Vangor
  • 1,018
  • 1
  • 13
  • 24

4 Answers4

2

I'm assuming the Helper Class is not Spring-managed. There is the option of using @Configurable (requires AspectJ) for this class. Take a look at the Documentation for more info.

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • Example : I have too much code in one method in @Service and I want to create a new class out of it (class that represents Video). I'm doing it through "new" operator and passing that object in constructor, but I also have to pass a repository with it, because I cannot autowire in that class. Is this bad thing to do ? – Robert Vangor Aug 13 '17 at 12:54
  • Can't this logic be abstracted in a dedicated Service bean? – dimitrisli Aug 13 '17 at 12:56
  • Yes, that's what I tried at first, but I need to use private variables and multiple private methods. Is this thing that should be done in service bean ? – Robert Vangor Aug 13 '17 at 13:00
  • Say you have ServiceA that is the one we are talking about. With all the private methods and other Beans that are needed for the say HelperA class. It sounds like HelperA is dependent on ServiceA rather then other way around? If this is not the case please post some code since this gets hard to track – dimitrisli Aug 13 '17 at 13:07
  • http://i.imgur.com/rkJHkwX.png VideoUploadHelper is just normal Java class, not Spring bean. I could transfer it to @Service, but idk if it is okay that service will have private methods/variables – Robert Vangor Aug 13 '17 at 13:14
  • 1
    Ok I understand. Basically the VideoUploadHelper class has no reason for existence since it requires all dependencies of VideoUploadService, the VideoUploadService itself along with the Model. You can do all that within VideoUploadService directly. But not on Private methods but rather on Protected so you are able to test them. By the way is this a Controller class? Looks like it because of the Model. Also if both your UploadServices have same Interface I suggest you @Autowired List and iterate on them agnostic of the impl to upload the video. – dimitrisli Aug 13 '17 at 13:22
1
public class SpringContextHolder implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }
}

<bean class="SpringContextHolder" lazy-init="false"/> xml configuration.

Now you can use the static context like SpringContextHolder.applicationContext.getBean(name).

aristotll
  • 8,694
  • 6
  • 33
  • 53
  • What about using protoype scope ? – Robert Vangor Aug 13 '17 at 12:33
  • 1
    @RobertVangor That is a different thing. Maybe this question is what you want https://stackoverflow.com/questions/14156278/is-it-possible-to-retrieve-a-spring-bean-with-prototype-scope-without-using-appl – aristotll Aug 13 '17 at 12:42
0

you can implement ApplicationContetAware to get the application context, then put it in a (static?) variable.

public class MyClass implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}

You can make applicationContext static and pass the application context to it as well.

then use applicationContext.getBean(...)

apines
  • 1,254
  • 14
  • 36
0

I assume helper classes are just but do some static method operations. Basic examples for helper classes are EmployeeUtil, ProductUtil, etc., if you see your process can be done through static method; then skip using @Autowired and access in static way.

@Configurable is a marker used by the AOP load-time weaving; may be a overhead in this case.

If you still see a need of @Autowired object inside, injecting through container will gives clear code, instead of creating object every time using new keyword.