Can @Autowire @Service beans but not members of a class instantiated with “new” keyword - even with context scanning configured?

If you just instantiate an object with new Spring is not involved, so the autowiring won't kick in. Component scanning looks at classes and creates objects from them - it doesn't look at objects you create yourself.

If you just instantiate an object with new, Spring is not involved, so the autowiring won't kick in. Component scanning looks at classes and creates objects from them - it doesn't look at objects you create yourself. This can be made to work, using Spring's AspectJ support, but it takes some effort.

Otherwise, you need to let Spring instantiate your objects if you wan autowiring to work. Should I just pass the blendService to the BlueValidator and forget about the Autowiring In your situation, I'd say yes, this is the least effort solution.

When you instantiate objects spring cannot do anything for them, so it does not get the dependencies injected (article). In your case, you have a couple of options: pass dependencies to the validator from the controller (where you can inject them) make the validator a spring bean and inject it, instead of instantiating it use @Configurable, which, via AspectJ, enables spring injection even in objects created with new.

Autowired is being used by Spring's ApplicationContext to populate those fields on creation. Since the ApplicationContext is not the one creating these beans (you are because of the keyword 'new'), they are not being autowired. You need to pass it in yourself if you are creating it.

Don't create validator manually -- allow to Spring do this work for you: @Controller class Controller { @Autowired BlueValidator validator; void doSomething() { ... validator. Validate(colorBlend, bindResult); ... } } Also pay attention that adding package com.myapp. Validators to context:scan-packages not enough, you also should annotate your validator class with @Component annotation: @Component public class BlueValidator implements Validator { @Autowired private BlendService blendService; (BTW this solution works in my project.).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions