SpringBoot 知识点1:常用注解

1. @SpringBootApplication

@SpringBootApplication注解是一个快捷的配置注解,在被它标注的类中,可以定义一个或多个Bean,并自动触发自动配置Bean和自动扫描组件。

@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan

@SpringBootApplication用于SpringBoot启动类

2. @EnableAutoConfiguration

@EnableAutoConfiguration注解用于通知Spring,根据当前类路径下引入的依赖包,自动配置与这些依赖包相关的配置项。

3. @PropertySource

用法:

方法1:
  1. Controller上面配置
    @PropertySource({“classpath:resource.properties”})

  2. 增加属性
    @Value("${test.name}")
    private String name;

方法2:实体类配置文件

步骤:

  1. 添加 @Component 注解;
  2. 使用 @PropertySource 注解指定配置文件位置;
  3. 使用 @ConfigurationProperties 注解,设置相关属性;
  4. 必须 通过注入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)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值