As mentioned by @Janar, Spring can only autowire beans into Spring-managed classes.
You can add this tricky implementation which allows you get ActionListMain outside of @Component.
We are going to centralize all bean injection (Spring + Custom) in BeansManager.
PRO / CONT
- All new services automatically will be instanced
- Centralized initiation solves
Circular Dependencies in Spring
- You need to call
.getBean to initiate manually
Dependencies
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
Classes which implements Injectable need to have constructor with no args
Injectable interfaces to get your @Service / @Component instances
public interface Injectable {
/**
* Initiate all beans dependencies.
* @param manager Beans manager allows to get singleton instances
*/
void init(BeansManager manager);
}
BeansManager class
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.reflections.Reflections;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeansManager {
@Autowired
private List<Injectable> injectables;
/**
* This method will make sure all the injectable classes will get the
* BeansManager in its steady state, where it's class members are ready to be
* set.
*/
@PostConstruct
protected void inject() throws Exception {
// Init all Spring @Component classes
for (Injectable injectableItem : injectables) {
injectableItem.init(this);
}
//Setup your package to scan
Reflections reflections = new Reflections("com.example.demo");
Set<Class<? extends Injectable>> classes = reflections.getSubTypesOf(Injectable.class);
for (Class<? extends Injectable> clazz : classes) {
// Avoid calling twicely if clazz already initialized by Spring
if (getBean(clazz, Boolean.FALSE) == null) {
Method init = clazz.getDeclaredMethod("init", BeansManager.class);
init.invoke(clazz.newInstance(), this);
}
}
}
/**
* Get singleton from BeansManager.
*
* @param <T> Spring service / component class
* @param clazz singleton class
* @return Singleton / throws exception
* @throws NoSuchBeanDefinitionException If bean not found (required=true)
*/
public <T> T getBean(Class<T> clazz) {
return getBean(clazz, Boolean.TRUE);
}
/**
* Get singleton from BeansManager.
*
* @param <T> Component service / component class
* @param clazz singleton class
* @param required If bean not found, it throw exception (true) or returns null
* (false)
* @return Singleton / null / throws exception
* @throws NoSuchBeanDefinitionException If bean not found (required=true)
*/
public <T> T getBean(Class<T> clazz, boolean required) {
Object bean = null;
for (Injectable injectableItem : injectables) {
if (clazz.isInstance(injectableItem)) {
bean = clazz.cast(injectableItem);
return clazz.cast(bean);
}
}
if (required) {
throw new NoSuchBeanDefinitionException(clazz);
} else {
return null;
}
}
}
Implement Injectable for your classes
@Service
public class ActionListMain implements Injectable
...
public class MyTable implements Injectable
...
@Override
public void init(BeansManager manager) {
allActions = manager.getBean(ActionListMain.class);
}
...
public class Action implements Injectable
private static ActionListMain actionListMain;
@Override
public void init(BeansManager manager) {
actionListMain = manager.getBean(ActionListMain.class);
}
@Component
public class StartupComponent implements Injectable
...
@Override
public void init(BeansManager manager) {
actionListMain = manager.getBean(ActionListMain.class);
}
Initialize your application
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
ActionListMain actionListMain;
@Autowired
StartupComponent startupComponent;
Action action = new Action();
MyTable myTable = new MyTable();
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
actionListMain.sayHello();
startupComponent.sayHello();
action.sayHello();
myTable.sayHello();
}
}
com.example.demo.ActionListMain: ready
class com.example.demo.StartupComponent com.example.demo.ActionListMain@d28c214
class com.example.demo.Action com.example.demo.ActionListMain@d28c214
class com.example.demo.MyTable com.example.demo.ActionListMain@d28c214