package com.xianmao;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.unionpay.acp.sdk.SDKConfig;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import com.qiniu.api.config.Config;
import com.xianmao.base.autoreloadconfig.ConfigHolder;
import com.xianmao.base.autoreloadconfig.ReloadableConfig;
import com.xianmao.base.security.AuthenticationTokenProcessingFilter;
import com.xianmao.base.security.UnauthorizedEntryPoint;
import com.xianmao.base.security.WebApiCustomFilter;
import com.xianmao.base.utilities.FileUtils;
import com.xianmao.common.beans.SolrConfiguration;
import com.xianmao.common.qiniu.QiniuFileUtils;
import com.xianmao.common.umeng.UmengConfig;
import com.xianmao.event.service.HxRegisterListener;
@Configuration
@ComponentScan
@EnableAutoConfiguration
//标注启动了缓存
@EnableCaching
@EnableAsync
@EnableScheduling
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static boolean TASK_ENABLE = false;
public static boolean SMS_ENABLE = false;
public static final String DEV = "dev";
public static final String DEVBACK = "devbackup";
public static final String PROD = "prod";
public static final String PRODBACK = "prodbackup";
public static String ENVIRONMENT = DEV;
@Value("${spring.profiles.active}")
public void setENVIRONMENT(String e){
Application.ENVIRONMENT = e;
}
public static final int APP_TYPE_BUYER = 0;
public static final int APP_TYPE_SELLER = 1;
public static final int APP_TYPE_ALL = 2; //广播,所有APP推送
@Bean
public ThreadPoolTaskScheduler taskScheduler()
{
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(20);
scheduler.setThreadNamePrefix("schedule-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
@Bean
public AuthenticationTokenProcessingFilter authenticationTokenProcessingFilter() {
return new AuthenticationTokenProcessingFilter();
}
@Bean
public WebApiCustomFilter webApiCustomFilter() {
return new WebApiCustomFilter();
}
@Bean
public UnauthorizedEntryPoint unauthorizedEntryPoint() {
return new UnauthorizedEntryPoint();
}
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("20480KB");
factory.setMaxRequestSize("20480KB");
return factory.createMultipartConfig();
}
@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
/**
* 银联参数初始化
* 如果是在web应用开发里,这个方写在可使用监听的方式写入缓存,无须在这出现
*/
SDKConfig.getConfig().loadPropertiesFromSrc();// 从classpath加载acp_sdk.properties文件
ConfigHolder<ReloadableConfig> config = ReloadableConfig.HOLDER;
if (config != null && config.getConfig() != null) {
return;
}
throw new RuntimeException("载入配置文件出错");
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("ServletContext destroyed," + sce.getServletContext());
}
};
}
public static void main(String[] args) {
logger.info("================ application start begin ......");
SpringApplication.run(Application.class);
print();
logger.info("================ application start done !");
}
public static void print(){
logger.info(UmengConfig.PRODUCTION_MODE);
logger.info(UmengConfig.DESCRIPTION);
logger.info(HxRegisterListener.PREFIX);
logger.info(QiniuFileUtils.IMAGE_URL_PREFIX);
logger.info(Config.ACCESS_KEY);
logger.info(Config.SECRET_KEY);
logger.info(SolrConfiguration.DEFAULT_URL);
logger.info(SolrConfiguration.USER_URL);
logger.info("task_enable:" + Application.TASK_ENABLE);
logger.info("sms_enable:" + Application.SMS_ENABLE);
logger.info("environment:" + Application.ENVIRONMENT);
logger.info("fileUploadSaveDir:" + FileUtils.SAVEPATH);
}
}
1.@Configuration:springboot推荐使用用java代码的形式申明注册bean。@Configuration注解可以用java代码的形式实现spring中xml配置文件配置的效果。
2.@ComponentScan:它做的事情就是告诉Spring从哪里找到bean,由你来定义哪些包需要被扫描。一旦你指定了,Spring将会将在被指定的包及其下级的包(sub packages)中寻找bean。
3.@EnableAutoConfiguration:这个注释告诉Spring Boot根据你添加的jar依赖关系“猜测”你将如何配置Spring(Spring Boot自动配置尝试根据您添加的jar依赖项自动配置Spring应用程序)。例如你在pom.xml引入了spring-boot-starter-web,Spring将自动引入添加Tomcat和Spring MVC相关jar依赖,因此自动配置将假设您正在开发Web应用程序并相应地设置了默认的Spring环境。
4.@EnableCaching:完成简单的缓存功能。
5.@EnableAsync:开始对异步任务的支持,从spring boot启动类中获取IOC中的bean,在启动类上标注@EnableAsync注解,启动@Async异步注解。或者在启动类中只标注@SpringBootApplication注解,因为该注解中已经包含了上面两个注解。
6.@EnableScheduling:开启定时任务注解。
7.Spring Boot使用@Async实现异步调用:ThreadPoolTaskScheduler线程池的优雅关闭。
@Bean
public ThreadPoolTaskScheduler taskScheduler()
{
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(20);
scheduler.setThreadNamePrefix("schedule-");
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
setWaitForTasksToCompleteOnShutdown(true)该方法就是这里的关键,用来设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean,这样这些异步任务的销毁就会先于Redis线程池的销毁。同时,这里还设置了setAwaitTerminationSeconds(60),该方法用来设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
8.身份验证令牌处理筛选器,用于验证token有效性。
@Bean
public AuthenticationTokenProcessingFilter authenticationTokenProcessingFilter() {
return new AuthenticationTokenProcessingFilter();
}
9.Web API自定义筛选器
@Bean
public WebApiCustomFilter webApiCustomFilter() {
return new WebApiCustomFilter();
}
10.未经授权的进入点
@Bean
public UnauthorizedEntryPoint unauthorizedEntryPoint() {
return new UnauthorizedEntryPoint();
}
11.多参数配置,setMaxFileSize(“20480KB”)表示设置上载文件允许的最大大小为20M,setMaxRequestSize(“20480KB”)表示设置多部分/表单数据请求允许的最大大小为20M。
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("20480KB");
factory.setMaxRequestSize("20480KB");
return factory.createMultipartConfig();
}
12.ServletContextListener:是servlet容器中的一个API接口, 它用来监听ServletContext的生命周期,也就是相当于用来监听Web应用的生命周期。
protected ServletContextListener listener() {
return new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
/**
* 银联参数初始化
* 如果是在web应用开发里,这个方写在可使用监听的方式写入缓存,无须在这出现
*/
SDKConfig.getConfig().loadPropertiesFromSrc();// 从classpath加载acp_sdk.properties文件
ConfigHolder<ReloadableConfig> config = ReloadableConfig.HOLDER;
if (config != null && config.getConfig() != null) {
return;
}
throw new RuntimeException("载入配置文件出错");
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("ServletContext destroyed," + sce.getServletContext());
}
};
}