• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot常用注解

武飞扬头像
想踢球考研的java
帮助1

目录

一、常用注解

1.@SpringBootApplication:标记该类为主程序启动类,用在程序入口类上

2.@Controller:标记该类是一个控制器

3.@RequestMapping:标记请求路径

4.@ResponseBody:将返回类型包装成一个json对象

5.@RestController:改注解是一个组合注解,等同于将@Controller和@ResponseBody两个注解组合使用

6.@Autowired:自动注入,从spring里的Bean中得到Bean对象

7.@Component:用于将对象作为bean自动注入到Spring容器中

二、配置类注解

1.@Configuration:改注解用于表达当前类是一个自定义配置类,改类会作为Bean组件添加到Spring容器中,这里等同于@Component

2.@PropertySource("classpath:xxx.properties"):改注解指定了自定义配置文件的位置和名称,此实例表示自定义文件为classpath路径下的xxx.properties文件

3.@ConfigurationProperties(prefix="test"):改注解将上述自定义配置文件中xxx.properties中以test开头的属性值注入该配置类属性中

4.@EnableConfigurationProperties(MyProperties.class):开启对应配置类的属性注入功能,该注解是配合@ConfigurationProperties使用的。如果自定义配置类使用了@Component注解而非@Configuration注解,那么@EnableConfigurationProperties注解可以省略 

5.@ImportResource("classpath:beans.xml"):加载自定义xml配置文件位置,该配置需要加在项目启动类上。

6.@Profile("test"): 指定多环境配置类标识,该配置需要与@Configuration一起使用,@Configuration注解将实现类声明为配置类,可以保证springboot自动扫描并识别;@Profile注解用于进行多环境配置,并通过属性标识配置环境。


一、常用注解

1.@SpringBootApplication:标记该类为主程序启动类,用在程序入口类上

  1.  
    @SpringBootApplication
  2.  
    public class SpringbootestApplication {
  3.  
    public static void main(String[] args) {
  4.  
    SpringApplication.run(SpringbootestApplication.class, args);
  5.  
    }
  6.  
    }

2.@Controller:标记该类是一个控制器

3.@RequestMapping:标记请求路径

4.@ResponseBody:将返回类型包装成一个json对象

5.@RestController:改注解是一个组合注解,等同于将@Controller和@ResponseBody两个注解组合使用

6.@Autowired:自动注入,从spring里的Bean中得到Bean对象

7.@Component:用于将对象作为bean自动注入到Spring容器中

8.@SpringBootTest:测试类注解需要与@RunWith一起使用

  1.  
    @SpringBootTest
  2.  
    @RunWith(SpringRunner.class)
  3.  
    class SpringbootestApplicationTests {
  4.  
    @Autowired
  5.  
    private ApplicationContext applicationContext;
  6.  
     
  7.  
    @Autowired
  8.  
    Myproperties myproperties;
  9.  
    @Test
  10.  
    void contextLoads() {
  11.  
    System.out.println(myproperties);
  12.  
    }
  13.  
    }

二、配置类注解

1.@Configuration:改注解用于表达当前类是一个自定义配置类,改类会作为Bean组件添加到Spring容器中,这里等同于@Component

2.@PropertySource("classpath:xxx.properties"):改注解指定了自定义配置文件的位置和名称,此实例表示自定义文件为classpath路径下的xxx.properties文件

3.@ConfigurationProperties(prefix="test"):改注解将上述自定义配置文件中xxx.properties中以test开头的属性值注入该配置类属性中

4.@EnableConfigurationProperties(MyProperties.class):开启对应配置类的属性注入功能,该注解是配合@ConfigurationProperties使用的。如果自定义配置类使用了@Component注解而非@Configuration注解,那么@EnableConfigurationProperties注解可以省略 

5.@ImportResource("classpath:beans.xml"):加载自定义xml配置文件位置,该配置需要加在项目启动类上。

6.@Profile("test"): 指定多环境配置类标识,该配置需要与@Configuration一起使用,@Configuration注解将实现类声明为配置类,可以保证springboot自动扫描并识别;@Profile注解用于进行多环境配置,并通过属性标识配置环境。

学新通

  1.  
    @Configuration
  2.  
    @Profile("test")
  3.  
    public class TestDBConnecter implements DBConnecter{
  4.  
    @Override
  5.  
    public String configure() {
  6.  
    System.out.println("Test环境");
  7.  
    return "Test环境";
  8.  
    }
  9.  
    }

三、数据库类注解

(1)Redis

1.@RedisHash("xxx"):用于指定操作实体类对象在Redis数据库中的存储空间

2.@Id:用于表示实体类逐渐,在Redis中会默认生成字符串形式的HashKey表示唯一的实体对象id,当然也可以在数据存储时手动指定id

3.@Indexed:用于表示对应属性在Redis数据库中生成二级索引;使用该注解会在redis数据库中生成属性对应的二级索引。索引名称就是属性名,可以方便地进行数据条件查询

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgciehc
系列文章
更多 icon
同类精品
更多 icon
继续加载