Error when using #StepScope with Spring Batch Admin - spring

I'm using Spring Batch Admin to launch batches from a batch module of my main project.
The batch module compile as a JAR addedstrong text in the dependency of the Spring Batch Admin project as follow:
<dependency>
<groupId>company.project</groupId>
<artifactId>project-batch</artifactId>
<version>1.10.0-SNAPSHOT</version>
</dependency>
Since I added "#StepScope" on one of my reader classes, I get the following error when deploying Spring Batch Admin
Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy71]: 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 class com.sun.proxy.$Proxy71
I had the same error when running my unit test on the batch module (without Spring Batch Admin) but I resolved it using the following bean declaration in my test configuration:
<bean class="org.springframework.batch.core.scope.StepScope">
<property name="autoProxy" value="false" />
</bean>
But I don't find how to use similar declaration to prevent the error in the Spring Batch Admin project.
I tried to add the bean into a configuration on the module side or on the SBA project side but nothing seems to work.

As far as I can see this error not caused by SBA. This is usual attempt of spring to proxify your bean. Please check your class annotated with #StepScope and remove final from class definition and public method definitions.

Related

Spring Boot Bean 'Required bean of type that could not be found'

I have been working on learning how to use spring data and I have created a very simple project to test it. The folder structure and applicationcontext.xml is shown here:applicationcontext.xml and folder structure
The error I am getting is shown here:console error output
.
I have the applicationContext on my classpath and have a bean of that class declared, any idea as to what my problem could be ? Thank you.
EDIT:
I have updated my post to show the main class and the dao class, as well as my pom.xml contents (as at this point, I am wondering if I need to include another dependency . . . )
main class
dao (repository)
I miss the following line in your application log:
... o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [applicationContext.xml]
So I assume your applicationContext.xml file is not loaded at all.
Either add
#ImportResource("classpath:applicationContext.xml")
to your application class or add the
#Repository
annotation to your UserRepository class.
In my opinion you should avoid mixing Java and XML Spring configuration if possible.

persistenceUnitName with JPA2.1

I am having a Spring project (with JPA2.1) and persistenceUnitName is defined as "default" in that project.
I have defined the dependency (using maven) of this project (using spring-core.jar in another project to reuse some Entity classes and other stuff) in another spring batch project (that is too using spring-JPA2.1).
When I am defining the same persistenceUnitName in this project then it is giving me a conflict error of persistenceUnitName.
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="default"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:dataSource-ref="dataSource" />
But when I changed the name of persistenceUnitName in this project then it is not able to create the persistenceUnitName ("default") of imported project. So not able to create JPA classes of imported project (for which I defined the dependency in pom.xml)
Error creating bean with name 'SampleJpa': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'default' is defined
Please suggest me some solution. Actually this was working in this way with hibernate but migration is giving us hard time.
I created a new module in imported project and separated persistence.xml from that. And then I imported the module (module is now having only classes that I need to import in another project not the whole core). this way it is working fine.

Exporting Spring Bean Service in OSGI- New Instance per Injection Call

I am trying to export a spring bean as service using Spring's application context, I need to have them exported as "prototype" scope, but this is not possible due to OSGi Service registry caching the service as singleton.
Doing some research I came upon a post suggesting to use "session" scope to get around this issue. I am following this tutorial to get this working, but I am stuck with an issue that has to do with class loader not finding an interface.
Here's how I am declaring the bean
<osgi:service id="SimulationExporter" ref="simulationService" interface="org.geppetto.core.simulation.ISimulation"/>
<bean id="simulationService" scope="session" class="org.geppetto.simulation.SimulationService">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
When exporting the bean to another bundle, I get the following error
Caused by: java.lang.IllegalArgumentException: interface org.springframework.aop.scope.ScopedObject is not visible from class loader
at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:484)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:713)
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:117)
The bundle importing the service bean has the dependency org.springframework.aop-3.0.0.RELEASE , and this has also being copied to the virgo repository. Any ideas why would the class loader not find that interface that it needs for scope "session"?
Does your MANIFEST.MF include import packages for aop.scope?
If you are using Apache Felix to generate it try adding something like this:
<Import-Package>org.aopalliance.aop,org.springframework.aop,org.springframework.aop.scope,org.springframework.aop.framework,*</Import-Package>

Problems changing from Load Time Weaver to Compile Time Weaver in Spring

