My project does use Redis but my unit test mocks it. However it doesn't mock it enough because I get this stack trace snippet:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisReferenceResolver': Cannot resolve reference to bean 'redisTemplate' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
It is correct. I have carefully ensured there is no redisTemplate bean available because I don't want to run redis in my unit test. But I don't know why it is trying to create the redisReferenceResolver bean.
I've turned off autoconfiguration with this:
spring:
profiles: RedisMock
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
(my test has #ActiveProfiles({"WebMvcTest","RedisMock"}) in the top)
I do have one class annotated with #RedisHash but I need that for my test.
Anyone know what might be triggering the autoconfigure?
Thanks
redisReferenceResolver is defined by Spring Data’s support for Redis-based repositories. Setting spring.data.redis.repositories.enabled to false and ensuring that you haven’t used #EnableRedisRepositories anywhere in your app will disable it.
Related
I have Spring Boot app that creates a RESTful API. It wants to automatically connect to port 27017, I believe because it contains a transitive dependency to MongoDB.
But I don't want it to auto-connect to MongoDB because that server may not yet have started, thus throwing an error.
So I exclude the Mongo AutoConfiguration classes from the app like so:
#SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
But then I get this error:
Parameter 0 of constructor in com.whatev.MyLogImpl required a bean named 'mongoTemplate' that could not be found.
Why does it need an mongoTemplate when I'm excluding Mongo AutoConfiguration?
But nonetheless, I then added a new #Configuration class containing MongoTemplate, but I get:
Parameter 0 of constructor in com.whatevz.MongoFileService required a bean of type 'org.springframework.data.mongodb.gridfs.GridFsTemplate' that could not be found.
- Bean method 'gridFsTemplate' not loaded because auto-configuration 'MongoDataAutoConfiguration' was excluded
True, I excluded the MongoDataAutoConfiguration class. But I don't want to also exclude gridFsTemplate if that's gonna cause a problem. Sheesh.
All I want to do is not see an error when I start my app if MongoDB is not yet running.
OK - problem ==> 2 Datasources (or more) will cause an error when using Springboot Session.
I have a Springboot project, with different data sources for different purposes. So within said project I would like to have Spring replace the HttpSession, with the Spring-Session module's HttpSession.
The creation of the session tables is simple if you follow the guides.
But the guides and API docs say you need JUST one data source. And so, since I have 2 or more I get the following error:
2020-07-21 21:38:59.134 ERROR 171262 --- [ restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter :
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException.
Message: Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource
[org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]:
Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'javax.sql.DataSource' available:
expected single matching bean but found 2: authModelDs,secureSessionDS
So since it cannot decide which one to use, I have to manually configure it, except that the entire process leads you to a class called
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration, which is quite literally non-configurable under the given need to set your own datasource.
I have looked a bit around, and there is no way to configure the Session Filter, once it is created to replace the datasource there too.
So I am boggled: how can I set the correct datasource for the configured Spring-Session?
Any help would be appreciated.
AHA! Soooooo
There is an annotation that you can place on the Bean creation of your datasource, to specify that such a bean is the SpringBoot session datasource.
The annotation is - #SpringSessionDataSource
It is not something that is really straight forward in the documentation at all!
Props to the following Stack Overflow answer:
Spring Boot and Spring Session: How to control the DataSource
I have stumbled upon one issue. I am using Spring for DI in my BDD tests. My components are Glue Scoped by marking components with #Scope("cucumber-glue"). But the issue now I am facing is this components are also needed to be used in my JUnit based tests. While trying to Autowire my components spring throws:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '<bean name>': Injection of autowired dependencies failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope name 'cucumber-glue'
As per the suggestion in one of the older reported git hub issues I tried registering GlueCodeScope to glueCustomConfigurer Bean but that class is package restricted so I am unable to access it. Is there another way via which I can continue to use my components in BDD as well as JUnit tests. Note I have to use cucumber-glue scope in order to ensure my tests can run in parallel and that each scenario receives a fresh bean.
I forked a simple spring project from github.
When i Run it, It got the following error
Description:
Field userRepository in com.example.demo.service.UserService required a bean of type 'com.example.demo.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.repository.UserRepository' in your configuration.
Make the spring boot app aware about the repository location
#SpringBootApplication(scanBasePackages={
"com.example.repository", "com.example.application"})
If it doesn't work then follow below thread.
'Field required a bean of type that could not be found.' error spring restful API using mongodb
I am attempting to convert an existing spring weblogic application to a spring boot embedded tomcat application.
There are lots of moving parts so it's hard to show any code, I'm hoping there is some general answer that might clue me in to the issue.
Under weblogic, using the spring-framework 4.3.6.RELEASE libraries, the application deploys fine. It has no problems creating the different service, repository and component beans.
However, when I migrate it to Spring Boot 1.5.1.RELEASE, I get the following error:
2017-06-21 17:08:16,402 [ERROR] SpringApplication reportFailure (815) - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'alertEventServiceImpl': Unsatisfied dependency expressed through field 'alertEventDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'alertEventDaoImpl' defined in URL [jar:file:/Users/username/Development/source/carma-war/target/carma-war-2.0.0-SNAPSHOT.war!/WEB-INF/lib/protocol-manager-1.8.0-SNAPSHOT.jar!/org/ihc/hwcir/protocol/dao/AlertEventDaoImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl
Many of our service classes are final as they shouldn't be extended. Since there are so many that are final, I wanted to minimize the amount of code in our different libraries that we modify to make this work.
I thought because the bean creation process works under weblogic, it should work under spring boot.
Things I have tried to force not using the cglib proxies:
All implementations implement interfaces already
In beans created via xml, added <aop:scoped-proxy proxy-target-class="false"/>
In beans created through annotations, added (example for service bean)
#Service
#Scope(proxyMode = ScopedProxyMode.INTERFACE)
However, in the end, I'm perplexed as to why spring can create beans (the classes marked as final) under the weblogic container but unable to do so under the embedded tomcat spring-boot container.
Spring Boot by default uses class based proxies, which will not work with final classes/methods.
To disable this add spring.aop.proxy-target-class=false to the application.properties to enable JDK Dynamic Proxies instead of class based proxies. (And revert your modifications).
NOTE: To have everything take into account the spring.aop.proxy-target-class you might need to upgrade to Spring Boot 1.5.3 as some final patches where made to include this property in parts that were missed in previous versions.
See the following issues for more information 8434, 8869 and 8887.
I was unable to make this work using M. Deinums' answer using spring.aop.proxy-target-class=false.
What worked for me was to add in the application.properties file
spring.dao.exceptiontranslation.enabled=false
Please note that this option disables proxy creation for repositories.
And in my spring boot application configurer the annotation to handle transactions without using a proxy class.
#EnableTransactionManagement(proxyTargetClass = false)
This is using Spring Boot version 1.5.1.RELEASE.