spring boot scanning and injecting external non-spring beans - java

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

Related

Bean is no longer being autowired after Spring 5.3 Upgrade

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.

How to add a protected configuration with access to properties to a Spring Boot Test context?

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

How to split Spring Conditional configuration?

My application uses its own Spring configuration Condition to provide beans according to setup, but because of volumes, I expect to split long #Configuration instead of adding #Conditional to tens of beans.
At the moment, my main security configuration looks like
#Configuration #EnableOAuth2Client #EnableWebSecurity
#Import(OptionalAuthenticationConfiguration.class)
public class WebshopSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired(required = false)
private OptionalAuthenticationConfiguration auth;
And here is conditional configuration
#Configuration
#Conditional(MyCondition.class)
public class OptionalAuthenticationConfiguration {
[...]
Because of #Conditional on optional configuration, #Import annotation fails with
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webshopSecurityConfig': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private OptionalAuthenticationConfiguration
WebshopSecurityConfig.auth; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [OptionalAuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Even if bean attribute WebshopSecurityConfig.auth has required = false.
Is there a way to achieve such conditional configuration splitting in Spring?
Make OptionalAuthenticationConfiguration an interface with two implementations.
An actual implementation with #Conditional(MyCondition.class) and an empty implementation with #ConditionalOnMissingBean.
This way when MyCondition doesn't allow the configuration to be created, it will get replaced by a placeholder empty implementation.
What I described in this question is expected to work... as far as "MyCondition" properly returns true, and my trouble was there.
Project context was far more complex than shown here: "OptionalAuthenticationConfiguration" is in fact a Spring Security SAML setup which expects many beans from "WebSecurityConfigurerAdapter".
As a result, my code ended with circular "#Import" between these two configurations. I had to move 5 beans from "OptionalAuthenticationConfiguration" to "WebshopSecurityConfig" and make each of them "#Conditional".

Java Spring JPA Repository

I'm a Spring noob and I'm struggling with it.
Basically before starting develop my Server with Spring in conjunction with JPA I tried to start a simple example just to get used to this framework.
I've already get succeded in make Spring working with some frameworks as Log4J, Swagger and others. Now I'm trying to work with JPA and there are some points i can find out the solution.
I saw some blogs on how to develop with it and from all thousands options i choose to create my Repository Interfece and extend Repository<T, ID>. You can see my code bellow:
package com.example.model;
#Entity
public class Person {
#Id
public Integer id;
public String name;
public Person(){}
}
package com.example.repository;
public interface PersonRepository extends Repository<Person, Integer> {
Collection<Person> findAll();
}
package com.example.controller;
#RestController
public class PersonController {
#Autowired
private PersonRepository repo;
#RequestMapping(value = "/persons", method = RequestMethod.GET)
public Collection<Person> getAll() {
return repo.findAll();
}
}
package com.example;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
And I also have the application.properties file:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
When I put the server running I get the following exception:
: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
: Closing JPA EntityManagerFactory for persistence unit 'default'
: Stopping service Tomcat
: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I created a Github Repository to share the code here.
Any clue about what am I doing wrong?
First thing here is why you need to implements the base interface Repository as doing so, you will not have usual CRUD operations. For such operations is better to implements CrudRepository. Since you implement CrudRepository no need to define a findAll() Method and many well known others you can find in doc mentioned.
Furthermore, when using #SpringBootApplication Spring boot use default values. If you see the #SpringBootApplication definition you will see that :
Many Spring Boot developers always have their main class annotated with #Configuration, #EnableAutoConfiguration and #ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient #SpringBootApplication alternative.
The #SpringBootApplication annotation is equivalent to using #Configuration, #EnableAutoConfiguration and #ComponentScan with their default attributes: [...]
That means when using default values for ComponentScan your packages sturctures shloud be as following :
com.example.model -> your entities
com.example.repositoriy -> your repositories
com.example.controller -> controllers
com.example -> MainApplication class
Here is an example for default project structure
The main Application Class should be in higher level package then the others. Unless you have to specify packages location with #ComponentScan.
As you are beginner with the framework. I suggest you to always see classes definitions in official documentation.
UPDATE :
Here is an example from one of my spring boot projects
Also see this spring guide for JPA
Just annotate your interface with #Repository. And if that doesnt work try adding #EnableJPARepositories to the main class.
Try adding the #Repository annotation to your PersonRepository, that could be the reason it doesn't find it.
You need to annotate your PersonRepository interface with #Respository, otherwise the spring context won't recognize it or create an implementation for it.

Spring #Resource Expected at least 1 bean which qualifies as autowire candidate for this dependency

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;
}

Categories