2

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.

apines
  • 1,254
  • 14
  • 36

1 Answers1

0

Well, what IntelliJ is saying is correct as you admit that your Comp is just a pojo and you havent Autowired it. so the error is correct as when spring tries to initialize a Bean of Op according to,

@Bean
@Scope(SCOPE_PROTOTYPE)
Op op(Comp comp) {      // <--- Intellij marks this as error
    return new Op(composition);
}

Obviously you don't have a Comp bean. So error is correct.

But for the question why it really works, Actually as spring cannot initialize the bean it just skips it and then what happens is something like this,

Op op(Comp comp) {     
    return new Op(composition);
}

@Bean
OpFactory opFactory() {
    return new OpFactory() {
        @Override
        public Op createOp(Comp comp) {
            return op(comp);
        }
    };
}

So why not work ? :D The main problem is your usage, if you have a factory returning whatever objects, it cannot return a single bean. Bean itself is singleton and here your configs should be changed accordingly. So I guess you need something like above.

Damith
  • 740
  • 4
  • 12
  • Thanks, I've updated and clarified my question. Also note that `Op` is `prototype` so we can have multiple beans of it each one with different `comp` – apines Oct 01 '18 at 06:28