|
| 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.beans.BeansException; |
| 18 | +import org.springframework.boot.SpringApplication; |
| 19 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 20 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 21 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 22 | +import org.springframework.context.ApplicationContext; |
| 23 | +import org.springframework.context.ApplicationContextAware; |
| 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 | +/**SpringBootApplication |
| 37 | + * 右键这个类 > Run As > Java Application |
| 38 | + * @author Lemon |
| 39 | + */ |
| 40 | +@Configuration |
| 41 | +@SpringBootApplication |
| 42 | +@EnableAutoConfiguration |
| 43 | +@EnableConfigurationProperties |
| 44 | +public class DemoApplication implements ApplicationContextAware { |
| 45 | + |
| 46 | + static { |
| 47 | + APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() { |
| 48 | + @Override |
| 49 | + public SQLConfig createSQLConfig() { |
| 50 | + return new DemoSQLConfig(); |
| 51 | + } |
| 52 | + @Override |
| 53 | + public SQLExecutor createSQLExecutor() { |
| 54 | + return new DemoSQLExecutor(); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增 |
| 59 | + // try { //加载驱动程序 |
| 60 | + // Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 61 | + // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 62 | + // Log.d(TAG, "成功加载 SQLServer 驱动��>>>>>>>>>>>>>>>>>>>>> "); |
| 63 | + // } |
| 64 | + // catch (ClassNotFoundException e) { |
| 65 | + // e.printStackTrace(); |
| 66 | + // Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!"); |
| 67 | + // } |
| 68 | + // |
| 69 | + // try { //加载驱动程序 |
| 70 | + // Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 71 | + // Class.forName("oracle.jdbc.driver.OracleDriver"); |
| 72 | + // Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 73 | + // } |
| 74 | + // catch (ClassNotFoundException e) { |
| 75 | + // e.printStackTrace(); |
| 76 | + // Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!"); |
| 77 | + // } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) throws Exception { |
| 82 | + SpringApplication.run(DemoApplication.class, args); |
| 83 | + |
| 84 | + Log.DEBUG = true; |
| 85 | + APIJSONApplication.init(false); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效 |
| 86 | + } |
| 87 | + |
| 88 | + // 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例 |
| 89 | + private static ApplicationContext APPLICATION_CONTEXT; |
| 90 | + public static ApplicationContext getApplicationContext() { |
| 91 | + return APPLICATION_CONTEXT; |
| 92 | + } |
| 93 | + @Override |
| 94 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 95 | + APPLICATION_CONTEXT = applicationContext; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + // 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 100 | + |
| 101 | + @Bean |
| 102 | + public WebMvcConfigurer corsConfigurer() { |
| 103 | + return new WebMvcConfigurer() { |
| 104 | + @Override |
| 105 | + public void addCorsMappings(CorsRegistry registry) { |
| 106 | + registry.addMapping("/**") |
| 107 | + .allowedOriginPatterns("*") |
| 108 | + .allowedMethods("*") |
| 109 | + .allowCredentials(true) |
| 110 | + .maxAge(3600); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + // 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 116 | + |
| 117 | +} |
0 commit comments