MetadataMBeanInfoAssembler does not support JDK dynamic proxies - java

Recently I added JMX support to my Spring web application.
This works fine for beans with #Component, however, for some services there seems to be a problem when adding #ManagedResource
I have a spring service annotated with #Service method.
I'm trying to add JMX to that service using #ManagedResource but it seems like spring is having troubles registering the beans:
org.springframework.beans.factory.BeanCreationException:
Caused by: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'mbeanExporter': Invocation of init method
failed; nested exception is
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable
to register MBean [...] with key 'appAnalysisServiceImpl'; nested
exception is org.springframework.jmx.export.MBeanExportException:
Could not create ModelMBean for managed resource [
l#6142152] with key 'myservice'; nested exception is java.lang.IllegalArgumentException: MetadataMBeanInfoAssembler does
not support JDK dynamic proxies - export the target beans directly or
use CGLIB proxies instead
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
From the exception message it seems like spring requires a special exporter for this kind of class.
How do i export the bean directly without writing my own exporter ?

If your service uses #Transactional then Spring won't be able to create a ModelBean since the service is proxied.

Related

component required a bean named 'org.springframework.boot.test.context.ImportsContextCustomizer$ImportsConfiguration'

I am getting this error while starting spring boot application.
App is running with Spring Boot v2.2.6.RELEASE, Spring v5.2.7.RELEASE
APPLICATION FAILED TO START
Description:
A component required a bean named 'org.springframework.boot.test.context.ImportsContextCustomizer$ImportsConfiguration' that could not be found.
Action:
Consider defining a bean named 'org.springframework.boot.test.context.ImportsContextCustomizer$ImportsConfiguration' in your configuration.
Also, this class is not public so it is now allowing me to create bean so if anyone faced this error and know resolution I would be appreciate your effort.

Spring configuration to access Openshift elasticsearch

I'm implementig a page that has a search field and is calling a elasticsearch service using spring.
The connection to the elasticsearch engine was done by using the spring bean provided by spring-elasticsearch (https://github.com/dadoonet/spring-elasticsearch):
<elasticsearch:client id="esClient" esNodes="localhost:9300" />
This worked well but I want to move my engine to a different server, so to development and test I created an app in Openshift with elasticsearch using this https://github.com/caruccio/openshift-elasticsearch and change the transported client esNode link. The problem here is always giving me errors like this one (or similar to this one):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.projectx.standard.services.common.SearchEngineService com.projectx.standard.app.controler.common.IndexController.searchEngineService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchEngineService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.elasticsearch.client.Client com.projectx.standard.services.common.implementation.ElasticSearchServiceImpl.esClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'esClient': Invocation of init method failed; nested exception is java.lang.NumberFormatException: For input string: "//elastic-projectxserver.rhcloud.com/"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
I think the problem is in the link because I'm using the http://... in the esNodes field, but I'm not sure.
Someone know how to put the application to connect to an engine in the openshift?
btw, I found this https://github.com/spring-projects/spring-data-elasticsearch that looks interesting but I don't know if this will help solve my problem or is worth to change!
Thanks in advance
This will not work as the https://github.com/dadoonet/spring-elasticsearch code is expecting the configuration in the form of host:port. Looking at the exception, you are providing as host as http://elastic-projectxserver.rhcloud.com/. The library will split it by : and you will //elastic-projectxserver.rhcloud.com/ as port. This is the reason you are getting NumberFormatException as port should be an int. In your client configuration, try using elastic-projectxserver.rhcloud.com:80.
Also, I would recommend you use https://github.com/spring-projects/spring-data-elasticsearch as that is official Spring ElasticSearch library.

How to instantiate CDI bean at runtime via [weld] API in JavaEE [7]?

I am newbie in CDI and I want to instantiate CDI bean via my XML files, but not via beans.xml. Is it possible and is it normal? Or maybe I misunderstand CDI concepts?
I found the answer.
A producer method is a method that acts as a source of bean instances.
The method declaration itself describes the bean and the container
invokes the method to obtain an instance of the bean when no instance
exists in the specified context. A producer method lets the
application take full control of the bean instantiation process.

Disable Spring from Starting up after BeanInstantiationException (Tomcat)

for the project I'm currently working at there is the requirement to disable the webapp context to be started when spring fails to initialize some beans (that call webservices during initialization and therefore are likely to crash then).
However, when a bean throws any exception during initialization, it looks like this:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'latestAdsRepository' defined in file [...]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!!
...
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!!
however the context starts and all references to the bean that was not able to start are null. Needless to say, this causes various bad Nullpointer Exceptions all over the place. Especially because this bean is very important for the webapp.
So I need a way to explicitly tell Spring that this webapp has failed to start if a bean could not be initialized. System.exit(1) however is not an option, because there are also other webapps on this Tomcat server.
Any ideas?
As far as I know, by default, the context startup fails when Spring fails to instantiate a bean, if the exception is propagated through the startup listener to the servlet container.
You can use the #Required annotation to mark a certain injection point as mandatory.
How are you loading the Spring context?
Sounds like you might be doing it manually (i.e. you are calling new ApplicationContext() somewhere), instead you should use the ContextLoaderListener so that your context is loaded automatically upon application startup (and shutdown during application shutdown). Exceptions during the Spring context startup will propogate to failing the web application context startup.

Weblogic 10.3.1 gives 'Dependency injection failure' warning when publishing code

Weblogic 10.3.1 gives me a "Dependency injection failure" when I publish my code; the publish itself succeeds. I am working via Eclipse.
Basically I publish an ear with a web service aaa.MyWebServicePort that has a bean bbb.MyBean declared as local variable with the ejb 3.0 #EJB annotation. bbb.MyBean is also in the ear as well as a client project with the interfaces defined.
After publishing the web service works on the appserver and calls bbb.MyBean. So why the warning?
Details of the warning in the console window of Eclipse: Error creating bean with name 'aaa.MyWebServicePort': Initialization of bean failed; nested exception is [...] BeanCreationException: Dependency injection failure: can't find the bean definition about class interface bbb.MyBean; nested exception is [...] NoSuchBeanDefinitionException: No unique bean of type bbb.MyBean is defined: No beans of type bbb.MyBean;
There may be a couple of issues at work here. I'm getting a very similar error in just a straight servlet that is trying to load an ejb that isn't packaged in the same ear, I'm still trying to track that down. I also know that there is a difficulty when loading EJB v2.x that is supposed to be fixed in Weblogic 10.3.3.
If you find anything please let me know, and I'll do the same. One thing I have noticed is that if I load the EJB via initialContext.lookup, it works fine.
YMMV

Categories