|
| 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.framework.APIJSONApplication; |
| 31 | +import apijson.framework.APIJSONCreator; |
| 32 | +import apijson.orm.SQLConfig; |
| 33 | +import apijson.orm.SQLExecutor; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Demo SpringBoot Application 主应用程序启动类 |
| 38 | + * 右键这个类 > Run As > Java Application |
| 39 | + * 具体见 SpringBoot 文档 |
| 40 | + * https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class |
| 41 | + * |
| 42 | + * @author Lemon |
| 43 | + */ |
| 44 | +@Configuration |
| 45 | +@SpringBootApplication |
| 46 | +@EnableAutoConfiguration |
| 47 | +@EnableConfigurationProperties |
| 48 | +public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { |
| 49 | + public static final String TAG = "DemoApplication"; |
| 50 | + |
| 51 | + // 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例 |
| 52 | + private static ApplicationContext APPLICATION_CONTEXT; |
| 53 | + public static ApplicationContext getApplicationContext() { |
| 54 | + return APPLICATION_CONTEXT; |
| 55 | + } |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + APPLICATION_CONTEXT = SpringApplication.run(DemoApplication.class, args); |
| 59 | + |
| 60 | + Log.DEBUG = true; |
| 61 | + APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效 |
| 62 | + } |
| 63 | + |
| 64 | + // SpringBoot 2.x 自定义端口方式 |
| 65 | + @Override |
| 66 | + public void customize(ConfigurableServletWebServerFactory server) { |
| 67 | + server.setPort(8080); |
| 68 | + } |
| 69 | + |
| 70 | + // 支持 APIAuto 中 JavaScript 代码跨域请求 |
| 71 | + @Bean |
| 72 | + public WebMvcConfigurer corsConfigurer() { |
| 73 | + return new WebMvcConfigurer() { |
| 74 | + @Override |
| 75 | + public void addCorsMappings(CorsRegistry registry) { |
| 76 | + registry.addMapping("/**") |
| 77 | + .allowedOriginPatterns("*") |
| 78 | + .allowedMethods("*") |
| 79 | + .allowCredentials(true) |
| 80 | + .maxAge(3600); |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + static { |
| 86 | + // 使用本项目的自定义处理类 |
| 87 | + APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() { |
| 88 | + @Override |
| 89 | + public SQLConfig createSQLConfig() { |
| 90 | + return new DemoSQLConfig(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public SQLExecutor createSQLExecutor() { |
| 95 | + return new DemoSQLExecutor(); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + try { //加载驱动程序 |
| 100 | + Log.d(TAG, "尝试加载 SQLite 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 101 | + Class.forName("org.sqlite.SQLiteJDBCLoader"); |
| 102 | + Log.d(TAG, "成功加载 SQLite 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 103 | + } |
| 104 | + catch (ClassNotFoundException e) { |
| 105 | + e.printStackTrace(); |
| 106 | + Log.e(TAG, "加载 SQLite 驱动失败,请检查 pom.xml 中 sqlite-jdbc 版本是否存在以及可用 !!!"); |
| 107 | + } |
| 108 | + |
| 109 | + try { //加载驱动程序 |
| 110 | + Log.d(TAG, "尝试加载 ShardingSphere 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 111 | + Class.forName("org.apache.shardingsphere.driver.ShardingSphereDriver"); |
| 112 | + Log.d(TAG, "成功加载 ShardingSphere 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 113 | + } |
| 114 | + catch (ClassNotFoundException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + Log.e(TAG, "加载 ShardingSphere 驱动失败,请检查 pom.xml 中 ShardingSphereDriver 版本是否存在以及可用 !!!"); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments