I created a spring boot application that this use spring data postgresql to access to a database. When I test this application works fine. However, I want to use this application in other project that doesn't use spring.
I have a big project that contains two o four java aplications, I use maven to integrate all this project (through modules). I added the dependency in the java aplication in this way:
enter image description here
<dependency>
<groupId>databases</groupId>
<artifactId>databases</artifactId>
<version>${version}</version>
</dependency>
When I try to use the class created into the project Spring boot, I have the problem that this uses #Autowired and when I execute the project java, a null pointer error appears because the internal class, that is injected is null.
This is the class created in the java project (don't use spring boot)
public class MainController implements Initializable {
private TagServiceBean tagService = new TagServiceBean ();
#Override
public void initialize(URL location, ResourceBundle resources) {
Collection<Tag> tags = tagService.findAll();
for (Tag tag : tags){
selectedTags.getItems().add(tag.getF_tagname());
}
}
}
In the line "Collection tags = TagService.findAll(); I have the following error:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at org.tenergia.historicosFX.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at org.tenergiaEditor.databases.service.spring.TagServiceBean.findAll(TagServiceBean.java:21)
at org.tenergia.historicosFX.controller.MainController.initialize(MainController.ja va:134)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
I'm assuming in TagServiceBean you are using #Autowired to inject other dependencies into fields. These fields are not automatically filled with values since you do not use Spring in your "Java Project".
You can either fill all those fields by hand (which would be quite cumbersome) or you use Spring in your "Java Project" to let Spring to the dirty work.
Related
I am currently experimenting with Spring Boot, the Axon Framework and java modules (using JDK 17).
I have created a repo to illustrate the problem: here
The problem is that the application does not start and I get the exception as follows:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountViewProjection' defined in file [C:\workspace\axon\_issue_replication_repos\axon-module-error\axon-example-app\build\classes\java\main\io\github\vab2048\axon\example_bug\app\query\account\AccountViewProjection.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.IllegalAccessError-->superinterface check failed: class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection$$EnhancerBySpringCGLIB$$159eee6a (in module io.github.vab2048.axon.example_bug.app) cannot access class org.springframework.aop.framework.Advised (in unnamed module #0x732d0d24) because module io.github.vab2048.axon.example_bug.app does not read unnamed module #0x732d0d24
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:628) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.20.jar:na]
at spring.context#5.3.20/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.20.jar:na]
at spring.context#5.3.20/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.20.jar:na]
at spring.boot#2.7.0/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.0.jar:na]
at spring.boot#2.7.0/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.0.jar:na]
at spring.boot#2.7.0/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.0.jar:na]
at spring.boot#2.7.0/org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.0.jar:na]
at spring.boot#2.7.0/org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.0.jar:na]
at spring.boot#2.7.0/org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.0.jar:na]
at io.github.vab2048.axon.example_bug.app/io.github.vab2048.axon.example_bug.app.AxonApplication.main(AxonApplication.java:10) ~[main/:na]
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.IllegalAccessError-->superinterface check failed: class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection$$EnhancerBySpringCGLIB$$159eee6a (in module io.github.vab2048.axon.example_bug.app) cannot access class org.springframework.aop.framework.Advised (in unnamed module #0x732d0d24) because module io.github.vab2048.axon.example_bug.app does not read unnamed module #0x732d0d24
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:209) ~[spring-aop-5.3.20.jar:5.3.20]
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110) ~[spring-aop-5.3.20.jar:5.3.20]
at org.axonframework.spring#4.5.9/org.axonframework.spring.config.AbstractAnnotationHandlerBeanPostProcessor.createAdapterProxy(AbstractAnnotationHandlerBeanPostProcessor.java:190) ~[axon-spring-4.5.9.jar:na]
at org.axonframework.spring#4.5.9/org.axonframework.spring.config.AbstractAnnotationHandlerBeanPostProcessor.postProcessAfterInitialization(AbstractAnnotationHandlerBeanPostProcessor.java:89) ~[axon-spring-4.5.9.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:455) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808) ~[spring-beans-5.3.20.jar:na]
at spring.beans#5.3.20/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.20.jar:na]
... 15 common frames omitted
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.IllegalAccessError-->superinterface check failed: class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection$$EnhancerBySpringCGLIB$$159eee6a (in module io.github.vab2048.axon.example_bug.app) cannot access class org.springframework.aop.framework.Advised (in unnamed module #0x732d0d24) because module io.github.vab2048.axon.example_bug.app does not read unnamed module #0x732d0d24
at spring.core#5.3.20/org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:512) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.3.20.jar:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at spring.core#5.3.20/org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572) ~[spring-core-5.3.20.jar:na]
at spring.core#5.3.20/org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419) ~[spring-core-5.3.20.jar:na]
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57) ~[spring-aop-5.3.20.jar:5.3.20]
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:206) ~[spring-aop-5.3.20.jar:5.3.20]
... 21 common frames omitted
Caused by: java.lang.IllegalAccessError: superinterface check failed: class io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection$$EnhancerBySpringCGLIB$$159eee6a (in module io.github.vab2048.axon.example_bug.app) cannot access class org.springframework.aop.framework.Advised (in unnamed module #0x732d0d24) because module io.github.vab2048.axon.example_bug.app does not read unnamed module #0x732d0d24
at java.base/java.lang.ClassLoader.defineClass0(Native Method) ~[na:na]
at java.base/java.lang.System$2.defineClass(System.java:2307) ~[na:na]
at java.base/java.lang.invoke.MethodHandles$Lookup$ClassDefiner.defineClass(MethodHandles.java:2439) ~[na:na]
at java.base/java.lang.invoke.MethodHandles$Lookup$ClassDefiner.defineClass(MethodHandles.java:2416) ~[na:na]
at java.base/java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:1843) ~[na:na]
at java.base/jdk.internal.reflect.GeneratedMethodAccessor33.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at spring.core#5.3.20/org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:507) ~[spring-core-5.3.20.jar:na]
... 35 common frames omitted
The offending code seems to to be in io.github.vab2048.axon.example_bug.app.query.account.AccountViewProjection:
#QueryHandler
public AccountView getAccount(GetAccountView query) {
log.debug("Handling: {}", query);
return repository.findById(query.id()).orElseThrow();
}
It is the Axon annotation #QueryHandler which causes it (you can verify this by commenting it out and then everything works). This is strange because none of the other Axon annotations cause this problem. The package itself is both exported and open in the module-info.java file.
What am I doing wrong? I am aware there may be command line arguments which may be added to solve this issue but am not sure what exactly to use (ideally I shouldn't have to give any command line arguments at all).
I am somewhat in doubt about posting this as an answer, but I will do it regardless.
Have you tried removing Spring AOP from the mix?
It seems like Spring AOP is making some inner classes that cause the IllegalAccessError, not so much Axon Framework.
If this solves the issue, I'd wager there's something to be done with Spring AOP to make this work.
Sadly, this isn't my forte, so I'd have to investigate what kind of settings, if any, would help in that perspective.
Recently I've been trying to set and customize scheduler in our XPages project. I've tried to put the following snippet in our project:
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator extends Plugin implements BundleActivator {
#Override
public void start(final BundleContext context) throws Exception {
// Here you start your scheduler
}
}
In the Code section of a template I can easily write this code I don't have a warning about unresolved dependencies. So, the org.eclipse.core.runtime and org.osgi.framework packages must be shipped with Domino I thought.
But it seems that it does not.
Whenever this code is executed I get 500 error in a browser and the stack as follows:
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org.eclipse.core.runtime.Plugin
at com.ibm.xsp.webapp.FacesServlet.handleError(FacesServlet.java:595)
at com.ibm.xsp.webapp.FacesServlet.renderErrorPage(FacesServlet.java:424)
at com.ibm.xsp.webapp.FacesServlet.service(FacesServlet.java:182)
at com.ibm.xsp.webapp.FacesServletEx.service(FacesServletEx.java:138)
at com.ibm.xsp.webapp.DesignerFacesServlet.service(DesignerFacesServlet.java:103)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.invokeServlet(ComponentModule.java:588)
at com.ibm.domino.xsp.module.nsf.NSFComponentModule.invokeServlet(NSFComponentModule.java:1335)
at com.ibm.designer.runtime.domino.adapter.ComponentModule$AdapterInvoker.invokeServlet(ComponentModule.java:865)
at com.ibm.designer.runtime.domino.adapter.ComponentModule$ServletInvoker.doService(ComponentModule.java:808)
at com.ibm.designer.runtime.domino.adapter.ComponentModule.doService(ComponentModule.java:577)
at com.ibm.domino.xsp.module.nsf.NSFComponentModule.doService(NSFComponentModule.java:1319)
at com.ibm.domino.xsp.module.nsf.NSFService.doServiceInternal(NSFService.java:662)
at com.ibm.domino.xsp.module.nsf.NSFService.doService(NSFService.java:482)
at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.doService(LCDEnvironment.java:357)
at com.ibm.designer.runtime.domino.adapter.LCDEnvironment.service(LCDEnvironment.java:313)
at com.ibm.domino.xsp.bridge.http.engine.XspCmdManager.service(XspCmdManager.java:272)
Caused by: java.lang.NoClassDefFoundError: org.eclipse.core.runtime.Plugin
at java.lang.ClassLoader.defineClassImpl(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:346)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.findClass(ModuleClassLoader.java:455)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:850)
at java.lang.ClassLoader.loadClass(ClassLoader.java:829)
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:403)
at java.lang.ClassLoader.loadClass(ClassLoader.java:809)
at ru.lanit.egrz.scheduler.RefreshEGRZTokenExecutor.executeAndScheduleRefreshToken(RefreshEGRZTokenExecutor.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:508)
at com.ibm.jscript.types.JavaAccessObject.call(JavaAccessObject.java:322)
at com.ibm.jscript.types.FBSObject.call(FBSObject.java:161)
at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:197)
at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)
at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139)
at com.ibm.jscript.JSExpression._interpretExpression(JSExpression.java:435)
at com.ibm.jscript.JSExpression.access$1(JSExpression.java:424)
at com.ibm.jscript.JSExpression$2.run(JSExpression.java:414)
at java.security.AccessController.doPrivileged(AccessController.java:686)
at com.ibm.jscript.JSExpression.interpretExpression(JSExpression.java:410)
at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:251)
at com.ibm.jscript.JSExpression.evaluateValue(JSExpression.java:234)
at com.ibm.xsp.javascript.JavaScriptInterpreter.interpret(JavaScriptInterpreter.java:222)
at com.ibm.xsp.binding.javascript.JavaScriptMethodBinding.invoke(JavaScriptMethodBinding.java:111)
at com.ibm.xsp.application.ActionListenerImpl.processAction(ActionListenerImpl.java:60)
at javax.faces.component.UICommand.broadcast(UICommand.java:324)
at com.ibm.xsp.component.UIEventHandler.broadcast(UIEventHandler.java:366)
at com.ibm.xsp.component.UIDataPanelBase.broadcast(UIDataPanelBase.java:400)
at com.ibm.xsp.component.UIViewRootEx.broadcast(UIViewRootEx.java:1535)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:307)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:428)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:94)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:210)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:96)
at com.ibm.xsp.controller.FacesControllerImpl.execute(FacesControllerImpl.java:256)
at com.ibm.xsp.webapp.FacesServlet.serviceView(FacesServlet.java:227)
at com.ibm.xsp.webapp.FacesServletEx.serviceView(FacesServletEx.java:157)
at com.ibm.xsp.webapp.FacesServlet.service(FacesServlet.java:159)
... 13 more
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.Plugin
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:359)
at java.lang.ClassLoader.loadClass(ClassLoader.java:809)
... 54 more
IBM literally freaks me out over and over and over again (sorry for that).
I tried to add it as external jars in the section -> Still the same.
I even tried to add these two to lib/ext -> Still the same.
In the build path these two included as Plug-In Dependencies.
Literally, there's something wrong with importing jars and resolving dependencies in Domino (even beyond the scope of this particular problem) I believe.
This is my 3rd if not 4th question on IBM Domino. I sincerely apologize to the community for being that embarrassing but get me right please. In Domino you solve one problem and then the other two emerge. You solve these two and then the other 4 break the app down. And so on
How do I import it correctly without any problems?
Thanks in advance
UPD
To improve my question I'll add some code
So, the bean looks like this:
SchedulerActivatorBean:
import java.io.Serializable;
public class SchedulerActivatorBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3559937702716018697L;
public static void activate() throws Exception
{
new SchedulerActivator().start(null);
}
}
SchedulerActivator (OSGI plugin):
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class SchedulerActivator extends Plugin implements BundleActivator {
#Override
public void start(final BundleContext context) throws Exception {
// Here you start your scheduler
QuartzScheduler.getInstance(); // starts the scheduler
}
}
Then I modified faces-config.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>schedulerActivatorBean</managed-bean-name>
<managed-bean-class>ru.lanit.egrz.scheduler.SchedulerActivatorBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
</faces-config>
And finally I call the bean from SSJS:
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete"
action="#{javascript:schedulerActivatorBean.activate()}">
</xp:eventHandler>
The bean is instantiated and available in the app. But when I call the method, I still get:
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.Plugin
at com.ibm.domino.xsp.module.nsf.ModuleClassLoader$DynamicClassLoader.loadClass(ModuleClassLoader.java:359)
at java.lang.ClassLoader.loadClass(ClassLoader.java:809)
... 54 more
Which, needless to say, left me clueless
A plugin doesn’t live in the NSF. You might want to learn about that first. Use this as a starter: https://www.slideshare.net/fiorep/domino-osgi-development
You might want to revisit your whole approach. Why not use a Notes agent that is scheduled and refreshes token which you store in an NSF. Simple problem solved.
Then have your XPage lookup the values in the NSF. Saves you the whole headache.
For new users you can use agent.run from XPages. The trick is the storage in the NSF.
Use reader fields to secure documents, so users can’t see each other tokens.
Currently I am using AspectJ Load Time Weaving to intercept the constructor of a base entity for auditing purposes. However when running the application I am getting incredibly inconsistent results revolving around the aspectOf() method that aspectJ weaves into LTW classes.
In some cases the application runs, the weaving is done correctly, and the code is functioning as expected. Other times I am met with:
java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
Currently I am using https://github.com/subes/invesdwin-instrument to dynamically attach the instrumentation agent into the JVM so our deployment guys don't need to do any extra configuration.
My Spring application main:
#SpringBootApplication
#EntityScan(basePackages = {"ca.gc.cfp.model"})
public class CfpWsApplication {
public static void main(final String[] args) {
DynamicInstrumentationLoader.waitForInitialized();
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
if (!InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
throw new IllegalStateException(
"Instrumentation is not available AspectJ weaver will not function.");
}
SpringApplication.run(CfpWsApplication.class, args);
}
The LTW Aspect:
#Aspect
public class BaseEntityAspect {
Logger logger = LoggerFactory.getLogger(BaseEntityAspect.class);
/** Application context property needed to fetch the AuditDate bean */
#Autowired private ApplicationContext context;
#AfterReturning(
"onBaseEntityCreated() && !within(ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect)")
public void injectAuditTimeStamp(JoinPoint joinPoint) {
try {
AuditDate auditDate = context.getBean(AuditDate.class);
Object entityTarget = joinPoint.getTarget();
// Inject the auditing date for this Entity instance
if (entityTarget instanceof BaseEntity) {
BaseEntity baseEnt = (BaseEntity) entityTarget;
baseEnt.setAuditDate(auditDate.getAuditingTimeStamp());
}
} catch (NullPointerException e) {
logger.error(
e.getMessage()
+ " Not yet in the conext of an httpRequest, the AuditDate bean has not yet been instantiated.");
}
}
#Pointcut(
"execution(ca.gc.cfp.model.entity.BaseEntity.new(..)) && !within(ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect)")
public void onBaseEntityCreated() {}
}
The Aspect config class with a temporary factory method using the Aspects utils to let spring know it should ask aspectJ for the woven Aspect:
#Configuration
#EnableAspectJAutoProxy
public class AspectConfig {
/**
* Static factory for access to the load time woven aspect. This allows the aspect to be injected
* with the application context and beans from the spring IoC
*/
#Bean
public BaseEntityAspect getBaseEntityAspect() {
return Aspects.aspectOf(BaseEntityAspect.class);
}
}
aop.xml:
<aspectj>
<weaver options="-verbose -showWeaveInfo -Xreweavable -debug">
<include within="ca.gc.cfp.model" />
<include within="ca.gc.cfp.model..*" />
<include within="ca.gc.cfp.core.cfpws.repository.aspect..*"/>
</weaver>
<aspects>
<aspect name="ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect" />
</aspects>
</aspectj>
In a lot of cases when I run with this configuration I get the following:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect]: Factory method 'getBaseEntityAspect' threw exception; nested exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 19 common frames omitted
Caused by: org.aspectj.lang.NoAspectBoundException: Exception while initializing ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at org.aspectj.lang.Aspects.aspectOf(Aspects.java:50) ~[aspectjrt-1.9.4.jar:1.9.4]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig.getBaseEntityAspect(AspectConfig.java:22) ~[classes/:na]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58.CGLIB$getBaseEntityAspect$0(<generated>) ~[classes/:na]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58$$FastClassBySpringCGLIB$$84edb9e.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58.getBaseEntityAspect(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 20 common frames omitted
Caused by: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at java.lang.Class.getDeclaredMethod(Class.java:2130) ~[na:1.8.0_211]
at org.aspectj.lang.Aspects.getSingletonOrThreadAspectOf(Aspects.java:134) ~[aspectjrt-1.9.4.jar:1.9.4]
at org.aspectj.lang.Aspects.aspectOf(Aspects.java:45) ~[aspectjrt-1.9.4.jar:1.9.4]
... 31 common frames omitted
However this is not always the case, occasionally the application runs just fine, the code executes as expected and there are no issues. This has been eluding me for a couple weeks now and I cannot seem to figure out what could be causing this code to occasionally work and occasionally not work. Could it be that CGLIB proxies are hiding the aspectOf() method from the compiler??
EDIT / UPDATE: I was able to remove the use of the above third party dependency for dynamically loading the java agent into the Spring application context. I instead used javaagent arguments and it is working fine from within my IDE. However building and running via Maven in a terminal is still causing issues. I've specified the javaagent argument via the Environment variable : MAVEN_OPTS. After doing so maven seems to be picking it up but my class still isn't being woven.
With regard to your own answer: The AspectJ Maven plugin helps you with compile-time weaving (CTW), not load-time weaving (LTW). For LTW you need to make sure that the weaving agent is active before any of the target classes have been loaded because LTW works at class-loader level. AspectJ Maven is only necessary if either you use aspects in native syntax (not annotation-based) or you want to use CTW.
I don't know about this invesdwin-instrument tool, but the AspectJ weaver offers its own capability to attach it during runtime. A third-party tool should not be necessary. Anyway, I do recommend to modify the Java command line in order to ensure the weaving agent is in place before anything else is loaded in your container. Your deployment guys ought to help you make sure that the -javaagent:... parameter is there. It is their job! To work around that in order to make life easier for them but the behaviour of your application potentially unpredictable is not going to improve its stability.
As per Kieveli's comment. My TL was thinking similarly. I'm new to Maven, Spring boot, and AOP. Looking into it externally run Maven and the build from within Eclipse seem to be "clobbering" one another.
I've been using maven via MINGW64 to clean / install / build, and the project doesn't specify usage of the AspectJ compiler which is likely why the factory method is not being woven into the class.
We're discussing a solution to this as we use Jenkins for build automation on our server. I am thinking that this Maven plugin may be the solution. https://www.mojohaus.org/aspectj-maven-plugin/
I'm using Flyway 5.2.4 and OSGI Bundle Activator. I want to migrate database on bundle Start() method.
Here's my ActivatorClass:
import org.flywaydb.core.Flyway;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator implements BundleActivator {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private ServiceRegistration serviceRegistration;
public void start(BundleContext bundleContext) {
log.info("Starting...");
serviceRegistration = bundleContext.registerService(IConnector.class.getName(), new Connector(), null);
log.info("Started.");
//FlyWay Section
Flyway flyway = Flyway.configure().dataSource("jdbc:postgresql://localhost:5432/", "postgres", "12345").load();
flyway.migrate();
}
public void stop(BundleContext bundleContext) {
log.info("Stopping...");
serviceRegistration.unregister();
log.info("Stopped.");
}
}
As you can see, I'm using slf4j as my Logger. Maybe that's why I get this errors in my StackTrace after app deploying:
java.lang.Exception: Could not start bundle mvn:internship/db-connector/1.0.0 in feature(s) feature-1.0.0: Activator start error in bundle db-connector [2343].
at org.apache.karaf.features.internal.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:519)[20:org.apache.karaf.features.core:3.0.8]
at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeatures(FeaturesServiceImpl.java:474)[20:org.apache.karaf.features.core:3.0.8]
at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeature(FeaturesServiceImpl.java:415)[20:org.apache.karaf.features.core:3.0.8]
at Proxy683d032e_ad2c_4b9a_98f7_baca7b5564f1.installFeature(Unknown Source)[:]
at Proxyfff1bf4b_671e_4ff6_bb97_e0dabf9f20e8.installFeature(Unknown Source)[:]
at org.apache.karaf.kar.internal.KarServiceImpl.installFeatures(KarServiceImpl.java:282)[89:org.apache.karaf.kar.core:3.0.8]
at org.apache.karaf.kar.internal.KarServiceImpl.install(KarServiceImpl.java:111)[89:org.apache.karaf.kar.core:3.0.8]
at org.apache.karaf.kar.internal.KarServiceImpl.install(KarServiceImpl.java:93)[89:org.apache.karaf.kar.core:3.0.8]
at Proxy40d8d25a_37b2_4855_a381_fbb78daa68ce.install(Unknown Source)[:]
at Proxy068a2f57_120b_4ee8_b953_7c8262bae9a2.install(Unknown Source)[:]
at org.apache.karaf.deployer.kar.KarArtifactInstaller.update(KarArtifactInstaller.java:62)[91:org.apache.karaf.deployer.kar:3.0.8]
at Proxy3c143b4c_a2ef_49d0_870b_21a9b5f74704.update(Unknown Source)[:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.update(DirectoryWatcher.java:1101)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.update(DirectoryWatcher.java:898)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:478)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:355)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:307)[7:org.apache.felix.fileinstall:3.5.2]
Caused by: org.osgi.framework.BundleException: Activator start error in bundle db-connector [2343].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.karaf.features.internal.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:516)[20:org.apache.karaf.features.core:3.0.8]
... 16 more
Caused by: java.lang.ExceptionInInitializerError
at org.flywaydb.core.internal.util.FeatureDetector.isSlf4jAvailable(FeatureDetector.java:96)
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:39)
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)
at org.flywaydb.core.internal.util.FeatureDetector.<clinit>(FeatureDetector.java:25)
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:35)
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)
at org.flywaydb.core.Flyway.<clinit>(Flyway.java:86)
at internship.connectors.postgresConnector.Activator.start(Activator.java:18)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
... 20 more
Caused by: org.flywaydb.core.api.FlywayException: Unable to instantiate class org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator : org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator
at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:63)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.internal.logging.LogCreatorFactory.getLogCreator(LogCreatorFactory.java:46)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.api.logging.LogFactory.getLog(LogFactory.java:78)[2348:org.flywaydb.core:5.2.4]
at org.flywaydb.core.internal.util.ClassUtils.<clinit>(ClassUtils.java:39)[2348:org.flywaydb.core:5.2.4]
... 30 more
Caused by: java.lang.ClassNotFoundException: org.flywaydb.core.internal.logging.javautil.JavaUtilLogCreator
at java.net.URLClassLoader.findClass(Unknown Source)[:1.8.0_212]
at java.lang.ClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at java.lang.ClassLoader.loadClass(Unknown Source)[:1.8.0_212]
at java.lang.Class.forName0(Native Method)[:1.8.0_212]
at java.lang.Class.forName(Unknown Source)[:1.8.0_212]
at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:61)[2348:org.flywaydb.core:5.2.4]
... 33 more
So, I just have no idea what's wrong here, I've used simpliest example from official Flyway's site and get this errors.
I've already try to delete my own Logger. I thought it can let FlyWay to use it's logger, but everything goes the same way (same errors).
Could you help me?
P.S. Looks like this pull request somehow related to my problem.
Indeed it seems Flyway might have issues in OSGi. Maybe you can provide them an issue and your example.
Another issue with your example is that you try to access the DataSource via a url. This does not work in OSGi. The reason is that this way flyway has to have direct access to the database driver classes. This does not work in OSGi.
In OSGi the way to access a Database is with a DataSourceFactory which the database driver creates as a service. From this factory you can create a DataSource.
As not all database drivers offer this service there is pax-jdbc which provides factories for all common databases. It also allows to create a DataSource including pooling from a OSGi config.
Your approach of migrating on bundle start is a very bad idea. The methods in the activator must return quickly and a databse migration might take a while. Of course you want to make sure the migration takes place before any bundle in the system accesses the database. Fortunately there is a way to hook into the DataSource creation to do things like a migration.
See the liquibase tutorial which also shows a database migration. It uses the PreHook offered by pax-jdbc which makes sure your migration code is run before the DataSource is given to any other bundle.
I managed to avoid this error by setting a custom LogFactory before entering flyway's migration code. The basic idea is presented in this sample commit
If i compile my controller and main class for my javafx project, they both compile without any errors and if I run the main class the Javafx (the GUI that I built in scene-builder) will pop-up like it is supposed to if the controller is not connected to the javafx. As soon as I try to connect the controller java file to the javafx through scene builder and then try to run the project, the GUI will not popup and it will give me this list of errors:
C:\Users\Kenny's\Desktop\Project3>java Project3
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/C:/Users/Kenny's/Desktop/Project3/Project3.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at Project3.start(Project3.java:12)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.ComboBox field Project3Controller.location to java.net.URL
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2554)
... 17 more
Exception running application Project3
C:\Users\Kenny's\Desktop\Project3>
I have tried to find a solution and I have tried it with a completely blank controller class and everything and it still does not work.
Anyone know any solutions?
Oh, this is a bit evil.
When the FXML file is loaded, as you know, it injects fields annotated with #FXML in the controller with the elements with corresponding fx:id attributes (the attribute value in the FXML file matching the field name in the controller).
What is little known (and undocumented) is that there are a small number of (well, two) additional values that can be injected. For example, you can do
#FXML
private ResourceBundle resources ;
to get a reference to the resource bundle provided to the FXMLLoader. Similarly, you can get a reference to the URL provided to the FXMLLoader with (you guessed it):
#FXML
private URL location ;
These happen automatically.
So, it looks from the stack trace that you have a ComboBox defined in your FXML file and controller which you called location:
#FXML
private ComboBox<String> location ;
When the FXML loader sees this, possibly before it even tries to parse the FXML file, it tries to inject the URL provided to the loader into the field, and of course fails because it's trying to assign a URL to a ComboBox.
To fix, just change the name of the combo box:
#FXML
private ComboBox<String> locationCombo ;
and similarly for the fx:id attribute in your FXML file.