Grails AOP config exception - spring

I have a running grails app.
I upgraded the
springsecurity-core from 1.2.7.3 to 2.0-RC2
springsecurity-acl from 1.1.1 to 2.0-RC1
I added
springsecurity-oauth-provider 1.0.5.1
And I get an exception during startup:
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.codehaus.groovy.grails.commons.spring.TypeSpecifyableTransactionProxyFactoryBean]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at grails.plugin.springsecurity.acl.AclAutoProxyCreator.createProxy(AclAutoProxyCreator.java:120)
... 4 more
Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
... 5 more
(only last part of ex)

The workaround was to remove the "#PreAuhthorize" annotations from the code. These versions of springSecurity don't work well together from some reason.

Related

Mocking a Supplier<>-Bean

I would like to mock a Bean (using mockito) that is defined like so
#Bean("idGenerator")
public Supplier<UUID> idGenerator() {
return () -> UUID.randomUUID();
}
In a SpringBootTest-class I get an error using #MockBean indicating, that that Bean cannot be mocked (due to some limitations in the JVM? - sorry, I don't have the stacktrace at hand right now).
I came up with a workaround that does not use Mocks but an additional field in a #TestConfiguration so that the return-value of the supplier can be specified externally.
Since I don't really like that workaround (and my colleagues won't either), I hope there is a proved pattern or the realization I am doing that mocking wrong.
Edit
Here is the stacktrace I am getting. As Markus pointed out - the standard unit-tests work - it seems to be a shortcoming of cucumber-java:
Before All/After All failed
io.cucumber.core.exception.CompositeCucumberException: There were 15 exceptions. The details are in the stacktrace below.
at io.cucumber.core.runtime.RethrowingThrowableCollector.getThrowable(RethrowingThrowableCollector.java:57)
at io.cucumber.core.runtime.CucumberExecutionContext.getThrowable(CucumberExecutionContext.java:102)
at io.cucumber.core.runtime.CucumberExecutionContext.finishTestRun(CucumberExecutionContext.java:97)
at io.cucumber.core.runtime.Runtime.execute(Runtime.java:96)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:87)
at io.cucumber.core.cli.Main.run(Main.java:87)
at io.cucumber.core.cli.Main.main(Main.java:30)
Suppressed: java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at io.cucumber.spring.TestContextAdaptor.<init>(TestContextAdaptor.java:32)
at io.cucumber.spring.SpringFactory.start(SpringFactory.java:120)
at io.cucumber.core.runner.Runner.buildBackendWorlds(Runner.java:134)
[...]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name [...]: Unsatisfied dependency expressed through constructor parameter 5: Initialization of bean failed;
nested exception is org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class BackendApplicationConfiguration$$Lambda$1713/0x00000008018fd980
Mockito cannot mock/spy because :
- VM does not support modification of given type
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
You can just define it like follows:
#MockBean(name = "idGenerator")
private Supplier<UUID> mockedSupplier;
there is no issue that prevents this from mocking. Would be good to include your stacktrace, as the issue is probably somewhere else.

Spring can't created bean when class has $1.class

When I compiled my java's file,and java compiler generated extra class,like example.class,example$1.class,example$2.class in my package,
and My ApplicationContext's file to scan component like this.
<context:component-scan base-package="com.test">
<context:include-filter type="regex" expression="com\.test\..*"/>
then I got this error message.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example.1' defined in file [/tmp/jetty-0.0.0.0-8050-relayserver.war-_relayserver-any-2814616804903816631.dir/webapp/WEB-INF/classes/com/test/example$1.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.test.example$1]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.test.example$1.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
For now,I just only set exclude filter to do this.
MyClass$1 (and $2 and so on) classes are generated for anonymous inner classes in your class. Anonymous inner classes are commonly used when using the listener pattern, they are defined like this:
MyInterface listener = new MyInterface() { // this is the anonymous inner class
... // implementation
}
So I guess you did try to use autowire on an anonymous inner class, which I suspect is not supported by spring (hard to say more without code).

Grails 2.4.3 Upgrade - Spring Security ACl - SpelExpressionParser

