I have the following classes and configuration in my project:
public class Op {
public Op(Comp comp) {
// Construct Op
}
}
public interface OpFactory {
public Op createOp(Comp comp);
}
And the Spring configuration:
@Configuration
public class OpConfig {
@Bean
@Scope(SCOPE_PROTOTYPE)
Op op(Comp comp) { // <--- Intellij marks this as error
return new Op(composition);
}
@Bean
OpFactory opFactory() {
return new OpFactory() {
@Override
public Op createOp(Comp comp) {
return op(comp);
}
};
}
}
This code works, however IntelliJ IDEA shows an error in the configuration stating Could not autowire. No beans of 'Comp' type found.
. Comp itself is a pojo and doesn't need to be autowired, and as you can see the factory supplies the Op ctor with one.
The code works, just IntelliJ doesn't like it. I usually listen to IntelliJ and I don't just want to suppress this with @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
, is there a better way to configure the beans and IntelliJ has a point?
Let me know if there's more information and/or code that I can provide.
Edit:
The same error occurs when I omit the factory bean and use BeanFactory#getBean(String name, Object... args)
as suggested in this answer. The factory approach is basically the same just to avoid additional calls to BeanFactory#getBean(String name, Object... args)
.
Also, I don't need the Comp
to be autowired because once I have the factory I can simply pass it along, and as I said comp itself it a pojo. Example usage:
Comp comp = new Comp();
OpFactory opFactory = getFactoryFromContext();
opFactory.createOp(comp);
I don't need comp to be autowired, in fact I can't have it autowired since I am building it based on user input at runtime.