实现FX-PLus和Spring结合的Demo

This commit is contained in:
billkiller
2019-07-07 12:21:30 +08:00
parent bb53425b43
commit 7ce75c9206
35 changed files with 1326 additions and 497 deletions

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: commons-logging:commons-logging:1.2">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/commons-logging/commons-logging/1.2/commons-logging-1.2.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/commons-logging/commons-logging/1.2/commons-logging-1.2-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/commons-logging/commons-logging/1.2/commons-logging-1.2-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: log4j:log4j:1.2.17">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/log4j/log4j/1.2.17/log4j-1.2.17.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/log4j/log4j/1.2.17/log4j-1.2.17-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/log4j/log4j/1.2.17/log4j-1.2.17-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.javassist:javassist:3.21.0-GA">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.springframework:spring-aop:4.3.13.RELEASE">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-aop/4.3.13.RELEASE/spring-aop-4.3.13.RELEASE.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-aop/4.3.13.RELEASE/spring-aop-4.3.13.RELEASE-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-aop/4.3.13.RELEASE/spring-aop-4.3.13.RELEASE-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.springframework:spring-beans:4.3.13.RELEASE">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-beans/4.3.13.RELEASE/spring-beans-4.3.13.RELEASE.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-beans/4.3.13.RELEASE/spring-beans-4.3.13.RELEASE-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-beans/4.3.13.RELEASE/spring-beans-4.3.13.RELEASE-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.springframework:spring-context:4.3.13.RELEASE">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-context/4.3.13.RELEASE/spring-context-4.3.13.RELEASE.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-context/4.3.13.RELEASE/spring-context-4.3.13.RELEASE-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-context/4.3.13.RELEASE/spring-context-4.3.13.RELEASE-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.springframework:spring-core:4.3.13.RELEASE">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-core/4.3.13.RELEASE/spring-core-4.3.13.RELEASE.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-core/4.3.13.RELEASE/spring-core-4.3.13.RELEASE-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-core/4.3.13.RELEASE/spring-core-4.3.13.RELEASE-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.springframework:spring-expression:4.3.13.RELEASE">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-expression/4.3.13.RELEASE/spring-expression-4.3.13.RELEASE.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-expression/4.3.13.RELEASE/spring-expression-4.3.13.RELEASE-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/springframework/spring-expression/4.3.13.RELEASE/spring-expression-4.3.13.RELEASE-sources.jar!/" />
</SOURCES>
</library>
</component>

1134
.idea/workspace.xml generated
View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" /> <output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" /> <output-test url="file://$MODULE_DIR$/target/test-classes" />
@@ -15,5 +20,13 @@
<orderEntry type="library" name="Maven: org.ow2.asm:asm:4.2" level="project" /> <orderEntry type="library" name="Maven: org.ow2.asm:asm:4.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.13.RELEASE" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
</component> </component>
</module> </module>

View File

Binary file not shown.

23
pom.xml
View File

@@ -23,6 +23,29 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies> </dependencies>

View File

