Spring-Data-Envers repository fails to inject - spring-data

In my spring context I've the following:
<jpa:repositories base-package="x.y.service.springDataJpa"/>
<jpa:repositories base-package="x.y.service.springDataEnvers"
factory-class="org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean" />
This should pick up my normal spring-data-jpa repositories and my spring-data-envers repository.
But when executing this junit testclass:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {ApplicationTestContext.class, OrmContext.class, DatasourceContext.class})
#ActiveProfiles(profiles = "LOCAL")
#Transactional
public class StoreAndRetrieveRevisionTest {
#Inject
private ServiceRepository serviceRepository;
#Inject
private ServiceRevisionRepository serviceRevisionRepository;
(The ApplicationTestContext.class loads the xml config).
It fails to inject the revisionrepository bean:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'x.y.server.springDataJpa.hibernateenvers.StoreAndRetrieveRevisionTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private x.y.service.springDataEnvers.ServiceRevisionRepository x.y.server.springDataJpa.hibernateenvers.StoreAndRetrieveRevisionTest.serviceRevisionRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [x.y.service.springDataEnvers.ServiceRevisionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.inject.Inject()}
What am I missing here?
The spring-data-envers documentation is rather sparse..

Please check if you have your entity class properly annotated with #Entity and #Table annotation. If not your repository bean will not get properly initialized and you will not be able to use it.

Related

Integration Test fails in jenkins with embedded mongodb

I ran my integration test locally, this works fine. But when try running jenkins, it fails.
This integration test contain embedded mongodb. The integration test is below:
#RunWith(SpringRunner.class)
#SpringBootTest
#EnableAutoConfiguration
public class MongoDbSpringUnitTest {
#Autowired
PriceRepository priceRepository;
#Test
public void testSavePriceMongoRepo() {
priceRepository.deleteAll();
Price price = priceRepository.save(new Price("1234productId", 24.5f));
// System.out.println("id : "+price);
assertEquals(0,24.5f, price.getCost());
assertNotNull("id is autogenerate", price.getId());
}
getting below error:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'priceController': Unsatisfied dependency expressed through field 'priceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'priceRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDbFactorySupport]: Factory method 'mongoDbFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource
[org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.MongodExecutable]: Factory method 'embeddedMongoServer' threw exception; nested exception is java.lang.IllegalArgumentException: this version does not support 32Bit: Version{3.5.5}:Windows:B32
above error show that embebbed db server cannot installed on 32 bit machine. I am not sure jenkins only support 32-bits. I am using jenkins 2.164.2.
The full code is present in below github:
https://github.com/ranjit5311/MicroServiceCode
Please let me know to resolve this issue.
Thanks,
Ranjit Kumar
After installing new version of Jenkins 2.164.3, resolved this issue.

Grails, Spring Security and Mongo

I try to configure Grails 3.1.4 rest-api aplication. I'd like to use Mongo and Spring Security for authentication, but when I erase from application.yml h2 datasource and hibernate and try to run application I get a lot of exceptions like:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceInterceptor':
Cannot create inner bean '(inner bean)#1b3bb287' of type [org.grails.orm.hibernate4.support.AggregatePersistenceContextInterceptor]
while setting constructor argument with key [0]; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1b3bb287':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method:
public void org.grails.orm.hibernate4.support.AggregatePersistenceContextInterceptor.setHibernateDatastores(org.grails.orm.hibernate.AbstractHibernateDatastore[]); nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.grails.orm.hibernate.AbstractHibernateDatastore]
found for dependency [array of org.grails.orm.hibernate.AbstractHibernateDatastore]:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
I can confirm this behavior with Grails 3.1.7 if you just comment out the default H2 and cache part in application.yml. However, it disappears for me, if I comment out the following:
// compile "org.grails.plugins:hibernate4"
For reference, I'm using the following plugin versions for mongoDB and spring-security-core:
compile 'org.grails.plugins:mongodb:5.0.6'
compile "org.mongodb:mongodb-driver:3.0.2"
runtime 'org.springframework.data:spring-data-mongodb:1.8.1.RELEASE'
compile 'org.grails.plugins:spring-security-core:3.1.1'
Cheers!

Should it be possible to use #EnableFeignClients on a BootstrapConfiguration?

