“Yeah It’s on. ”
下面是项目中一些常用的Configuration:
Spring MVC
1
2
3
4
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}CharacterEncoding
1
2
3
4
5
6
7
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding(characterEncoding);
filter.setForceEncoding(true);
return filter;
}RestTemplate
1
2
3
4
public RestTemplate restTemplate() {
return new RestTemplate();
}Filter
1
2
3
4
5
6
7
public FilterRegistrationBean appFilter() {
FilterRegistrationBean reg = new FilterRegistrationBean();
reg.setFilter(new AppFilter());
reg.addUrlPatterns("/*");
return reg;
}MessageConverter
1
2
3
4
5
6
public StringHttpMessageConverter stringHttpMessageConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(
Charset.forName(characterEncoding));
return stringHttpMessageConverter;
}JSON转换
1
2
3
4
5
6
7
public ObjectMapper jsonMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}数据源,以Druid为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public DataSource dataSource() {
return new DruidDataSource();
}
private String druidAllowUrl;
private String druidDenyUrl;
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean reg = new ServletRegistrationBean();
reg.setServlet(new StatViewServlet());
reg.addUrlMappings("/druid/*");
reg.addInitParameter("allow", druidAllowUrl);
reg.addInitParameter("deny", druidDenyUrl);
return reg;
}1
2
3
4
5
6
7##application.properties
datasource.druid.url=jdbc:oracle:thin:@192.168.1.111:1521:xxx
datasource.druid.username=xxx
datasource.druid.password=xxx
datasource.druid.filters=stat
datasource.druid.allow=192.168.0.1/24,192.168.0.212
datasource.druid.deny=192.168.200.1/24RequestContextListener
1
2
3
4
5
6
public RequestContextListener requestContextListener(){
RequestContextListener requestContextListener = new RequestContextListener();
return requestContextListener;
}上传文件,文件大小限制
1
2
3#写在application.properties 文件中
#org.springframework.boot.autoconfigure.web.MultipartProperties
multipart.maxFileSize=10Mb设置项目端口号
1
2
3#写在application.properties 文件中
# 以5100为项目端口号
server.port=5100