Getting the below error while running the app, no issues during compile. Kindly help, how to fix this issue.
ERROR [localhost-startStop-1] 2014-09-26 19:19:19,050 org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener.error(213) : Error initializing the application: Error creating bean with name 'flowBuilderServices': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.expression.spel.standard.SpelExpressionParser' to required type 'org.springframework.binding.expression.ExpressionParser' for property 'expressionParser'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.expression.spel.standard.SpelExpressionParser] to required type [org.springframework.binding.expression.ExpressionParser] for property 'expressionParser': no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowBuilderServices': Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.expression.spel.standard.SpelExpressionParser' to required type 'org.springframework.binding.expression.ExpressionParser' for property 'expressionParser'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.expression.spel.standard.SpelExpressionParser] to required type [org.springframework.binding.expression.ExpressionParser] for property 'expressionParser': no matching editors or conversion strategy found
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.expression.spel.standard.SpelExpressionParser' to required type 'org.springframework.binding.expression.ExpressionParser' for property 'expressionParser'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.expression.spel.standard.SpelExpressionParser] to required type [org.springframework.binding.expression.ExpressionParser] for property 'expressionParser': no matching editors or conversion strategy found
... 4 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.expression.spel.standard.SpelExpressionParser] to required type [org.springframework.binding.expression.ExpressionParser] for property 'expressionParser': no matching editors or conversion strategy found
What plugins are you using? I was getting the same exception when upgrading to Grails 2.4.3 and just narrowed it down (it wasn't easy) to the fact that the Grails WebFlow plugin and the ACL plugin both setup a bean named expressionParser, but each plugin is using a different class... one is overwriting the other during Grails initialization of spring beans. By renaming the web flow plugin's expressionParser to flowExpressionParser in the WebFlowPluginSupport.groovy file, I was able to get around this exception. You could search in your plugins directory for 'expressionParser' and see if you have >1 plugin using it.
I have let the webflow plugin folks know, and a bit more detail here.
https://jira.grails.org/browse/GPWEBFLOW-109

Spring - Instanciate a bean with a class which throws an Exception

I'm trying to instanciate a bean which constructor could throw an Exception.
I can't modify this class (given by an external team).
<bean id="myClass" class="myClass" />
The myClass constructor throws Exception.
I've been thinking about extending this class with a Singleton Pattern which is the behavior I want (be sure to instantiate only one instance of MyClass).
Error message :
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'myClass'
defined in class path resource
[.../spring_applicationContext.xml]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[myClass]:
Constructor threw exception; nested exception is
java.lang.ExceptionInInitializerError
Thanks in advance for your answers
I think the problem comes from the fact that my constructor throws an Exception.
My question is : with Spring, is it possible to instanciate a bean with a constructor which could throw an exception ?
If no bean scope is specified in bean configuration file, default to singleton. Your bean myClass is a singleton and you dont need to do anything more.
Spring Doc
Spring beans are by default singletons.
You should provide more of the stacktrace - what you're showing indicates that it is not myClass that is the problem, but the ClassPathXmlApplicationContext constructor that throws an exception - the cause usually follows later in the stacktrace.
Cheers,
As per stack trace, looks like there is an unexpected error either in static block or variable while creating an object of class 'myClass'.

Error while injecting a Spring bean into a JSF ManagedBean

I have a JSF ManagedBean which has a property that should be set by Spring. However, I get the following error:
Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert persistence.AuthDao#2f6e6ad9 of type class $Proxy166 to class persistence.AuthDao
at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:68)
at com.sun.faces.el.ELUtils.coerce(ELUtils.java:536)
at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:592)
at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606)
... 57 more
Caused by: java.lang.IllegalArgumentException: Cannot convert persistence.AuthDao#2f6e6ad9 of type class $Proxy166 to class persistence.AuthDao
at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397)
at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:66)
I have the ELresolver in faces-config.xml.
<managed-bean>
<managed-bean-name>authController</managed-bean-name>
<managed-bean-class>controllers.AuthController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>authDao</property-name>
<value>#{authDao}</value>
</managed-property>
</managed-bean>
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
It seems that it can find the class, but the class is of another type ($Proxy166?, not sure where that comes from).
PS: Removing the ELResolver seems to do the trick; I thought explicitly providing managed-bean in faces-config.xml would override ELResolver. Is there any way of both of these to coexist, then? Similarly, if I provide both annotation and XML configuration for a bean, which one of these is preferred, or is there a way to merge them, provide some properties in annotation, some in XML?
PPS: After adding interfaces and changing my current classes to implement them, I get the following error:
Error occurred during deployment: Exception while loading the app :
java.lang.IllegalStateException: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'authDao' defined in ServletContext resource
[/WEB-INF/applicationContext.xml]: Initialization of bean failed;
nested exception is
org.springframework.beans.ConversionNotSupportedException: Failed to
convert property value of type '$Proxy157 implementing
persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'persistence.UserDaoImpl' for property 'userDao';
nested exception is java.lang.IllegalStateException: Cannot convert
value of type [$Proxy157 implementing
persistence.UserDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [persistence.UserDaoImpl] for property 'userDao': no
matching editors or conversion strategy found. Please see server.log
for more details.
This is a proxy of your class. You are implementing an interface, so spring creates a proxy around the interface, but you are trying to inject by concrete type. Switch to the interface instead (in the managed bean).
If you really need for some reason to inject by concrete class, you can use #Scoped(proxyMode=ScopeProxyMode.TARGET_CLASS)

Resources