@@ -0,0 +1,14 @@
package cn.edu.scau.biubiusuisui.annotation;
import java.lang.annotation.*;
/**
* @Author jack
* @Date:2019/7/4 13:58
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface FXBind {
String [] bind();
}

View File

@@ -1,5 +1,7 @@
package cn.edu.scau.biubiusuisui.config; package cn.edu.scau.biubiusuisui.config;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.factory.FXFactory;
import javafx.fxml.*; import javafx.fxml.*;
/* /*
@@ -109,6 +111,16 @@ public class FXMLLoaderPlus {
private static final RuntimePermission GET_CLASSLOADER_PERMISSION = private static final RuntimePermission GET_CLASSLOADER_PERMISSION =
new RuntimePermission("getClassLoader"); new RuntimePermission("getClassLoader");
private boolean show = false;
public boolean isShow() {
return show;
}
public void setShow(boolean show) {
this.show = show;
}
// Abstract base class for elements // Abstract base class for elements
private abstract class Element { private abstract class Element {
public final cn.edu.scau.biubiusuisui.config.FXMLLoaderPlus.Element parent; public final cn.edu.scau.biubiusuisui.config.FXMLLoaderPlus.Element parent;
@@ -747,6 +759,8 @@ public class FXMLLoaderPlus {
super.processStartElement(); super.processStartElement();
updateValue(constructValue()); updateValue(constructValue());
//如果是FXBaseController对象需要注入fxml
//因为如果只是单纯反射不具有FXPlus功能只有FX功能
if (value instanceof Builder<?>) { if (value instanceof Builder<?>) {
processInstancePropertyAttributes(); processInstancePropertyAttributes();
@@ -1009,12 +1023,17 @@ public class FXMLLoaderPlus {
if (value == null) { if (value == null) {
try { try {
value = ReflectUtil.newInstance(type); if(FXBaseController.class.isAssignableFrom(type)) {
value = FXFactory.getFXController(type,fx_id);
}else{
value = type.newInstance();
}
} catch (InstantiationException exception) { } catch (InstantiationException exception) {
throw constructLoadException(exception); throw constructLoadException(exception);
} catch (IllegalAccessException exception) { } catch (IllegalAccessException exception) {
throw constructLoadException(exception); throw constructLoadException(exception);
} }
} }
} }
@@ -2551,6 +2570,7 @@ public class FXMLLoaderPlus {
((Initializable)controller).initialize(location, resources); ((Initializable)controller).initialize(location, resources);
} else { } else {
// Inject controller fields // Inject controller fields
if (!show) {
Map<String, List<Field>> controllerFields = Map<String, List<Field>> controllerFields =
controllerAccessor.getControllerFields(); controllerAccessor.getControllerFields();
@@ -2576,6 +2596,7 @@ public class FXMLLoaderPlus {
} }
} }
} }
}
} catch (final LoadException exception) { } catch (final LoadException exception) {
throw exception; throw exception;
} catch (final Exception exception) { } catch (final Exception exception) {

View File

@@ -1,7 +1,10 @@
package cn.edu.scau.biubiusuisui.config; package cn.edu.scau.biubiusuisui.config;
import cn.edu.scau.biubiusuisui.annotation.FXScan; import cn.edu.scau.biubiusuisui.annotation.FXScan;
import cn.edu.scau.biubiusuisui.factory.BeanBuilder;
import cn.edu.scau.biubiusuisui.factory.FXBuilder;
import cn.edu.scau.biubiusuisui.factory.FXFactory; import cn.edu.scau.biubiusuisui.factory.FXFactory;
import cn.edu.scau.biubiusuisui.function.FXWindowParser;
import cn.edu.scau.biubiusuisui.utils.ClassUtils; import cn.edu.scau.biubiusuisui.utils.ClassUtils;
import java.util.List; import java.util.List;
@@ -17,7 +20,14 @@ import java.util.Set;
public class FXPlusApplication { public class FXPlusApplication {
public static void start(Class clazz){ private static FXWindowParser windowAnnotationParser = new FXWindowParser();
private static BeanBuilder DEFALUT_BEAN_FACTORY = new FXBuilder();
private static BeanBuilder beanBuilder;
public static void start(Class clazz, BeanBuilder beanBuilder){
FXPlusApplication.beanBuilder = beanBuilder;
Annotation []annotations = clazz.getDeclaredAnnotations(); Annotation []annotations = clazz.getDeclaredAnnotations();
for(Annotation annotation : annotations){ for(Annotation annotation : annotations){
if(FXScan.class.equals(annotation.annotationType())){ if(FXScan.class.equals(annotation.annotationType())){
@@ -32,7 +42,7 @@ public class FXPlusApplication {
List<String> temps = classUtils.scanAllClassName(dir); List<String> temps = classUtils.scanAllClassName(dir);
for(String className:temps){ for(String className:temps){
try { try {
loadFXPlusClass(className); loadFXPlusClass(className,beanBuilder);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -41,9 +51,12 @@ public class FXPlusApplication {
} }
} }
} }
public static void start(Class clazz){
start(clazz,DEFALUT_BEAN_FACTORY);
}
private static void loadFXPlusClass(String className) throws ClassNotFoundException { private static void loadFXPlusClass(String className,BeanBuilder beanBuilder) throws ClassNotFoundException {
Class clazz = Class.forName(className); Class clazz = Class.forName(className);
FXFactory.loadFXController(clazz); FXFactory.loadFXController(clazz,beanBuilder);
} }
} }

View File

@@ -56,6 +56,7 @@ public class FXBaseController extends Pane {
FXMLLoaderPlus fxmlLoader = new FXMLLoaderPlus(getClass().getClassLoader().getResource(fxController.path())); FXMLLoaderPlus fxmlLoader = new FXMLLoaderPlus(getClass().getClassLoader().getResource(fxController.path()));
fxmlLoader.setRoot(this); fxmlLoader.setRoot(this);
fxmlLoader.setController(this); fxmlLoader.setController(this);
fxmlLoader.setShow(true);
try { try {
fxmlLoader.load(); fxmlLoader.load();
} catch (IOException e) { } catch (IOException e) {

View File

@@ -2,8 +2,11 @@ package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.FXScan; import cn.edu.scau.biubiusuisui.annotation.FXScan;
import cn.edu.scau.biubiusuisui.config.FXPlusApplication; import cn.edu.scau.biubiusuisui.config.FXPlusApplication;
import cn.edu.scau.biubiusuisui.factory.BeanBuilder;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @Author jack * @Author jack
@@ -14,6 +17,12 @@ import javafx.stage.Stage;
public class Demo extends Application { public class Demo extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
FXPlusApplication.start(Demo.class); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
FXPlusApplication.start(Demo.class, new BeanBuilder() {
@Override
public Object getBean(Class type) {
return context.getBean(type);
}
});
} }
} }

View File

@@ -0,0 +1,8 @@
package cn.edu.scau.biubiusuisui.example;
/**
* @Author jack
* @Date:2019/7/7 10:44
*/
public class HelloFXPlusDemo {
}

