@SpringBootApplication注解是一个快捷的配置注解,在被它标注的类中,可以定义一个或多个Bean,并自动触发自动配置Bean和自动扫描组件。
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication用于SpringBoot启动类
@EnableAutoConfiguration注解用于通知Spring,根据当前类路径下引入的依赖包,自动配置与这些依赖包相关的配置项。
用法:
Controller上面配置
@PropertySource({“classpath:resource.properties”})
增加属性
@Value("${test.name}")
private String name;
步骤:
@Autowired
private ServerSettings serverSettings;
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
......
@RunWith和@SpringBootTest一起用于SpringBoot单元测试
使用示例代码如下:
@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest(classes={ClassApplication.class})//启动整个springboot工程
public class SpringBootTest {
......
使用示例代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={ClassApplication.class})
@AutoConfigureMockMvc
public class MockMvcTestDemo {
@Autowired
private MockMvc mockMvc;
@Test
public void apiTest() throws Exception {
MvcResult mvcResult =
mockMvc.perform(MockMvcRequestBuilders.get("/test/home")).
andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println(status);
}
}
@ControllerAdvice 如果是返回json数据 则用 RestControllerAdvice,就可以不加 @ResponseBody
捕获全局异常,处理所有不可知的异常
@ExceptionHandler(value=Exception.class)
2)启动类里面增加 @ServletComponentScan,进行扫描,以在项目中添加Filter
@WebFilter 标记一个类为filter,被spring进行扫描
标记一个类为Servlet
示例:
@WebServlet(name = "userServlet",urlPatterns = "/test/customs")
public class UserServlet extends HttpServlet{
自定义Listener
示例如下:
注册拦截器:
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
registry.addInterceptor(new TwoIntercepter()).addPathPatterns("/api2/*/**");
// .excludePathPatterns("/api2/xxx/**"); //拦截全部 /*/*/**
WebMvcConfigurer.super.addInterceptors(registry);
}
}
定义拦截器:
public class LoginIntercepter implements HandlerInterceptor {
/**
* 进入controller方法之前
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("LoginIntercepter------->preHandle");
return HandlerInterceptor.super.preHandle(request, response, handler);
}
/**
* 调用完controller之后,视图渲染之前
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("LoginIntercepter------->postHandle");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
/**
* 整个完成之后,通常用于资源清理
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("LoginIntercepter------->afterCompletion");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
启动类增加mapper扫描
示例:
@MapperScan("net.project.mapper")
技巧:保存对象,获取数据库自增id
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
service逻辑引入事务
示例:
@Transantional(propagation=Propagation.REQUIRED)