org.springframework.aot.context.bootstrap.generator.BeanDefinitionGenerationException: Failed to handle bean with name 'xxx' and type 'xxx' - spring-native

I want to try SpringNative. I have a custom Bean, but when I run "spring-aot:generate" or "spring-boot:build-image", I get the following exception. I don't know how to configure and add Hint Key exception information is as follows
org.springframework.aot.context.bootstrap.generator.BeanDefinitionGenerationException: Failed to handle bean with name 'devInfoHeaderFilter' and type 'tablo.jhin.application.filters.DevInfoHeaderFilter' at org.springframework.aot.context.bootstrap.generator.ApplicationContextAotProcessor.writeBeanDefinitions
...
Caused by: java.lang.IllegalStateException: Multiple privileged packages: [tablo.jhin.config, tablo.jhin.application.filters]at org.springframework.aot.context.bootstrap.generator.infrastructure.ProtectedAccessAnalysis.getPrivilegedPackageName
...
[ERROR]
org.apache.maven.plugin.MojoExecutionException: Could not exec java
at org.springframework.aot.maven.AbstractBootstrapMojo.forkJvm (AbstractBootstrapMojo.java:197)
...
Caused by: org.apache.maven.plugin.MojoExecutionException: Bootstrap code generator finished with exit code: 1
at org.springframework.aot.maven.AbstractBootstrapMojo.forkJvm (AbstractBootstrapMojo.java:190)
at org.springframework.aot.maven.GenerateMojo.execute (GenerateMojo.java:132)
....
In a internal class under the 'tablo.jhin.config', I configured such a Bean: ServiceTraceFilterConfiguration as an internal class
#ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class ServletTraceFilterConfiguration implements NativeConfiguration {
#Bean
public DevInfoHeaderFilter devInfoHeaderFilter() {
return new DevInfoHeaderFilter();
}
}

Related

conditional class not detected on mvn clean package

