Skip to content
章节导航

Spring Boot 常用注解

@SpringBootApplication

定义在 main 方法入口类处,用于启动 spring boot 应用项目

@EnableAutoConfiguration

  • 让 spring boot 根据类路径中的jar包依赖当前项目进行自动配置
  • 在 src/main/resources 的 META-INF/spring.factories

@Value

application.properties定义属性,直接使用 @Value 注入即可

shell
public class A{
  @Value("${push.start:0}") //   如果缺失,默认值为0
  private Long  id;
}

@Configuration

配置注解

@Configuration("name")//表示这是一个配置信息类,可以给这个配置类也起一个名称
public class Config {

    @Autowired //自动注入,如果容器中有多个符合的bean时,需要进一步明确
    @Qualifier("compent") //进一步指明注入bean名称为compent的bean
    private Compent compent;

    @Bean //类似于xml中的<bean id="newbean" class="spring4.Compent"/>
    public Compent newbean(){
        return new Compent();
    }   
}

@Component

泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@ConfigurationProperties

可以新建一个 properties 文件,ConfigurationProperties 的属性 prefix 指定 properties 的配置的前缀,通过 location 指定 properties 文件的位置

shell
@ConfigurationProperties(prefix="person")
public class PersonProperties {
 
   private String name ;

   private int age;
   
}

@EnableConfigurationProperties

用 @EnableConfigurationProperties 注解使 @ConfigurationProperties 生效,并从 IOC 容器中获取 bean。

https://blog.csdn.net/u010502101/article/details/78758330

@RestController

组合 @Controller 和 @ResponseBody

@Controller

用于标注控制层组件(如struts中的action)

@RequestMapping

  • 用来映射web请求(访问路径和参数)、处理类和方法,可以注解在类或方法上。注解在方法上的路径会继承注解在类上的路径。
  • produces属性: 定制返回的response的媒体类型和字符集,或需返回值是json对象
shell
@RequestMapping(value="/add/user", produces="application/json;charset=UTF-8", method = RequestMethod.POST)

@PathVariable

用来获得请求url中的动态参数

@Controller  
public class TestController {  

     @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)  
     public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId){
           
         System.out.println("User Id : " + userId);  
         System.out.println("Role Id : " + roleId);  
         return "hello";  
     
     }  
}

@RequestParam

获取request请求的参数值

public List<UserInfoVO> getOpList(HttpServletRequest request,
  @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
  @RequestParam(value = "pageSize", required = false) Integer pageSize) {
}

@ResponseBody

支持将返回值放在response体内,而不是返回一个页面。比如Ajax接口,可以用此注解返回数据而不是页面。此注解可以放置在返回值前或方法前。

另一个玩法,可以不用@ResponseBody。
继承FastJsonHttpMessageConverter类并对writeInternal方法扩展,在spring响应结果时,再次拦截、加工结果
// stringResult: json返回结果
//HttpOutputMessage outputMessage

byte[] payload = stringResult.getBytes();
outputMessage.getHeaders().setContentType(META_TYPE);
outputMessage.getHeaders().setContentLength(payload.length);
outputMessage.getBody().write(payload);
outputMessage.getBody().flush();

@Autowired

  • 在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。
  • 当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false),这等于告诉 Spring: 在找不到匹配 Bean 时也不报错

@Service

用于标注业务层组件

@ImportResource

加载xml配置,一般是放在启动main类上

shell
//  单个
@ImportResource("classpath*:/spring/*.xml")

//  多个
@ImportResource({
  "classpath*:/spring/1.xml",
  "classpath*:/spring/2.xml"
})

@Bean

@Bean(name="bean的名字", initMethod="初始化时调用方法名字", destroyMethod="close")定义在方法上,在容器内初始化一个bean实例类。

@Bean(destroyMethod="close")
@ConditionalOnMissingBean
public PersonService registryService() {
  return new PersonService();
}

@Repository

用于标注数据访问组件,即 DAO/Mapper 组件

@PostConstruct

Spring容器初始化时,要执行该方法

@PostConstruct  
public void init() {   
}

@ComponentScan

注解会告知 Spring 扫描指定的包来初始化 Spring

@ComponentScan(basePackages = "com.github.xx")

@ConditionalOnMissingClass

如果存在它修饰的类的 bean,则不需要再创建这个bean。

@ConditionOnMissingBean

表示如果 name 为“example”的bean存在,该注解修饰的代码块不执行。