View File

@@ -0,0 +1,23 @@
package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.FXController;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import javafx.stage.StageStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author jack
* @Date:2019/7/4 11:36
*/
@FXController(path = "Main.fxml")
@Component
@FXWindow(title = "hello",resizable = true,style = StageStyle.UNDECORATED)
public class Main2 extends FXBaseController {
@Autowired
Student student;
}

View File

@@ -1,7 +1,6 @@
package cn.edu.scau.biubiusuisui.example; package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.FXController; import cn.edu.scau.biubiusuisui.annotation.*;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController; import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext; import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import cn.edu.scau.biubiusuisui.factory.FXEntityFactory; import cn.edu.scau.biubiusuisui.factory.FXEntityFactory;
@@ -9,6 +8,7 @@ import javafx.beans.property.Property;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
@@ -20,7 +20,7 @@ import java.util.ResourceBundle;
* @Date:2019/6/25 1:47 * @Date:2019/6/25 1:47
*/ */
@FXController(path = "Main.fxml") @FXController(path = "Main.fxml")
@FXWindow(title = "demo1", draggable = true, resizable = true,style = StageStyle.UNDECORATED) //@FXWindow(title = "demo1", draggable = true, resizable = true,style = StageStyle.UNDECORATED)
public class MainController extends FXBaseController{ public class MainController extends FXBaseController{
@FXML @FXML
@@ -38,9 +38,14 @@ public class MainController extends FXBaseController{
@FXML @FXML
private ListView<String> list; private ListView<String> list;
@FXML
@FXBind(bind = {"text=${student.name}"})
Label label;
Student student; Student student;
@FXML @FXML
@FXSender
void addWord(ActionEvent event) { void addWord(ActionEvent event) {
student.addList("hello" ); student.addList("hello" );
} }
@@ -50,6 +55,12 @@ public class MainController extends FXBaseController{
student.delList("hello"); student.delList("hello");
} }
@FXReceiver(name = "MainController#haha:addWord")
void test2(){
System.out.println("?--?");
}
@Override @Override
public void initialize() { public void initialize() {
student = (Student) FXEntityFactory.createJavaBeanProxy(Student.class); student = (Student) FXEntityFactory.createJavaBeanProxy(Student.class);

View File

@@ -2,6 +2,7 @@ package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.FXEntity; import cn.edu.scau.biubiusuisui.annotation.FXEntity;
import cn.edu.scau.biubiusuisui.annotation.FXField; import cn.edu.scau.biubiusuisui.annotation.FXField;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -11,6 +12,7 @@ import java.util.List;
*/ */
@FXEntity @FXEntity
@Component
public class Student { public class Student {
@FXField @FXField
@@ -63,4 +65,5 @@ public class Student {
public void delList(String word){ public void delList(String word){
list.remove(word); list.remove(word);
} }
} }

View File

@@ -0,0 +1,50 @@
package cn.edu.scau.biubiusuisui.example.springDemo;
import cn.edu.scau.biubiusuisui.annotation.FXController;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import cn.edu.scau.biubiusuisui.example.Student;
import cn.edu.scau.biubiusuisui.factory.FXEntityFactory;
import cn.edu.scau.biubiusuisui.factory.FXFactory;
import javafx.beans.property.Property;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.stage.StageStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author jack
* @Date:2019/7/4 11:36
*/
@FXController(path = "springDemo.fxml")
@Component
@FXWindow(title = "hello",resizable = true,style = StageStyle.UNDECORATED)
public class SpringController extends FXBaseController {
@Autowired
Student student;
Student studentProxy;
@FXML
Label label;
int count = 1;
@Override
public void initialize() {
studentProxy = (Student) FXEntityFactory.createJavaBeanProxy(student);
Property property = FXPlusContext.getEntityPropertyByName(studentProxy, "name");
label.textProperty().bind(property);
}
@FXML
public void add(){
studentProxy.setName("Jack : " + count);
count++;
}
}

View File

@@ -0,0 +1,29 @@
package cn.edu.scau.biubiusuisui.example.springDemo;
import cn.edu.scau.biubiusuisui.annotation.FXScan;
import cn.edu.scau.biubiusuisui.config.FXPlusApplication;
import cn.edu.scau.biubiusuisui.example.Demo;
import cn.edu.scau.biubiusuisui.factory.BeanBuilder;
import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author jack
* @Date:2019/7/7 10:43
*/
@FXScan(base = {"cn.edu.scau.biubiusuisui.example.springDemo"})
public class SpringDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
FXPlusApplication.start(SpringDemo.class, new BeanBuilder() {
@Override
public Object getBean(Class type) {
System.out.println(type);
return context.getBean(type);
}
});
}
}

View File

@@ -0,0 +1,9 @@
package cn.edu.scau.biubiusuisui.factory;
/**
* @Author jack
* @Date:2019/7/4 11:16
*/
public interface BeanBuilder {
Object getBean(Class type);
}

View File

@@ -0,0 +1,20 @@
package cn.edu.scau.biubiusuisui.factory;
/**
* @Author jack
* @Date:2019/7/4 11:13
*/
public class FXBuilder implements BeanBuilder{
@Override
public Object getBean(Class type) {
Object object = null;
try {
object = type.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return object;
}
}

View File

@@ -23,15 +23,16 @@ public class FXEntityFactory {
private FXEntityFactory(){} private FXEntityFactory(){}
public static Object createJavaBeanProxy(Class clazz){ public static Object createJavaBeanProxy(Class clazz){
Object object = null; return createJavaBeanProxy(clazz, new FXBuilder());
try {
object = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} }
public static Object createJavaBeanProxy(Class clazz,BeanBuilder beanBuilder) {
Object object = null;
object = beanBuilder.getBean(clazz);
if(object !=null){
return createJavaBeanProxy(object); return createJavaBeanProxy(object);
}else {
return null;
}
} }
public static Object createJavaBeanProxy(Object object){ public static Object createJavaBeanProxy(Object object){

View File

@@ -5,9 +5,13 @@ import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.config.FXMLLoaderPlus; import cn.edu.scau.biubiusuisui.config.FXMLLoaderPlus;
import cn.edu.scau.biubiusuisui.entity.FXBaseController; import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext; import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import cn.edu.scau.biubiusuisui.example.Student;
import cn.edu.scau.biubiusuisui.function.FXExpressionParser;
import cn.edu.scau.biubiusuisui.function.FXWindowParser; import cn.edu.scau.biubiusuisui.function.FXWindowParser;
import cn.edu.scau.biubiusuisui.messageQueue.MessageQueue; import cn.edu.scau.biubiusuisui.messageQueue.MessageQueue;
import cn.edu.scau.biubiusuisui.proxy.classProxy.FXControllerProxy; import cn.edu.scau.biubiusuisui.proxy.classProxy.FXControllerProxy;
import com.sun.javafx.fxml.BeanAdapter;
import javafx.collections.ObservableMap;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.stage.Stage; import javafx.stage.Stage;
@@ -15,6 +19,7 @@ import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.net.URL; import java.net.URL;
import java.util.Map;
/** /**
* @Author jack * @Author jack
@@ -22,89 +27,106 @@ import java.net.URL;
*/ */
public class FXFactory { public class FXFactory {
private static final BeanBuilder BEAN_BUILDER = new FXBuilder();
private static FXWindowParser windowAnnotationParser = new FXWindowParser(); private static FXWindowParser windowAnnotationParser = new FXWindowParser();
private static FXExpressionParser expressionParser = new FXExpressionParser();
private FXFactory() { }
public static void loadFXController(Class clazz) { private FXFactory() {
getFXController(clazz, ""); }
public static void loadFXController(Class clazz, BeanBuilder beanBuilder) {
if (clazz.getDeclaredAnnotation(FXWindow.class) != null) {
getFXController(clazz, null, beanBuilder);
}
} }
public static FXBaseController getFXController(Class clazz) { public static FXBaseController getFXController(Class clazz) {
FXBaseController fxBaseController = getFXController(clazz, ""); return getFXController(clazz, BEAN_BUILDER);
}
public static FXBaseController getFXController(Class clazz, BeanBuilder beanBuilder) {
FXBaseController fxBaseController = getFXController(clazz, null, beanBuilder);
return fxBaseController; return fxBaseController;
} }
public static FXBaseController getFXController(Class clazz, String controllerName) { public static FXBaseController getFXController(Class clazz, String controllerName) {
FXBaseController fxBaseController = getFxBaseController(clazz, controllerName); return getFXController(clazz, controllerName, BEAN_BUILDER);
}
public static FXBaseController getFXController(Class clazz, String controllerName, BeanBuilder beanBuilder) {
FXBaseController fxBaseController = getFxBaseController(clazz, controllerName, beanBuilder);
return fxBaseController; return fxBaseController;
} }
private static void register(FXBaseController fxBaseController, FXBaseController fxBaseControllerProxy) { private static void register(FXBaseController fxBaseController, FXBaseController fxBaseControllerProxy) {
FXPlusContext.addController(fxBaseController); FXPlusContext.addController(fxBaseController); //保存
MessageQueue.getInstance().registerCosumer(fxBaseController, fxBaseControllerProxy); MessageQueue.getInstance().registerCosumer(fxBaseController, fxBaseControllerProxy); // 添加进入消息队列 信号功能
}
private static FXBaseController getFxBaseController(Class clazz, String controllerName) {
return null;
} }
/** /**
*
* @param clazz instance that extends by FXBaseController * @param clazz instance that extends by FXBaseController
* @param controllerName * @param controllerName
* @return * @return
*/ */
private static FXBaseController getFxBaseController(Class clazz, String controllerName) {
Annotation[] annotations = clazz.getAnnotations(); private static FXBaseController getFxBaseController(Class clazz, String controllerName, BeanBuilder beanBuilder) {
Boolean isController = false; Boolean isController = false;
URL fxmlPath; URL fxmlPath;
Boolean isWindow = false; Boolean isWindow = false;
FXWindow fxWindow = null; FXWindow fxWindow = null;
FXController fxController = null; FXController fxController = null;
//reflect and get FXController cn.edu.scau.biubiusuisui.annotation //reflect and get FXController cn.edu.scau.biubiusuisui.annotation
for (Annotation annotation : annotations) { fxController = (FXController) clazz.getDeclaredAnnotation(FXController.class);
if (annotation.annotationType().equals(FXController.class)) { fxWindow = (FXWindow) clazz.getDeclaredAnnotation(FXWindow.class);
fxController = (FXController) annotation;
isController = true;
} else if (annotation.annotationType().equals(FXWindow.class)) {
fxWindow = (FXWindow) annotation;
isWindow = true;
}
}
Pane parent = null; Pane parent = null;
FXBaseController fxControllerProxy = null; FXBaseController fxControllerProxy = null;
FXBaseController fxBaseController = null; FXBaseController fxBaseController = null;
if (isController) { if (fxController != null) {
String name = fxController.path(); String name = fxController.path();
fxmlPath = clazz.getClassLoader().getResource(name); fxmlPath = clazz.getClassLoader().getResource(name);
FXMLLoaderPlus fxmlLoader = new FXMLLoaderPlus(fxmlPath); FXMLLoaderPlus fxmlLoader = new FXMLLoaderPlus(fxmlPath);
try {
// create a cn.edu.scau.biubiusuisui.proxy for monitoring methods // create a cn.edu.scau.biubiusuisui.proxy for monitoring methods
fxBaseController = (FXBaseController) clazz.newInstance(); fxBaseController = (FXBaseController) beanBuilder.getBean(clazz);
if (fxBaseController != null) {
FXControllerProxy controllerProxy = new FXControllerProxy(); FXControllerProxy controllerProxy = new FXControllerProxy();
fxControllerProxy = (FXBaseController) controllerProxy.getInstance(fxBaseController); fxControllerProxy = (FXBaseController) controllerProxy.getInstance(fxBaseController);
fxControllerProxy.setName(controllerName);
fxBaseController.setName(controllerName);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
fxmlLoader.setRoot(fxControllerProxy); fxmlLoader.setRoot(fxControllerProxy);
fxmlLoader.setController(fxControllerProxy); fxmlLoader.setController(fxControllerProxy);
try { try {
parent = fxmlLoader.load(); parent = fxmlLoader.load();
if (controllerName != null) {
fxControllerProxy.setName(controllerName);
fxBaseController.setName(controllerName);
} else {
fxBaseController.setName(parent.getId());
}
register(fxBaseController, fxControllerProxy);
} catch (IOException exception) { } catch (IOException exception) {
throw new RuntimeException(exception); throw new RuntimeException(exception);
} }
}
} else { } else {
return null; return null;
} }
//register //register
register(fxBaseController, fxControllerProxy);
if (isWindow) { if (fxWindow != null) {
createWindow(fxWindow, fxControllerProxy, fxBaseController);
}
return fxControllerProxy;
}
private static void createWindow(FXWindow fxWindow, FXBaseController fxControllerProxy, FXBaseController fxBaseController) {
Stage stage = new Stage(); Stage stage = new Stage();
fxControllerProxy.setStage(stage); fxControllerProxy.setStage(stage);
fxBaseController.setStage(stage); fxBaseController.setStage(stage);
@@ -115,7 +137,9 @@ public class FXFactory {
windowAnnotationParser.parse(stage, fxControllerProxy, fxWindow); windowAnnotationParser.parse(stage, fxControllerProxy, fxWindow);
stage.show(); stage.show();
} }
return fxControllerProxy;
private static void parseControllerBindExpression(FXBaseController fxBaseController, ObservableMap<String, Object> namespaces) {
expressionParser.parse(fxBaseController, namespaces);
} }
} }

View File

@@ -0,0 +1,56 @@
package cn.edu.scau.biubiusuisui.function;
import cn.edu.scau.biubiusuisui.annotation.FXBind;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import com.sun.javafx.fxml.BeanAdapter;
import com.sun.javafx.fxml.expression.Expression;
import com.sun.javafx.fxml.expression.ExpressionValue;
import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableMap;
import java.lang.reflect.Field;
import java.util.Map;
/**
* @Author jack
* @Date:2019/7/4 13:55
*/
public class FXExpressionParser {
private static final String BIND_EXPRESSION_PREFIX = "${";
private static final String BIND_EXPRESSION_SUFIX = "}";
public static void parse(FXBaseController fxBaseController, ObservableMap<String, Object> namespaces) {
// Class clazz = fxBaseController.getClass();
// Field[] fields = clazz.getDeclaredFields();
// for (Field field : fields) {
// FXBind fxBind = field.getDeclaredAnnotation(FXBind.class);
// if (fxBind != null) {
// String[] names = fxBind.bind();
// for (String propertyName : names) {
// String args[] = propertyName.split("=");
// String name = args[0];
// String value = args[1];
// if (value.startsWith(BIND_EXPRESSION_PREFIX) && value.endsWith(BIND_EXPRESSION_SUFIX)) {
// value = value.substring(BIND_EXPRESSION_PREFIX.length(), value.length() - 1);
// Object object = null;
// try {
// field.setAccessible(true);
// object = field.get(fxBaseController);
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
//
// BeanAdapter targetAdapter = new BeanAdapter(object);
// ObservableValue<Object> propertyModel = targetAdapter.getPropertyModel(name);
// Class<?> type = targetAdapter.getType(name);
// if (propertyModel instanceof Property<?>) {
// ((Property<Object>) propertyModel).bind(FXPlusContext.getEntityPropertyByName(fxBaseController,));
// }
// }
// }
// }
}
}

View File

@@ -58,6 +58,8 @@ public class MessageQueue {
if (lists != null) { if (lists != null) {
for (FXMethodEntity fxMethodEntity : lists) { for (FXMethodEntity fxMethodEntity : lists) {
Method method = fxMethodEntity.getMethod(); Method method = fxMethodEntity.getMethod();
method.setAccessible(true);
FXBaseController fxBaseController = fxMethodEntity.getFxBaseController(); FXBaseController fxBaseController = fxMethodEntity.getFxBaseController();
if (method.getParameterCount() == 0) { if (method.getParameterCount() == 0) {
try { try {

View File

@@ -8,6 +8,7 @@ import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.MethodProxy;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/** /**
@@ -23,18 +24,23 @@ public class FXControllerProxy implements MethodInterceptor {
FXBaseController target; FXBaseController target;
public Object getInstance(FXBaseController target) { public Object getInstance(FXBaseController target) {
this.target = target; this.target = target;
Enhancer enhancer = new Enhancer(); Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.target.getClass()); enhancer.setSuperclass(this.target.getClass());
enhancer.setCallback(this); enhancer.setCallback(this);
return enhancer.create(); Object proxy = enhancer.create();
inject(target, proxy);
return proxy;
} }
@Override @Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
Object o1 = methodProxy.invokeSuper(o, objects); Object o1 = methodProxy.invokeSuper(o, objects);
Annotation []annotations = method.getDeclaredAnnotations(); Annotation []annotations = method.getDeclaredAnnotations();
// System.out.println("name" +target.getName());
for(Annotation annotation : annotations){ for(Annotation annotation : annotations){
if(FXSender.class.equals(annotation.annotationType())){ if(FXSender.class.equals(annotation.annotationType())){
FXSender fxSender = (FXSender)annotation; FXSender fxSender = (FXSender)annotation;
@@ -44,10 +50,24 @@ public class FXControllerProxy implements MethodInterceptor {
}else{ }else{
name += fxSender.name(); name += fxSender.name();
} }
System.out.println("method" + name);
MessageQueue.getInstance().sendMsg(name,o1); MessageQueue.getInstance().sendMsg(name,o1);
} }
} }
return o1; return o1;
} }
private void inject(Object target,Object proxy){
Field[] fields = target.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object filedValue = field.get(target);
System.out.println(filedValue);
field.set(proxy,filedValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
} }

View File

@@ -41,8 +41,8 @@ public class FXEntityProxy implements MethodInterceptor {
*/ */
@Override @Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
Object o1 = methodProxy.invokeSuper(o, objects);
Object o1 = methodProxy.invokeSuper(o, objects);
String methodName = method.getName(); String methodName = method.getName();
String fieldName = null; String fieldName = null;
if (methodName.length() >= 3) { if (methodName.length() >= 3) {
@@ -55,7 +55,7 @@ public class FXEntityProxy implements MethodInterceptor {
if(fxFieldPropertyMapping == null || property == null){ if(fxFieldPropertyMapping == null || property == null){
return o1; return o1;
} }
System.out.println("??? <--->");
Class type = fxFieldPropertyMapping.getType(); Class type = fxFieldPropertyMapping.getType();
if (methodName.startsWith("set")) { if (methodName.startsWith("set")) {
if(Boolean.class.equals(type)){ if(Boolean.class.equals(type)){

View File

@@ -1,13 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?> <?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<fx:root prefHeight="400.0" prefWidth="600.0" type="Pane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.edu.scau.biubiusuisui.example.MainController"> <fx:root id="haha" prefHeight="403.0" prefWidth="625.0" type="Pane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.edu.scau.biubiusuisui.example.MainController">
<children> <children>
<Button fx:id="addBtn" layoutX="432.0" layoutY="83.0" mnemonicParsing="false" onAction="#addWord" text="add" /> <Label fx:id="label" layoutX="18.0" layoutY="166.0" prefHeight="68.0" prefWidth="304.0" text="${input.text}">
<Button fx:id="delBtn" layoutX="432.0" layoutY="151.0" mnemonicParsing="false" onAction="#delWord" text="del" /> <font>
<ListView fx:id="list" layoutX="42.0" layoutY="51.0" prefHeight="275.0" prefWidth="334.0" /> <Font size="20.0" />
</font></Label>
<TextField fx:id="input" layoutX="316.0" layoutY="185.0" prefHeight="30.0" prefWidth="277.0" />
</children> </children>
</fx:root> </fx:root>

View File

@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import cn.edu.scau.biubiusuisui.example.MainController?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?> <?import javafx.scene.layout.Pane?>
<fx:root prefHeight="400.0" prefWidth="600.0" type="Pane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.edu.scau.biubiusuisui.example.MainController"> <fx:root prefHeight="709.0" prefWidth="995.0" type="Pane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.edu.scau.biubiusuisui.example.Main2">
<children> <children>
<Button fx:id="btn" layoutX="266.0" layoutY="179.0" mnemonicParsing="false" text="Button" /> <Button fx:id="btn" layoutX="793.0" layoutY="146.0" mnemonicParsing="false" text="Button" />
<MainController layoutX="85.0" layoutY="123.0" />
</children> </children>
</fx:root> </fx:root>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <bean id="..." class="...">
collaborators and configuration for this bean go here
</bean>
<bean id="..." class="...">
collaborators and configuration for this bean go here
</bean> -->
<!-- more bean definitions go here
UserDao ud = new UserDao();
-->
<context:component-scan base-package="cn.edu.scau.biubiusuisui"/>
</beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.Button?>
<fx:root id="haha" prefHeight="403.0" prefWidth="625.0" type="Pane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.edu.scau.biubiusuisui.example.springDemo.SpringController">
<children>
<Label fx:id="label" layoutX="18.0" layoutY="166.0" prefHeight="68.0" prefWidth="304.0" text=""></Label>
<Button onAction="#add"></Button>
</children>
</fx:root>