I have the following conditional class that should be unseen unless some other class annotates itself with a custom annotation:
#ConditionalOnBean(MyConf.class)
#Service
#RequiredArgsConstructor
public class CondClass{
public void method() throws MessagingException
Then I have a spring boot test:
#SpringBootTest
public void myTest{
It works fine if I manually execute it, but if I do:
mvn clean package
I get:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalStateException: Failed to introspect Class
[it.bmed.medmad.arch.common.websocket.service.impl.CondClass] from ClassLoader
[sun.misc.Launcher$AppClassLoader#18b4aac2]
Caused by: java.lang.NoClassDefFoundError: org/springframework/messaging/MessagingException
Caused by: java.lang.ClassNotFoundException:
org.springframework.messaging.MessagingException
Indeed that MessagingException is on a dependency that is not included, but I would expect that the whole class is not inspected because of the annotation.
Add #ConditionalOnClass(MyConf.class), too:
#ConditionalOnClass(MyConf.class)
#ConditionalOnBean(MyConf.class)
#Service
#RequiredArgsConstructor
public class CondClass {
// ...
}
sample: official usage

java.lang.IllegalStateException: Failed to load ApplicationContext-How to test main class of Spring-boot application

I have a spring-boot application where my #SpringBootApplication starter class looks like a standard one and getting Caused by:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class [com.abct.ECRRestServer];
nested exception is java.io.FileNotFoundException: Could not open
ServletContext resource [/application.properties]
#RunWith(SpringRunner.class)
#SpringBootTest
public class ECRRestServerTest {
#Test
public void contextTest() {
}
}
Encountering below issue.
2019-04-27 18:51:25.732 ERROR 15096 --- [ main]
o.s.test.context.TestContextManager : Caught exception while
allowing TestExecutionListener
[org.springframework.test.context.web.ServletTestExecutionListener#78e117e3]
to prepare test instance [com.abct.ECRRestServerTest#16ecee1]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
Failed
to parse configuration class [com.abct.ECRRestServer]; nested
exception is java.io.FileNotFoundException: Could not open
ServletContext resource [/applicati
on.properties]

Can't Get Elasticsearch Plugin to Work with Grails 3

I've tried to hook up Elasticsearch plugin ('org.grails.plugins:elasticsearch:1.4.1') with a Grails 3.1.9 project without any luck.
I've also uploaded a simple application in github to demonstrate the issue (https://github.com/jackiebai/testEs)
The above sample testing app only has 1 domain class called Person:
class Person {
String name
static searchable = true
static constraints = {
}
}
The corresponding service class PersonService is used to save and index the Person data:
import grails.transaction.Transactional
#Transactional
class PersonService {
def addPerson(Person person){
person.save()
person.index()
}
def addPerson(String name) {
Person p = new Person(name:name)
addPerson(p)
}
}
I've also added a couple of lines of code in the BootStrap.groovy so that some data will get inserted into the default hibernate db when the application starts:
class BootStrap {
def personService
def init = { servletContext ->
personService.addPerson("Person One")
personService.addPerson("John Smith")
}
def destroy = {
}
}
The application.yml variable settings for the development environment look like below:
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
elasticSearch:
client:
mode: node
transport.sniff: true
bulkIndexOnStartup: true
datastoreImpl: hibernateDatastore
Additional dependencies and bootRun code block were included in the build.gradle by following the plugin user guide (http://noamt.github.io/elasticsearch-grails-plugin/docs/index.html)
dependencies {
...
compile 'org.grails.plugins:elasticsearch:1.4.1'
compile 'org.elasticsearch:elasticsearch:5.4.1'
...
}
bootRun{
jvmArgs = ['-Des.path.home=/Users/myname/elasticsearch-5.4.1']
}
Unfortunately the application failed to run under my osx 10.11.6,java 1.8.0_91 environment with below errors:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticSearchClient': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: org/elasticsearch/mapper/attachments/MapperAttachmentsPlugin
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1590)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
... 34 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/mapper/attachments/MapperAttachmentsPlugin
at grails.plugins.elasticsearch.ClientNodeFactoryBean.getObject(ClientNodeFactoryBean.groovy:207)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 39 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.mapper.attachments.MapperAttachmentsPlugin
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 41 common frames omitted
According to documentation you should use v1.2.0
https://github.com/noamt/elasticsearch-grails-plugin
I believe version 1.4.1 is broken. You can try 2.4.0.RC1 with Grails 3.3+ but you need to add dependency to mapper-attachments and specify elasticsearch.version for Spring Boot:
ext['elasticsearch.version'] = '5.4.1'
dependencies {
compile 'org.grails.plugins:elasticsearch:2.4.0.RC1'
runtime 'org.elasticsearch.plugin:mapper-attachments:2.4.6'
}
See issue#198

Usage of Spring #ConfigurationProperties gives nullpointer when running tests, JHipster app

I have implemented a simple file upload routine using Spring Boot ConfigurationProperties annotation so I can load the directory from my YAML configuration file:
Service:
#Service
public class FileSystemStorageService implements StorageService {
private final Logger log = LoggerFactory.getLogger(FileSystemStorageService.class);
private final Path pictureLocation;
#Autowired
public FileSystemStorageService(StorageProperties storageProperties) {
pictureLocation = Paths.get(storageProperties.getUpload());
}
And the StorageProperties:
#Component
#ConfigurationProperties("nutrilife.meals")
public class StorageProperties {
private String upload;
public String getUpload() {
return upload;
}
public void setUpload(String upload) {
this.upload= upload;
}
}
In the yaml file I have:
nutrilife:
meals:
upload: /$full_path_to_my_upload_dir
This works perfectly in normal Spring Boot runtime but the problem starts when I try to run my integration tests, it throws the error:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystemStorageService' defined in file [/home/mmaia/git/nutrilife/build/classes/main/com/getnutrilife/service/upload/FileSystemStorageService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.getnutrilife.service.upload.FileSystemStorageService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
So basically during tests the YAML file configuration looks like it's not being properly picked up. I am new to Spring. How can I fix it?
try adding the #value annotation :
#Value("upload")
private String upload;
The above answer is valid for application.properties config.
it also works with yaml as well.
You may find your correct correct yaml config here :
24.6 Using YAML instead of Properties

Not an managed type for Spring Data Mongodb

I have the following mongo configuration:
<context:component-scan base-package="com.example" />
<mongo:repositories base-package="com.example.repositories.mongodb" />
I have the repository as follows:
package com.example.repositories.mongodb;
public interface ReviewRepository extends CrudRepository<Review, String> {...}
and the bean:
package com.example.domain;
#Document(collection="Review")
public class Review implements Serializable { ... }
Unfortunately, when I start the app, I get the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private
com.example.repositories.mongodb.ReviewRepository
com.example.Controller.reviewRepository; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'reviewRepository': FactoryBean threw
exception on object creation; nested exception is
java.lang.IllegalArgumentException: Not an managed type: class
com.example.domain.Review
....
Caused by: java.lang.IllegalArgumentException: Not an managed type:
class com.example.domain.Review
I think that this might be caused by the repository and the bean it manages not being in the same package. But I get the same issue also when I do place them in the same package. Any ideas?
In the same project we were also using mysql and it was scanning the package: com.example.repositories. This caused the issue and once we changed it to com.example.repositories.mysql it was solved.

Resources