1. @SpringBootApplication
@SpringBootApplication注解是一个快捷的配置注解,在被它标注的类中,可以定义一个或多个Bean,并自动触发自动配置Bean和自动扫描组件。
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication用于SpringBoot启动类
2. @EnableAutoConfiguration
@EnableAutoConfiguration注解用于通知Spring,根据当前类路径下引入的依赖包,自动配置与这些依赖包相关的配置项。
3. @PropertySource
用法:
方法1:
-
Controller上面配置
@PropertySource({“classpath:resource.properties”}) -
增加属性
@Value("${test.name}")
private String name;
方法2:实体类配置文件
步骤:
- 添加 @Component 注解;
- 使用 @PropertySource 注解指定配置文件位置;
- 使用 @ConfigurationProperties 注解,设置相关属性;
- 必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
@Autowired
private ServerSettings serverSettings;
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
......
4. @RunWith和@SpringBootTest
@RunWith和@SpringBootTest一起用于SpringBoot单元测试
使用示例代码如下:
@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest(classes={ClassApplication.class})//启动整个springboot工程
public class SpringBootTest {
......
5. @AutoConfigureMockMvc
使用示例代码如下:
@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);
}
}
6 .@ControllerAdvice 和 @ExceptionHandler
@ControllerAdvice 如果是返回json数据 则用 RestControllerAdvice,就可以不加 @ResponseBody
捕获全局异常,处理所有不可知的异常
@ExceptionHandler(value=Exception.class)
7. @ServletComponentScan
2)启动类里面增加 @ServletComponentScan,进行扫描,以在项目中添加Filter
8. @WebFilter
@WebFilter 标记一个类为filter,被spring进行扫描
9. @WebServlet
标记一个类为Servlet
示例:
@WebServlet(name = "userServlet",urlPatterns = "/test/customs")
public class UserServlet extends HttpServlet{
10. @WebListener
自定义Listener
11. @Configuration
示例如下:
注册拦截器:
@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);
}
}
12. @MapperScan
启动类增加mapper扫描
示例:
@MapperScan("net.project.mapper")
技巧:保存对象,获取数据库自增id
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
13. @Transantional
service逻辑引入事务
示例:
@Transantional(propagation=Propagation.REQUIRED)