I'm trying to change from load-time-weaving to compile-time-weaving with my Spring 2.5 app.
To do this, I did the following:
In my ant build file, I added
<path id="aspectPath">
<pathelement location="${lib.home}/spring-aspects.jar"/>
</path>
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
<classpath>
<pathelement location="${aspectj.home}/aspectjtools.jar"/>
</classpath>
</taskdef>
and replaced the reference to the javac compiler with the following
<iajc sourceroots="${src.home}"
destdir="${build.home}/WEB-INF/classes"
classpathRef="compile.classpath"
aspectPathRef="compile.classpath"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
encoding="cp1252"
source="1.6"
target="1.6"
showWeaveInfo="${compile.debug}"/>
In applicationContext.xml I then replaced
<context:load-time-weaver/>
with
<context:spring-configured/>
Other configuration settings in my app context file, BTW, include
<tx:annotation-driven/>
<context:component-scan base-package="com.domain.somepackage"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
In the context.xml file, I removed the following from the loader tag
loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
When I run the build script, it compiles without errors.
I do get this warning however.
[iajc] warning at <Unknown>::0 Found #DeclareAnnotation while current release
does not support it (see 'org.aspectj.weaver.bcel.AtAjAttributes')
at the top, and this warning at the bottom:
[iajc] warning at C:\server-
lib\aspectjtools.jar!org\aspectj\ajdt\internal\compiler\
CompilerAdapter.class:121::0 advice defined in
org.aspectj.ajdt.internal.compiler.CompilerAdapter has not been
applied [Xlint:adviceDidNotMatch]
Most of the logging looks like:
[iajc] weaveinfo Join point 'method-execution(void com.kjconfigurator.upgra
de.Upgrade1_07HelperImp.addServiceParticipation(com.kjconfigurator.core.domain.U
ser, com.kjconfigurator.core.domain.ServiceAccount))' in Type 'com.kjconfigurato
r.upgrade.Upgrade1_07HelperImp' (Upgrade1_07HelperImp.java:196) advised by after
Returning advice from 'org.springframework.transaction.aspectj.AnnotationTransac
tionAspect' (spring-aspects.jar!AbstractTransactionAspect.class:77(from Abstract
TransactionAspect.aj))
I removed the tomcatspringweaver jar from the tomcat lib.
I am using aspectj1.7
When I start the app up, I get an error indicating that when a dao class is being injected into a service class there is an NPE at at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested
PropertyAccessExceptions (1) are: PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property 'dao' threw exception;
nested exception is java.lang.NullPointerException
The Dao class extends an AbstractJpaDao class that looks like this:
public abstract class AbstractJpaDao<T> {
private static Logger log = Logger.getLogger(AbstractJpaDao.class.getName());
private EntityManager entityManager;
#PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this. entityManager = entityManager;
}
...
}
It's been such a long time since all this was initially set up, I don't remember how all the configurations work. Nor do I understand class loaders or AspectJ very well. But something is not happening correctly, perhaps the Entitymanager is not being injected for some reason.
Questions.
What might be causing this?
Is <context:spring-configured/> really needed?
The package referenced by <context:component-scan base-package="com.domain.somepackage"/> does not include the Dao class in question. When I do add another component-scan tag with the dao's package in it, nothing different happens. Is this necessary?
Do you have any schedule tasks defined somewhere - it sounds like the scheduled tasks are getting triggered before the Spring context is completely initialized.
I finally found a solution to this problem with the help of a Spring consultant.
There was an aspect that was being called before it was fully initialized resulting in an NPE in the aspect. (Eclipse incorrectly showed the NPE originating in the class that was being advised.) I disabled the aspect by removing the annotations because the aspect was not critical; however, a better fix would have been for me to instruct Spring to initialize that class ahead of the others, or use a point cut expression that was more narrow and which excluded setter methods.

Spring: <util:properties> ignored in JUnit environment?

In my Spring servlet configuration (lets call it service-servlet.xml) I am loading properties from a file:
<util:properties id="ServiceProperties" location="classpath:service.properties" />
And then later autowiring them in my service class:
#Value("#{ServiceProperties['my.property']}")
private Integer myProp;
This works fine and service runs with no problems when deployed under Glassfish.
Now I am writing a test class for my service, and running it with SpringJUnit4ClassRunner:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({
"classpath:service-test-servlet.xml",
"classpath:service-test-context.xml"
})
public class ServiceTest {
// ...
}
So in service-test-servlet.xml, which is a copy of service-servlet.xml with few minor adjustments for testing environment, I do include the properties file the same way, but seems it is simply ignored...
First, I thought, I may be that my file is not found or so, but then even if I intentionally enter wrong name - it just ignores it and goes further with crashes at some later point because there is no bean named "ServiceProperties" and it cannot autowire "myProp" in my service class...
Is the <util:properties> being totally ignored in JUnit environment?

Resources