Spare2Application.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.jiayue.ipfcst.spare2;
  2. import io.netty.util.ResourceLeakDetector;
  3. import lombok.extern.slf4j.Slf4j;
  4. import net.sf.ehcache.CacheManager;
  5. import org.apache.velocity.app.Velocity;
  6. import org.apache.velocity.app.VelocityEngine;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.autoconfigure.SpringBootApplication;
  9. import org.springframework.boot.builder.SpringApplicationBuilder;
  10. import org.springframework.cache.annotation.EnableCaching;
  11. import org.springframework.cache.ehcache.EhCacheCacheManager;
  12. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.core.env.Environment;
  16. import org.springframework.core.io.ClassPathResource;
  17. import org.springframework.scheduling.annotation.EnableScheduling;
  18. import java.util.Properties;
  19. import java.util.TimeZone;
  20. /**
  21. * 启动类
  22. *
  23. * @author zzy
  24. * @version 1.0
  25. * @since 2019/7/22 9:05
  26. */
  27. @Configuration
  28. @SpringBootApplication
  29. @Slf4j
  30. @EnableScheduling
  31. @EnableCaching
  32. public class Spare2Application {
  33. private final Environment environment;
  34. @Autowired
  35. public Spare2Application(Environment environment) {
  36. this.environment = environment;
  37. }
  38. public static void main(String[] args) {
  39. TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
  40. ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
  41. //变更配置文件读取位置启动
  42. new SpringApplicationBuilder(Spare2Application.class).run(args);
  43. }
  44. /**
  45. * ecache缓存对象获取
  46. */
  47. @Bean
  48. public EhCacheCacheManager cacheManager(CacheManager cacheManagerFactory) {
  49. EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
  50. ehCacheCacheManager.setCacheManager(cacheManagerFactory);
  51. return ehCacheCacheManager;
  52. }
  53. /**
  54. * ecache缓存配置加载
  55. */
  56. @Bean
  57. public EhCacheManagerFactoryBean cacheManagerFactory() {
  58. EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
  59. ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache-setting.xml"));
  60. return ehCacheManagerFactoryBean;
  61. }
  62. /**
  63. * 上报文件模板引擎
  64. *
  65. * @return 返回模板引擎
  66. */
  67. @Bean
  68. public VelocityEngine velocityEngine() {
  69. VelocityEngine ve = new VelocityEngine();
  70. Properties properties = new Properties();
  71. //设置velocity资源加载方式为file
  72. properties.setProperty("resource.loader", "file");
  73. //设置velocity资源加载方式为file时的处理类
  74. properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
  75. properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
  76. properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
  77. properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
  78. properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
  79. ve.init(properties);
  80. return ve;
  81. }
  82. }