I upgraded my package from Spring 4.3 to Spring 5.3 which has caused a issue I am completely to solve, for some reason a Bean that is created and works under 4.3 is being created, but not wired in. The bean is being created, and the configuration has not changed, yet it doesn't work?
Bean Creation:
#Configuration
#SuppressWarnings("checkstyle:HideUtilityClassConstructor")
#Slf4j
public class EnvironmentConfig {
#Bean
public static AppEnvironmentHelper environmentHelper() throws IOException {
final AppEnvironmentHelper.Config config = new AppConfigEnvironmentHelper.Confg()
// Config object is configured
return new AppEnvironmentHelper(config);
}
}
Attempted Injection of Bean:
#Configuration
#Slf4j
public class OtherConfig {
#Value("${role}")
private String someRole;
#Value("${port}")
private String somePort;
#Autowired
private EnvironmentHelper environmentHelper;
.......
}
The class path for the bean is in the configuration, and when launching the service I see logs that indicate that EnviromentConfig is created before OtherConfig
org.springframework.beans.factory.support.DefaultListableBeanFactory: Finished creating instance of bean 'environmentHelper'
But I always get
UnsatisfiedDependencyException: Error creating bean with name 'OtherConfig': Unsatisfied dependency expressed through field 'environmentHelper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.amazon.coral.spring.EnvironmentHelper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
After it errors spring goes to clean up the beans with
Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#40db2a24: defining beans [.....,EnvironmentConfig, environmentHelper ]
Which seems to indicate to me that it is finding the bean, its creating the bean, but then when it comes to wire in enviromentHelper it doesnt find it. Ive tried naming the bean using qualifiers but that did nothing. And I wrapped the EnviromentHelper and added logs during creation of the bean and destruction, so I know the bean isnt being destroyed prematurely. Im not certain what else to try to troubleshoot further.
Related
i use JUnit 5 with Spring Boot 2.5.2 . Now i want to write a Unit test, that does not load the full Application context.
Therefore i annotate my test like that:
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = {ConfigurationA.class})
class Test1{
...
}
In ConfigurationA the Bean1 gets created.
The Problem is that the ConfigurationA accesses an ConfigurationB for creating Bean1, but ConfigurationB is protected.
Now i get the following Error:
Error creating bean with name 'Bean1' defined in com.package.sample.config.ConfigurationA: Unsatisfied dependency expressed through method 'createBean1' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.package.sample.config.ConfigurationB available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
When i change #ContextConfiguration... to #SpringBootTest it works but the whole Context gets loaded.
Is there any solution to load not the whole context?
Just make ConfigurationA extend ConfigurationB
I have written a Restful application in springboot.
So for that i have a controller class with #RestController and a service class with #Service annotation.
In my Rest controller, i #Autowire the service like below.
#Controller
public class TestController {
#Autowire
TestService testService;
}
#Service
public class TestService {
}
Note : My main springboot class at top level of all the packages.
The strange thing is that, when i start my Spring boot application, it start perfectly and service bean initalized correctly. I also tested the same. But when i do maven build it gets failed with error :
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I'm trying to implement Swagger using the JHipster implementation as reference into my Kotlin application.
However, when I run my tests, I get the following error for most of my tests (these tests work fine if I remove this swagger code):
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pageableParameterBuilderPlugin' defined in class path resource [com/application/config/apidoc/SwaggerPluginsAutoConfiguration$SpringPagePluginConfiguration.class]: Unsatisfied dependency expressed through method 'pageableParameterBuilderPlugin' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springfox.documentation.schema.TypeNameExtractor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
My SwaggerPluginsAutoConfiguration class:
#Configuration
#ConditionalOnWebApplication
#ConditionalOnBean(Docket::class)
#AutoConfigureAfter(SwaggerAutoConfiguration::class)
class SwaggerPluginsAutoConfiguration {
#Configuration
#ConditionalOnClass(Pageable::class)
class SpringPagePluginConfiguration {
#Bean
#ConditionalOnMissingBean
fun pageableParameterBuilderPlugin(typeNameExtractor: TypeNameExtractor,
typeResolver: TypeResolver): PageableParameterBuilderPlugin {
return PageableParameterBuilderPlugin(typeNameExtractor, typeResolver)
}
}
}
The SwaggerAutoConfiguration class:
#Configuration
#ConditionalOnWebApplication
#ConditionalOnClass(ApiInfo::class, BeanValidatorPluginsConfiguration::class, Servlet::class, DispatcherServlet::class)
#Profile(value = [SPRING_PROFILE_SWAGGER])
#EnableSwagger2
#Import(BeanValidatorPluginsConfiguration::class)
class SwaggerAutoConfiguration(applicationProperties: ApplicationProperties) {
...
}
If I include the swagger profile in my tests application config file, the tests pass. However, if the profile isn't present, the tests fail with the error above. I'm not sure why Spring is trying to configure swagger if the profile isn't set.
How can I set this up, so that the configuration doesn't try to load if the profile isn't specified?
What does it take, or is it even possible for Spring to scan and inject non-spring annotated classes? For example.
resource.jar
com.project.resource.ResourceInterface
com.project.resource.StandardResource <-- concrete implementation
#Singleton <--- Standard CDI annotation
public class StandardResource implements ResourceInterface{
#Override
public void something(){}
}
Now let's say I have a spring boot application which depends on resource.jar.
com.project.resource.SpringApp
#SpringBootApplication(scanBasePackages = {"com.project"})
#EnableAutoConfiguration
public class SpringApp{
... initializer
#Inject
private ResourceInterface resourceService; <--- this is not found
}
Is this supposed to work out of the box? Is this even possible? I'm using spring boot 2.0.0.RELEASE. I'm getting the following error:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MainController': Unsatisfied dependency expressed through field 'resourceService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.project.resource.ResourceInterface' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#javax.inject.Inject()}
Thanks
For Spring framework #Singleton has no meaning, as such even if class is picked up by component scanning it's going to be ignored. In order for Spring to recognize your class you can:
Create a configuration class in com.project.resource with #Bean of
ResourceInterface and instantiate it as StandardResource.
Since you are using Spring Boot you can create Auto-configuration (which will be similar to the first option) in resource.jar. You can follow examples
from creating autoconfiguration. With this approach no changes needed in com.project.resource
After that your spring boot app will run normally
I'm trying to run a web app and I'm having some issues. Basically I have a controller and a process and they both share a queue.
The controller manages the files that are uploaded to the server and it puts them in the queue. In the other side, the process takes the files in the queue and uses them for other things.
I've defined the queue as a LinkedBlockingQueue and the annotation #Resource on both of them, but when I run the app, the following exception appears:
Error creating bean with name 'csvQueueConsumerBean': Injection of resource
dependencies failed; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [java.util.concurrent.LinkedBlockingQueue] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for this
dependency.
The code of both clases is the following:
#RestController
#RequestMapping("/upload")
public class FileUploadControllerW {
#Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;
...
}
#Component
public class CsvQueueConsumerBean{
#Resource
protected LinkedBlockingQueue<QueueObject> csvQueue;
...
}
Just for the record, both classes are not on the same package.
The reason for this is because Spring context cannot wire the Bean called
csvQueueConsumerBean
You will need to initialize its LinkedBlockingQueue dependency in the Spring config file like this:
#Bean
public LinkedBlockingQueue<QueueObject> linkedBlockingQueue(){
LinkedBlockingQueue<QueueObject> blockingQueue = new LinkedBlockingQueue<QueueObject>();
// do what you need here...
return blockingQueue;
}