一直想优雅地再JavaEE程序中方便地使用集成在spring上的工具,以前的方法是用如下的方式硬编码。
private static ApplicationContext applicationContext; static { String path2 = AppUtils.class.getResource("/applicationContext.xml").toString(); String path1 = AppUtils.class.getResource("/applicationContext-myBatis.xml").toString(); applicationContext = new ClassPathXmlApplicationContext(path1,path2); } public staticT beanFactory(Class clazz) { return applicationContext.getBean(clazz); }
如今发现可以用Springboot项目启动自动触发方法,可以很方便地使用spring工具。
@Component public class InitProject implements ApplicationRunner { private static final Logger LOG = LoggerFactory.getLogger(InitProject.class); @Override public void run(ApplicationArguments args) throws Exception { LOG.info("==========redis data start==========="); } }
示例:Spring-boot整合FastDFS
FastDFS与Springboot集成_技术改变生活-CSDN博客_fastdfs springboot
1.添加pom依赖
com.github.tobato fastdfs-client1.25.2-RELEASE
2、将Fdfs配置引入项目
@import(FdfsClientConfig.class) @SpringBootApplication public class JingtongApplication { public static void main(String[] args) { SpringApplication.run(JingtongApplication.class, args); } }
3.配置文件中加入fdfs相关配置
application.yml
fdfs: soTimeout: 1500 connectTimeout: 600 thumbImage: #缩略图生成参数 width: 150 height: 150 trackerList: #TrackerList参数,支持多个 - 192.168.0.201:22122 - 192.168.0.202:22122
application.properties
fdfs.soTimeout=1500 fdfs.connectTimeout=600 fdfs.thumbImage.width=150 fdfs.thumbImage.height=150 fdfs.trackerList[0]=192.168.0.201:22122 fdfs.trackerList[1]=192.168.0.202:22122
4.简单封装FastDFS使用类
package com.example.components; import com.github.tobato.fastdfs.FdfsClientConfig; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.import; import org.springframework.jmx.support.RegistrationPolicy; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; //解决jmx重复注册bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) @import(FdfsClientConfig.class)//只需要一行注解 @import(FdfsClientConfig.class)就可以拥有带有连接池的FastDFS Java客户端了 @Component public class FastDFSClientWrapper { @Autowired private FastFileStorageClient storageClient; public String uploadFile(File source) throws IOException { FileInputStream is=null; try { is = new FileInputStream(source); StorePath storePath = storageClient.uploadFile(is, source.length(), FilenameUtils.getExtension(source.getName()), null); return getResAccessUrl(storePath); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { if(is!=null){ is.close(); } } return null; } // 封装文件完整URL地址 private String getResAccessUrl(StorePath storePath) { String fileUrl = "192.168.10.183:80" + "/" + storePath.getFullPath(); return fileUrl; } }
5.项目启动自动触发方法
package com.example.components; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import java.io.File; @Component public class InitProject implements ApplicationRunner { @Autowired private FastDFSClientWrapper fastDFSClientWrapper; @Override public void run(ApplicationArguments args) throws Exception { //在这里写数据逻辑 System.out.println("start upload job!!!!!!!!!"); String pathname = "C:\Users\76204\Postman\files\youyin_test1.mp4"; pathname = "D:\test\test.png"; String path = fastDFSClientWrapper.uploadFile(new File(pathname)); System.out.println("path========>>>>>"+path); } }
6.项目入口
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //在启动类下添加扫描路径 @ComponentScan(basePackages = {"com.example.components"}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
7.测试类
package com.example.demo; import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.io.FilenameUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.*; @SpringBootTest class SpringbootApplicationTest { @Autowired private FastFileStorageClient storageClient; @Test public void uploadTest() { InputStream is = null; try { // 获取文件源 // File source = new File("C:\Users204\Postman\files\youyin_test1.mp4"); File source = new File("D:\test\test.png"); // 获取文件流 is = new FileInputStream(source); // 进行文件上传 StorePath storePath = storageClient.uploadFile(is, source.length(), FilenameUtils.getExtension(source.getName()), null); // 获得文件上传后访问地址 String fullPath = storePath.getFullPath(); // 打印访问地址 System.out.println("fullPath = " + fullPath); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(is != null) { // 关闭流资源 is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
扩充:
使用过程中发现了一个很好用的注解@PostConstruct注解
这个注解是由Java提供的,它用来修饰一个非静态的void方法。它会在服务器加载Servlet的时候运行,并且只运行一次。
@Component public class SystemConstant { public static String surroundings; @Value("${spring.profiles.active}") public String environment; @PostConstruct public void initialize() { System.out.println("初始化环境..."); surroundings = this.environment; } }
这个注解还可以方便地将自动装配地类供static工具类使用,例如
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class RedisUtil { private static RedisTemplate
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)