Yuwensanofi patch 1
Hi Javlon thank you… I’ll take a look a this.
Thank you for you efforts
Best regards
Andy
Am 07.12.2014 um 00:02 schrieb Javlon [email protected]:
I use JacpFX. And want to share my implementation, made using Spring Boot. THANK YOU...
SpringBootLauncher.java
package org.jacpfx.spring.launcher;
import org.jacpfx.api.fragment.Scope; import org.jacpfx.api.launcher.Launcher; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
/**
@author created on 07.12.2014 by Javlon Eraliyev
*/ public class SpringBootLauncher implements Launcher<ApplicationContext> {
private ConfigurableApplicationContext context;
@Override public ConfigurableApplicationContext getContext() { if (context == null) { context = SpringApplication.run(ApplicationLauncher.class); } return context; }
@Override public <P> P getBean(Class<P> aClass) { return getContext().getBean(aClass); }
@Override public <P> P getBean(String s) { return (P) getContext().getBean(s); }
@Override public <P> P registerAndGetBean(Class<? extends P> type, String id, Scope scope) { if (getContext().containsBean(id)) { return getBean(id); } final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory(); final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory; final GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(type); if (scope != null) { beanDefinition.setScope(scope.getType()); } beanDefinition.setAutowireCandidate(true); registry.registerBeanDefinition(id, beanDefinition); factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); return getBean(id); } } AFXSpringBootLauncher.java
package org.jacpfx.spring.launcher;
import javafx.stage.Stage; import org.jacpfx.api.annotations.workbench.Workbench; import org.jacpfx.api.exceptions.AnnotationNotFoundException; import org.jacpfx.api.exceptions.AttributeNotFoundException; import org.jacpfx.api.exceptions.ComponentNotFoundException; import org.jacpfx.api.fragment.Scope; import org.jacpfx.api.launcher.Launcher; import org.jacpfx.rcp.workbench.EmbeddedFXWorkbench; import org.jacpfx.rcp.workbench.FXWorkbench; import org.springframework.context.ApplicationContext; import org.jacpfx.spring.launcher.SpringBootLauncher;
/**
JavaFX / Spring application launcher; This abstract class handles reference
to spring boot context and contains the JavaFX start method; * *
Example of usage:
@Configuration @ImportResource("classpath:mySpringApplicationContext.xml") public class ApplicationLauncher extends AFXSpringBootLauncher {
public static void main(String[] args) { Application.launch(args); }
@Override protected Class<? extends FXWorkbench> getWorkbenchClass() { return MyJacpFXWorkbench.class; }
@Override protected String[] getBasePackages() { return new String[]{"my.company"}; }
@Override public void postInit(Stage stage) { mainStage = stage; Scene scene = stage.getScene(); // add style sheet scene.getStylesheets().addAll( ApplicationLauncher.class.getResource("/myFolder/myStyle.css").toExternalForm() ); } }
@author created on 07.12.2014 by Javlon Eraliyev */ public abstract class AFXSpringBootLauncher extends ASpringLauncher {
@SuppressWarnings("unchecked") @Override public void start(Stage stage) throws Exception { initExceptionHandler(); scanPackegesAndInitRegestry(); final Launcher<ApplicationContext> launcher = new SpringBootLauncher(); final Class<? extends FXWorkbench> workbenchHandler = getWorkbenchClass(); if (workbenchHandler == null) throw new ComponentNotFoundException("no FXWorkbench class defined"); initWorkbench(stage, launcher, workbenchHandler); }
private void initWorkbench(final Stage stage, final Launcher<ApplicationContext> launcher, final Class<? extends FXWorkbench> workbenchHandler) { if (workbenchHandler.isAnnotationPresent(Workbench.class)) { this.workbench = createWorkbench(launcher, workbenchHandler); workbench.init(launcher, stage); postInit(stage); } else { throw new AnnotationNotFoundException("no @Workbench annotation found on class"); } }
private EmbeddedFXWorkbench createWorkbench(final Launcher<ApplicationContext> launcher, final Class<? extends FXWorkbench> workbenchHandler) { final Workbench annotation = workbenchHandler.getAnnotation(Workbench.class); final String id = annotation.id(); if (id.isEmpty()) throw new AttributeNotFoundException("no workbench id found for: " + workbenchHandler); final FXWorkbench handler = launcher.registerAndGetBean(workbenchHandler, id, Scope.SINGLETON); return new EmbeddedFXWorkbench(handler); }
} Dependency in pom.xml
— Reply to this email directly or view it on GitHub https://github.com/JacpFX/JacpFX/issues/12. org.springframework.boot spring-boot-starter ${spring.boot.version} log4j-over-slf4j org.slf4j org.springframework.boot spring-boot-starter-logging