首先明白的一点的是:SpringBoot是默认采用ssm架构,里面的日志采用的是logback ,嵌入式的tomcat容器。
配置端口号,项目名等我们除了可以通过application.properties配置文件配置外,我们可以用代码自定义配置tomcat的相关设置。
server.port= 9999
spring.thymeleaf.cache= false //解除thymeleaf的缓存,此处没有用到
这个是时候启动项目,端口号是9999
在项目路径下,新建configure包,里面新建MyConfigure类,用于项目的配置
@Configuration
public class MyConfigure {
/**
* 配置嵌入式容器的一些基本配置
* @return
*/
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8088);
//configurableEmbeddedServletContainer.setContextPath("/test");
}
};
}
configurableEmbeddedServletContainer.setPort(8088); 设置端口号为8088了
这个时候启动容器,端口号是8088
在之前的ssm,ssh中,我们配置Servlet、Filter、Listener都是在web.xml里面配置的。但是我们使用SpringBoot来开发项目之后,在项目中已经没有web.xml配置文件了,那么我们该如何编写和配置这三大组件呢。
ServletRegistrationBean 用于配置Servlet
FilterRegistrationBean 用户配置Filter
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
System.out.print("访问了MyServlet的get方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
System.out.print("访问了MyServlet的get方法");
}
}
/**
* 注册自己编写的Servlet
* @return
*/
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.print("访问了MyFilter的init方法");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.print("访问了MyFilter的doFilter方法");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.print("访问了MyFilter的destroy方法");
}
}
/**
* 注册自己编写的Filter拦截器
* @return
*/
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/say1"));
return filterRegistrationBean;
}
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("访问了MyListener的contextInitialized方法,启动了监听");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("访问了MyListener的contextDestroyed方法,销毁了监听");
}
}
/**
* 注册自己编写的Listener监听器
* @return
*/
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean(new MyListener());
return servletListenerRegistrationBean;
}
注意SpringBoot配置SpringMCV的dispatchServlet是在DispatcherServletAutoConfiguration.class中
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public ServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet, this.serverProperties.getServletMapping());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}