springBoot之配置文件的读取以及过滤器和拦截器的使用

在之前的学习springBoot中,成功的实现了Restful风格的基本服务。但是想将之前的工程作为一个项目来说,那些是仅仅不够的。可能还需要获取自定义的配置以及添加过滤器和拦截器。至于为什么将这些写在一起,只是因为这些比较简单而且也会经常用到,所以干脆就一起写出来了。

读取配置文件

在使用maven项目中,配置文件会放在resources根目录下。
我们的springBoot是用Maven搭建的,所以springBoot的默认配置文件和自定义的配置文件都放在此目录。
springBoot的 默认配置文件为 application.propertiesapplication.yml,这里我们使用 application.properties

首先在application.properties中添加我们要读取的数据。
springBoot支持分层结构。
例如:

web: pancm: title: SpringBoot description: test

注意前面的空格!

application.properties添加完之后,我们便在代码中进行读取。
这里我们使用**@Value** 方式。
首先在类中添加 **@Component**、**@ConfigurationProperties**这两个注解。
第一个注解表示这个类是获取配置文件,第二个注解表示从配置文件中获取的数据转换为对象。因为我们使用了层级结构,获取web.pancm目录下的参数,所以我们再加上prefix = "web.pancm"这个表示获取这之后的数据,就避免了下面在重复书写。
那么代码如下:

import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * * Title: MyProperties * Description: * 从application.properties 获取 配置 * Version:1.0.0 * @author pancm * @date 2018年1月11日 */ @Component @ConfigurationProperties(prefix = "web.pancm") public class MyProperties { /** * 获取个人标题 * */ @Value("${title}") private String title; /** * 获取个人描述 */ @Value("${description}") private String description; /** get和set略 */ }

本类中可以直接获取该属性,不过在外部类调用的时候,需要添加**@Autowired**。

例如:

@Autowired MyProperties myProperties; System.out.println(myProperties.getTitle()); System.out.println(myProperties.getDescription());

上面的是获取application.properties中的方法。
如果想自定义配置文件和属性的话,只需再添加一个**@PropertySource**注解就可,然后添加 value属性,指向文件路径即可。
例如:

import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * * Title: MyConfig * Description: * 自定义配置文件 * Version:1.0.0 * @author pancm * @date 2018年1月20日 */ @Component @ConfigurationProperties(prefix = "myconfig") @PropertySource(value = "classpath:myconfig.proferties") public class MyConfig { @Value("${test}") private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; } }

调用方法同上!

注: 之前的springBoot版本的@ConfigurationProperties注解可以使用 locations 方法来指定自定义配置文件路径,不过在 springBoot 1.5以上的就已经不支持 location属性,所以这里使用的是PropertySource。

过滤器

过滤器是什么?
简单的来说,过滤器就是过滤的作用,在web开发中过滤一些我们指定的url。
过滤器主要做什么?
过滤掉一些不需要的东西,例如一些错误的请求。
也可以修改请求和相应的内容。

过滤器的代码实现
过滤器(filter)有三个方法,其中初始化(init)和摧毁(destroy)方法一般不会用到,主要用到的是doFilter这个方法。
而至于怎么过滤呢?
如果过滤通过,则在doFilter执行filterChain.doFilter(request,response);该方法。

这里我们在过滤器中设置下请求的时间, 符合就通过。否则返回错误信息!
代码示例:

class MyFilter implements Filter { @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) srequest; //执行过滤操作... System.out.println("请求的url :"+request.getRequestURI()); // 获取系统时间 Calendar ca = Calendar.getInstance(); int hour = ca.get(Calendar.HOUR_OF_DAY); // 设置限制运行时间 if (0<hour && hour < 18) { HttpServletResponse response = (HttpServletResponse) sresponse; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); // 消息 Map<String, Object> messageMap = new HashMap<>(); messageMap.put("status", "1"); messageMap.put("message", "此接口可以请求时间为:18-24点"); ObjectMapper objectMapper=new ObjectMapper(); String writeValueAsString = objectMapper.writeValueAsString(messageMap); response.getWriter().write(writeValueAsString); return; } filterChain.doFilter(srequest, sresponse); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("参数初始化:"+filterConfig); } @Override public void destroy() { System.out.println("开始销毁..."); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspdjx.html