springBoot(一)

来源:互联网 发布:什么是主域名与子域名 编辑:程序博客网 时间:2024/06/06 00:23

1.SpringBoot的启动类

@SpringBootApplicationpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
启动后,默认的tomcat端口是8080,可以进行自定义端口

@Beanpublic EmbeddedServletContainerFactory getServletContainer(){TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();factory.setPort(8001);factory.setSessionTimeout(10, TimeUnit.MINUTES);factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound.html"));return factory;}
配置端口,还可以配置无法访问的路径页面



@SpringBootApplication(scanBasePackages = {        "com.example.xxx",        "com.example.xxx"})@EnableSpringHttpSessionpublic class DemoApplication extends WebMvcConfigurerAdapter {    public static void main(String... args) {        SpringApplication springApplication = new SpringApplication(WebAnalysisRestApplication.class);        springApplication.run(args);    }    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));        List<MediaType> supportedMediaTypes = Lists.newArrayList();        supportedMediaTypes.add(MediaType.parseMediaType("application/json;charset=UTF-8"));        stringConverter.setSupportedMediaTypes(supportedMediaTypes);        stringConverter.setWriteAcceptCharset(false);        converters.add(stringConverter);    }}

继承了webMvcConfigurerAdapter,重写了configureMessageConverters方法,定制了http的消息转换,http输入请求将消息转为java实体类,输出请求是转成json格式,将字符集设置为utf-8

@SpringBootApplication注解的scanBasePackages,自动扫描路径下的所有Bean

@EnableSpringHttpSession  基于session的管理

@EnableTransactionManagement  基于事务的管理,一般用于启动service层,在操作数据库的service类或方法上加上@Transactional

@EnableScheduling 基于定时任务的管理,在方法前加上@Scheduled即可实现定时任务









原创粉丝点击