I'd like to use a feign client from a PropertySourceLocator which has to be registered during bootstrap. I have the following in my spring.factories:
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
test.PropertiesConfiguration
with
#Configuration
#EnableFeignClients
public class PropertiesConfiguration {
#Bean
public PropertySourceLocator propertySourceLocator() {
return new MyPropertySourceLocator();
}
}
The feign client is autowired into MyPropertySourceLocator. However, while the same client works in regular beans, it doesn't work here. All requests fail with this exception:
feign.codec.DecodeException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] 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)}
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:173)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:141)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
at com.sun.proxy.$Proxy23.get(Unknown Source)
at test.MyPropertySourceLocator$1.getProperty(MyPropertySourceLocator.java:32)
at org.springframework.core.env.CompositePropertySource.getProperty(CompositePropertySource.java:59)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:60)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:511)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:135)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:70)
at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:207)
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:962)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:342)
// SNIP
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.autoconfigure.web.HttpMessageConverters] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectFactory.getObject(DefaultListableBeanFactory.java:1419)
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyProvider.get(DefaultListableBeanFactory.java:1436)
at org.springframework.cloud.netflix.feign.support.SpringDecoder.decode(SpringDecoder.java:57)
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
... 93 common frames omitted
This happens with #FeignClient(url = "localhost:8080"). Using ribbon with #FeignClient("config") fails completely:
Caused by: java.lang.NullPointerException
at feign.ribbon.LoadBalancingTarget.apply(LoadBalancingTarget.java:91)
at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:164)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:108)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:96)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:92)
at com.sun.proxy.$Proxy26.get(Unknown Source)
at test.MyPropertySourceLocator$1.getProperty(MyPropertySourceLocator.java:32)
I want to access configuration using REST (and thus feign). The possible workaround would of course be to use feign directly.
The bootstrap context is supposed to be lightweight (and fast to create), so it's not a full #EnableAutoconfiguration. To use #FeignClient you would need to import the FeignAutoConfiguration (and anything that depends on, which isn't much IIRC).

Spring data jpa: No property flush found for type void

I am using Spring Data JPA to develop a Spring MVC app. I built a JPA repository.
public interface AccessReportRepository extends JpaRepository<AccessReport, Long> {
}
I also use Spring Data Mongo along with JPA in my project.
When I run the project, I get this error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lastDateController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.ReadingService com.innolabmm.software.mongotest.springrest.LastDateController.readingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.AccessReportRepository com.innolabmm.software.mongotest.springrest.ReadingService.reportRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accessReportRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property flush found for type void
Does anyone have an idea what is going on? I am ready to provide more information if this will help solve the problem. Thanks in advance.
Are you using Spring Boot?
I had the same exception thrown when trying to use JPA and Mongo together, in a Spring Boot application. I found that the Repositories were always being interpreted by both JPA and Mongo, leading to problems as my repositories specifically extend JpaRepository.
I only wanted JPA repositories generated, so added the following to the Application entry point.
#EnableAutoConfiguration(exclude={MongoRepositoriesAutoConfiguration.class})
If you are using MongoDB with JPA (my JPA db is mysql) do this:
create separete packages for domain objects and for repositories (DAOs): i.e. demo.mysql.domain, demo.mysql.dao, demo.mongo.domain, demo.mongo.dao
in your configuration class use these annotations:
#EntityScan(basePackages={"demo.mysql.domain"})
#EnableJpaRepositories(basePackages={"demo.mysql.dao"})
#EnableMongoRepositories(basePackages={"demo.mongo.dao"})
annotate your JPA entities with #Entity and put them all in demo.mysql.domain package
annotate your MongoDB entities with #Document and put them all in demo.mongo.domain package
keep all of MongoDB reporitories in demo.mongo.dao package
keep all JPA repositories in demo.mysql.dao package
all mongo repositories should extend MongoRepository (i.e. public interface TransactionRepository extends MongoRepository)
all JPA repositories should extend JpaRepository (i.e. public interface CityRepository extends JpaRepository)
This is by far the easiest and the cleanest way that I have found (after digging through docs, btw.) to have MongoDB and Mysql working in one Spring Boot app.
That is what I have tried and what works. You could probably use MongoTemplate or extend CrudRepository instead of JpaRepository, or whatever else, try it, it might work.

error on integrating spring data jpa with spring batch admin

o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productfeedSvc':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.dealwallet.productFeed.repository.ProductFeedRepository
com.dealwallet.productFeed.svc.impl.ProductSvcImpl.productFeedRepository;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productFeedRepository': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: **org.springframework.transaction.interceptor.TransactionInterceptor.setTransactionManagerBeanName(Ljava/lang/String;)**V
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
I am using spring data jpa with spring batch admin,
i configured application-context.xml and persistance.xml,
when am running this in tomcat i got this error.
It says there is problem in auto-wiring.
So check these items:
- Make sure your class (the one which is using the other) has setter for that property.
- Make sure your class's property (the one which is using the other) has the exact name of the other class bean id.
- Make sure you have enabled annotations.