SSM(Spring SpingMVC Mybatis)框架常用注解

本文详细介绍了Spring Boot和Spring框架中常见的注解,包括@SpringBootApplication、@EnableConfigurationProperties、@ConfigurationProperties等用于配置注入,@ComponentScan、@Service、@Repository、@Component、@Autowire等用于组件管理,以及@Controller、@ResponseBody、@RestController、@RequestMapping等Spring MVC注解。此外,还提到了@Resource、@PostConstruct和@PreDestroy等其他关键注解及其在Spring bean生命周期中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Spring Boot的常用的注解

@SpringBootApplication

此注解是Sprnig Boot项目的核心注解,目的是开启自动配置

@EnableConfigurationProperties和@ConfigurationProperties

属性注入

将属性配置注入到Spring 的Bean中,在很多中间件中都需要去定义一些属性的,方式总共有四种:

第一种方式,@ConfigurationProperties 和 @Component 搭配使用,属性需要在 application.yml 中进行配置;

@ConfigurationProperties(prefix = "person")
@Component
public class Person {
    private Integer id;
    private String name;
    private List<String> interests;
    
    // setter and getter 
}
# yaml 文件中内容
person:
   id: 10
   name: 李四
   interests:
     - movie
     - sport
     - read

第二种方式,@ConfigurationProperties 和 @EnableConfigurationProperties 搭配使用,但是要注意以下两点:

  1. @EnableConfigurationProperties 需要放在一个被纳入到 IOC 容器的Bean的头顶;
  2. @EnableConfigurationProperties 和 @ConfigurationProperties 不是在一起使用的;
  3. @EnableConfigurationProperties 的值是头顶加了 @ConfigurationProperties 这个类的 Class 类型的对象
@ConfigurationProperties(prefix = "person")
public class Person {
    private Integer id;
    private String name;
    private List<String> interests;
    
    // setter and getter 
}
@SpringBootApplication
@EnableConfigurationProperties(Person.class)
public class FirstSpringBootApplication {   // FirstSpringBootApplication也会被纳入到IOC容器中
    public static void main( String[] args) {
        /**
         * 启动springboot的项目,第一个参数是标注了 @SpringBootApplication 这个注解的类型
         */
		SpringApplication.run(FirstSpringBootApplication.class, args);
    }
}
# yaml 文件中内容
person:
   id: 10
   name: 李四
   interests:
     - movie
     - sport
     - read

第三种方式,@PropertySource(“classpath: /config.properties”) 和 @Component 搭配使用,然后属性要使用 @Value 实现注入

@PropertySource(value="classpath:/config.properties", encoding = "utf-8")
@Component
public class User {
    @Value("${user.id}")
    private Integer id;
    @Value("${user.nickname}")
    private String name;
    @Value("${user.interests}")
    private List<String> interests;
    // 
}

第四种方式,@PropertySource 和 @ConfigurationProperties 、@Component 搭配使用,@PropertySource来指定外部资源; @ConfigurationProperties 指定前缀。不需要使用 @Value 来实现属性的注入

@PropertySource("classpath:/config.properties")
@ConfigurationProperties(prefix = "wx")
@Component
public class Pay {
    private String appKey;
    private String tradeId;
    
    // setter and getter 
}

2.Spring的常用注解

@ComponentScan

@ComponentScan这个注解在Spring中很重要,它对应XML配置中的元素,@ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中。
我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。

@Service

这个注解放到 Serviece 的实现类上。

@Repository

这个注解放到 DAO 的实现类上。

@Component

遇到性质不明确的类的时候,就加上该注解。

@Autowire

(纳入IOC容器)首先根据类型查找,如果多个,就看谁的头顶的有 @Primary,就注入谁;如果没有没有@Primary 注解,就按照名字来进行注入,如果名字都没有区分就报异常。

@Qualifier

它的出现就是为了和 @Autowire 搭配使用的,就是指定一个名字,在注入的时候根据类型的时候同时判断名字来实现注入,无视名字和@Primary注解,只根据@Qualifier来创建。

@Resource

首先根据名字来实现注入,如果命名没有相同,就根据类型查找,如果类型有多个,就看谁的大头顶有 @Primary 注解,名字优先于@Primary。

@Bean

@Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean

3.springmvc的常用注解

@Controller

@Controller表示在服务器启动的时候,把这个类作为一个控制器加载到Spring的Bean工厂,加上了这个注解的类就相当于以前的一个servlet层的类,主要负责处理前端控制器(DispatcherServlet )发过来的请求,经过业务逻辑层处理之后封装层一个model,并将其返回给view进行展示。

@ResponseBody

此注解的主要作用就是返回给前端的数据全为json数据

@RestController

@RestController注解里面包含了@Controller注解和@ResponseBody注解

@RequestMapping

此注解是一个用来处理请求地址映射的注解(设置一个前端可访问的域名后缀),它可以用于类上,也可以用于方法上。用于类上的注解会将一个特定请求或者请求模式映射到一个控制器之上,表示类中的所有响应请求的方法都是以该地址作为父路径;方法的级别上注解表示进一步指定到处理方法的映射关系。

@PathVariable

此注解主要用来获取 URL 参数,如:

http://localhost:8080/login?username=zhangsan&password=123

@RequestParam

此注解主要用来获取 URL 参数,如:

http://localhost:8080/login/zhangsan/123

@RequestBody

此注解用于形参列表中,目的是将前端转换的json数据转换为一个实体类,这种类一般放在dto包中

@ResponseBody

词注解用于方法上,目的是返回给前端的数据为json数据

@GetMapping

注解用于 GET 请求上,接收 JSON 实体参数,设置后置域名

@PostMappding

注解用于 POST 请求上,接收 JSON 实体参数,设置后置域名

4. 其他常用注解

@Resource

首先根据名字来实现注入,如果命名没有相同,就根据类型查找,如果类型有多个,就看谁的大头顶有 @Primary 注解,名字优先于@Primary。

@PostConstruct

对应着 init-method 这个配置的一个注解,是spring bean的生命周期的一个注解。在Spring中碰到方法名或者注解中带有 Post, 表示在XXXX之后。

@PreDestroy

对应着destroy-method这个配置的一个注解,销毁的时候执行的方法。在Spring中碰到方法名或者注解中带有 Pre, 表示在XXXX之前。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值