Spring boot DI not working for scala typeclass - spring

Spring boot DI not working for scala typeclass. Throwing NoSuchBeanException. I am using spring 4.3.13 and spring boot version 1.5.9
This is pseudo code snippet for reference:
trait Repo[T]
#Repository
class RedisRepo #Autowired()(redis: RedisClient) extends Repo[Event]
class RedisDao #Autowired()(repo: Repo[Event])
I have also tried using #Qualifier for the constructor parameter in RedisDao class. However, its also not working.
Thanks.

Related

Spring boot Jpa is not working with Spring batch and spring integration

I am working with spring batch. I needed to add some jpa repositories. So previously i was using JDBCTemplate which was working fine.
But when I started working with JPA, the spring boot application could not find the repos. Which were there.
#Autowired
ClassLevelConfigRepo clcr;
I checked these things as the best practices.
Added #EnableJpaRepositories in springBoot application class.
Added #Repostiories to the repository interfaces.
extended the interfaces with JpaRepository<Account, String>
Added #Entity to the entity classes and defined the #Table and # Column annotations properly.
But I am still getting below error.
Field clcr in com.cloudtask.batchconfig.util.LhmUtility required a bean of type 'com.cloudtask.batchconfig.repo.ClassLevelConfigRepo' that could not be found.
I tried checking all the dependencies in pom.xml it was as per recommended. And I have all the tables defined properly in data base.
I was expecting the application to return the Autowired clcr object propely.
Edit 1 : spring boot application annotations
#SpringBootApplication
#ComponentScan({"com.cloudtask"})
#EnableAsync
#IntegrationComponentScan({"com.cloudtask"})
#EnableIntegrationManagement(defaultLoggingEnabled = "true")
#EnableJpaRepositories
#EntityScan
public class imclassApplication ```
When you work with Spring Data Jpa with those basic points you should also keep track of below points.
you have added spring-boot-starter-data-jpa in your pom.xml
you have added the entity and repo package below one level of the application package.
If you the package is at same level you should specify the exact package details in the annotation. in your case it should be like :
#EnableJpaRepositories("com.cloudtask.batchconfig.repo")
#EntityScan(basePackages = {"com.cloudtask.batchconfig.entity"})
Happy programming!

Spring Boot 2.1 - #WebMvcTest without Spring Security Auto-Configuration

Before migrating to Spring Boot 2.1, we had a couple of controller tests in our services utilizing #WebMvcTest in combination with #AutoConfigureMockMvc:
#WebMvcTest(SomeController.class)
#AutoConfigureMockMvc(secure = false)
public class SomeControllerTests { ... }
This had the effect that the Spring Security configuration was disabled and you could run MVC tests without mocking OAuth/JWT.
In Spring Boot 2.1, the secured attribute is deprecated and the release notes mention that
[...] #WebMvcTest looks for a WebSecurityConfigurer bean [...].
In order to avoid the deprecated secured attribute and loading of our WebSecurityConfigurer we rewrote our tests to:
#WebMvcTest(
value = SomeController.class,
excludeFilters = #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class),
excludeAutoConfiguration = MockMvcSecurityAutoConfiguration.class)
public class SomeControllerTests { ... }
The question is: is there a more compact way in Spring Boot 2.1 to define such tests?
Yes, rather than working around the fact the flag is deprecated, you should embrace the fact that this is going in that direction going forward.
As of Spring Boot 2.1, if you have Spring Security, your tests will be secured using your custom configuration. What is the actual problem with that?
If you don't want to authenticate for certain tests, just use Spring Security's test infrastructure and add #WithMockUser.
Encountered the same scenario and what helped was using the below annotations instead of #WebMvcTest. In this case, #WithMockUser did not help.
#WebAppConfiguration
#Import({MockMvcAutoConfiguration.class})
#EnableConfigurationProperties({ResourceProperties.class, WebMvcProperties.class})
Classes that existed in controllers / value of #WebMvcTest goes into value of #Import annotation.
Source: https://github.com/spring-projects/spring-boot/issues/14227#issuecomment-688824627

Should we use #Component on #Entity class in Spring boot

Well i am a newbie in spring boot. I was working on a project where I needed to #Autowired my Entity in a controller class. But I ended up with error:
Field repository in abc required a bean of type 'xyz' that could not be found.
But it solved after adding #Component in Entity class.
So my questions are:
Why Spring boot was not scanning my Entity class as it was under #SpringBootApplication declaration?
When and where we should use #Component annotation in our application?
Use #Component to flag your Pojo as Spring Bean, so that you inject it into other beans with #Autowired
Use #Entity to flag your Pojo as JPA or Spring Data managed bean to read or write it to a database

Why Kotlin class does not need to open in Spring Boot anymore

In a latest video about Kotlin and Spring Boot: Spring Tips: Bootiful Kotlin Redux. The Application class of Spring Boot looks like:
class SpringBootKotlinApplication
fun main(args: Array<String>) {
}
I remember a Kotlin has to be annotated as open in Spring Boot.
open class SpringBootKotlinApplication
See this video.
So why it is not necessary to be annotated open now? Spring Boot does need to extend the Application class now?
When you have a look at one of the speaker’s sample projects like this, you will notice a compiler plugin, in this case kotlin-maven-allopen. It’s described in the official docs:
For instance, when you use Spring, you don't need all the classes to be open, but only classes annotated with specific annotations like #Configuration or #Service. All-open allows to specify such annotations.
As SpringBootAnnotation, as a meta annotation, is fulfilling that condition, there’s no need to make the class open in the source code since the compiler does it behind the scenes.

Spring boot jersey - prevent startup instantiation of controller

I am using spring boot with web and jersey (spring-boot-jersey-starter). I have a Jersey endpoint that needs to inject a request scope bean. However, at startup of the application I am getting a no bean found error.
#Component
#Path("blah")
#RequestScoped
public class JerseyController{
#Inject
private MyEntity entity;
}
#Component
public class JerseyConfiguration extends ResourceConfig{
public JeyseyConfiguration(){
register(JeyseyController.class);
registere(MyEntityProvider.class);
}
}
Is there a way, in a spring-boot web app, to prevent Spring from attempting to instantiate and inject my JerseyController until an HTTP request is received so that the injected dependency can be provided by my Jersey provider?
#Component is not required on Jersey resources. Having it will cause Spring to instantiate it (with default Singleton scope). I don't think Spring doesn't respect the #RequestScoped. This is a Jersey annotation. If you want to use the #Component, I think the Spring #Scope("request") might do the trick though.
You can also remove the #RequestScoped. This is the default scope for Jersey resources.
The only time I have ever found a need to use #Component on Jersey resources, is if I need to use the Spring #Value (maybe AOP also, but I don't do much AOP). Other than that, the Jersey-Spring integration already supports the most common used feature of Spring which is DI. And if you really want to make the Jersey resource a singleton, Jersey supports the #Singleton annotation.

Resources