目录
1.@SpringBootApplication:标记该类为主程序启动类,用在程序入口类上
4.@ResponseBody:将返回类型包装成一个json对象
5.@RestController:改注解是一个组合注解,等同于将@Controller和@ResponseBody两个注解组合使用
6.@Autowired:自动注入,从spring里的Bean中得到Bean对象
7.@Component:用于将对象作为bean自动注入到Spring容器中
1.@Configuration:改注解用于表达当前类是一个自定义配置类,改类会作为Bean组件添加到Spring容器中,这里等同于@Component
3.@ConfigurationProperties(prefix="test"):改注解将上述自定义配置文件中xxx.properties中以test开头的属性值注入该配置类属性中
5.@ImportResource("classpath:beans.xml"):加载自定义xml配置文件位置,该配置需要加在项目启动类上。
一、常用注解
1.@SpringBootApplication:标记该类为主程序启动类,用在程序入口类上
@SpringBootApplication
public class SpringbootestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootestApplication.class, args);
}
}
2.@Controller:标记该类是一个控制器
3.@RequestMapping:标记请求路径
4.@ResponseBody:将返回类型包装成一个json对象
5.@RestController:改注解是一个组合注解,等同于将@Controller和@ResponseBody两个注解组合使用
6.@Autowired:自动注入,从spring里的Bean中得到Bean对象
7.@Component:用于将对象作为bean自动注入到Spring容器中
8.@SpringBootTest:测试类注解需要与@RunWith一起使用
@SpringBootTest
@RunWith(SpringRunner.class)
class SpringbootestApplicationTests {
@Autowired
private ApplicationContext applicationContext;
@Autowired
Myproperties myproperties;
@Test
void contextLoads() {
System.out.println(myproperties);
}
}
二、配置类注解
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注解用于进行多环境配置,并通过属性标识配置环境。
@Configuration
@Profile("test")
public class TestDBConnecter implements DBConnecter{
@Override
public String configure() {
System.out.println("Test环境");
return "Test环境";
}
}
三、数据库类注解
(1)Redis
1.@RedisHash("xxx"):用于指定操作实体类对象在Redis数据库中的存储空间
2.@Id:用于表示实体类逐渐,在Redis中会默认生成字符串形式的HashKey表示唯一的实体对象id,当然也可以在数据存储时手动指定id
3.@Indexed:用于表示对应属性在Redis数据库中生成二级索引;使用该注解会在redis数据库中生成属性对应的二级索引。索引名称就是属性名,可以方便地进行数据条件查询