|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.demo; |
| 16 | + |
| 17 | +import org.springframework.boot.SpringApplication; |
| 18 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 19 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 20 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 21 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
| 22 | +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; |
| 23 | +import org.springframework.context.ApplicationContext; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| 27 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 28 | + |
| 29 | +import apijson.Log; |
| 30 | +import apijson.demo.script.GraalJavaScriptExecutor; |
| 31 | +import apijson.demo.script.LuaScriptExecutor; |
| 32 | +import apijson.demo.script.NashornScriptExecutor; |
| 33 | +import apijson.framework.APIJSONApplication; |
| 34 | +import apijson.framework.APIJSONCreator; |
| 35 | +import apijson.orm.SQLConfig; |
| 36 | +import apijson.orm.SQLExecutor; |
| 37 | +import apijson.orm.script.ScriptExecutor; |
| 38 | + |
| 39 | +/** |
| 40 | + * Demo SpringBoot Application 主应用程序启动类 右键这个类 > Run As > Java Application 具体见 |
| 41 | + * SpringBoot 文档 |
| 42 | + * https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class |
| 43 | + * |
| 44 | + * @author Lemon |
| 45 | + */ |
| 46 | +@Configuration |
| 47 | +@SpringBootApplication |
| 48 | +@EnableAutoConfiguration |
| 49 | +@EnableConfigurationProperties |
| 50 | +public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { |
| 51 | + |
| 52 | + // 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例 |
| 53 | + private static ApplicationContext APPLICATION_CONTEXT; |
| 54 | + |
| 55 | + public static ApplicationContext getApplicationContext() { |
| 56 | + return APPLICATION_CONTEXT; |
| 57 | + } |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + APPLICATION_CONTEXT = SpringApplication.run(DemoApplication.class, args); |
| 61 | + |
| 62 | + Log.DEBUG = true; |
| 63 | + // 加载扩展脚本执行器 |
| 64 | + extendScriptExecutor(); |
| 65 | + APIJSONApplication.init(true); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效 |
| 66 | + } |
| 67 | + |
| 68 | + public static void extendScriptExecutor() { |
| 69 | + ScriptExecutor javaScriptExecutor = new NashornScriptExecutor(); |
| 70 | + ScriptExecutor luaExecutor = new LuaScriptExecutor(); |
| 71 | + ScriptExecutor GraalJSExecutor = new GraalJavaScriptExecutor(); |
| 72 | + APIJSONApplication.addScriptExecutor("nashornJS", javaScriptExecutor); |
| 73 | + APIJSONApplication.addScriptExecutor("luaj", luaExecutor); |
| 74 | + APIJSONApplication.addScriptExecutor("graalJS", GraalJSExecutor); |
| 75 | + } |
| 76 | + |
| 77 | + // SpringBoot 2.x 自定义端口方式 |
| 78 | + @Override |
| 79 | + public void customize(ConfigurableServletWebServerFactory server) { |
| 80 | + server.setPort(8080); |
| 81 | + } |
| 82 | + |
| 83 | + // 支持 APIAuto 中 JavaScript 代码跨域请求 |
| 84 | + @Bean |
| 85 | + public WebMvcConfigurer corsConfigurer() { |
| 86 | + return new WebMvcConfigurer() { |
| 87 | + @Override |
| 88 | + public void addCorsMappings(CorsRegistry registry) { |
| 89 | + registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("*").allowCredentials(true).maxAge(3600); |
| 90 | + } |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + static { |
| 95 | + // 使用本项目的自定义处理类 |
| 96 | + APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() { |
| 97 | + @Override |
| 98 | + public SQLConfig createSQLConfig() { |
| 99 | + return new DemoSQLConfig(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public SQLExecutor createSQLExecutor() { |
| 104 | + return new DemoSQLExecutor(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增 |
| 109 | + // try { //加载驱动程序 |
| 110 | + // Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 111 | + // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 112 | + // Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 113 | + // } |
| 114 | + // catch (ClassNotFoundException e) { |
| 115 | + // e.printStackTrace(); |
| 116 | + // Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 |
| 117 | + // !!!"); |
| 118 | + // } |
| 119 | + // |
| 120 | + // try { //加载驱动程序 |
| 121 | + // Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 122 | + // Class.forName("oracle.jdbc.driver.OracleDriver"); |
| 123 | + // Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 124 | + // } |
| 125 | + // catch (ClassNotFoundException e) { |
| 126 | + // e.printStackTrace(); |
| 127 | + // Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!"); |
| 128 | + // } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments