博主最近在自学SpringBoot,由于它其中蕴含有许多的注解,于是便在此记录下来,方便日后的记忆,更新的进度与自学进度相同。


相关注解

  • @AutoConfigurationPackage
  • @Autowired
  • @Bean
  • @Colum(name="xxxx",length=xx)
  • @Component
  • @Conditional()
  • @ConditionalOnBean()
  • @ConditionalOnClass(xxxxxx.class)
  • @ConditionalOnExpression
  • @ConditionalOnJava
  • @ConditionalOnjndi
  • @ConditionalOnMissingBean()
  • @ConditionalOnProperty(prefix="xxxxx",value="enable" matchmissing="true/false")
  • @ConditionalOnResource
  • @ConditionalOnSingleCandidate
  • @ConditionalOnWebApplication
  • @Configuration
  • @ConfigurationProperties(prefix="xxxxx")
  • @Controller
  • @DeleteMapping(value="/xxxxx/xxxxxx")
  • @Email
  • @EnableAutoConfiguration
  • @EnableConfigurationProperties(xxxxx.class)
  • @Entity
  • @ExceptionHandle(xxxxx.class)
  • @GeneratedValue(stategy=GenerationType.IDENTITY)
  • @GetMapping(value="/xxxxx/xxxxxx")
  • @Id
  • @Import("xxxxxx.class")
  • @ImportResouce(locations={"classpath:XXX.xml"})
  • @Mapper
  • @MapperScan(value="com.xxxx.xxxx")
  • @PostMapping(value="xxxxx/xxxx")
  • @PathVariable
  • @PropertySource(value={"classpath:xxxxxxxxxx"})
  • @PutMapping(value="/xxxxx/xxxxxx")
  • @Repository
  • @RequestBody
  • @RequestMapping("/xxxxxx")
  • @RequestParam("xxxx")
  • @ResponseBody
  • @RestController
  • @SpringBootApplication
  • @SpringBootConfiguration
  • @SpringBootTest
  • @Table(name="")
  • @Value("")


@AutoConfigurationPackage

作用:自动配置包。

@Autowired

作用:将类自动注入。
事例:

public class SpringBootConfigApplicationTests{
	@Autowired
	Person personl

	@Test
	public void contextLoads(){
		System.out.println(person);
	}
}

@Bean

作用:将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名。

@Configuration
public class MyAppConfig{
	@Bean
	public HelloService helloService(){
		return new HelloService();
	}
}

@Colum(name=“xxxx”,length=xx)

作用:用于JPA中,表示标注的是和数据表对应的一个列,括号里可以设置列名和相关属性,如列的长度,括号可以省略,在省略的状况下默认列名就是属性名。

@Component

作用:将类加入到容器中。

@Conditional()

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置类里面的所有内容才生效。

@ConditionalOnBean()

作用:容器中存在指定bean。

@ConditionalOnClass(xxxxxx.class)

作用:判断当前项目有没有xxxxxx这个类。

@ConditionalOnExpression

作用:满足SpEL表达式指定

@ConditionalOnJava

作用:判断当前java版本是否符合要求。

@ConditionalOnjndi

作用:jndi存在指定项。

@ConditionalOnMissingBean()

作用:容器中不存在指定bean。

@ConditionalOnProperty(prefix=“xxxxx”,value=“enable” matchmissing=“true/false”)

作用:判断配置文件中是否存在某个配置,如果不存在认为这个判断成立的/不成立的
简单来说就是即使配置文件中不配置xxxxx,也是默认生效的/不生效的

@ConditionalOnResource

作用:类路径下是否存在指定资源文件。

@ConditionalOnSingleCandidate

作用:容器中只有一个指定的Bean,或者这个Bean是首选Bean

@ConditionalOnWebApplication

作用:判断当前是不是web应用。如果是,当前配置类生效。

@Configuration

作用:在配置类上标注这个注解,告诉spring这是一个配置类。

@ConfigurationProperties(prefix=“xxxxx”)

作用:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,默认从全局配置文件中获取值。
prefix=“xxxxx”:配置文件中xxxxx下面的所有属性进行一一映射。
将配置文件中对应的值域XXXXX绑定起来。

@Controller

作用:声明它标记的类就是一个SpringMvc Controller对象。
事例:

@Controller
public class HelloController{
	@ResponseBody
	@RequestMapping("/hello")
	public String hello(){
		return "Hello World!";
	}
}

@DeleteMapping(value="/xxxxx/xxxxxx")

作用:处理delete请求,与@RequestMapping(value="/xxxxx/xxxxxx",method=“RequestMethod.DELETE”)相同。

@Email

作用:JSR303数据校验规则。说明标注的东西必须填成邮箱格式

@EnableAutoConfiguration

作用:开启自动配置功能。以前需要自己配置的东西,现在由Spring Boot帮我们自动配置。@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效。

@EnableConfigurationProperties(xxxxx.class)

作用:启用指定类的ConfigurationProperties功能。

@Entity

作用:告诉JPA这是一个实体类(和数据表映射的关系)

@ExceptionHandle(xxxxx.class)

作用:所注释的类去处理一个名为xxxxx的异常。

@GeneratedValue(stategy=GenerationType.IDENTITY)

作用:用于JPA中,与@Id可混用,表示自增主键。

@GetMapping(value="/xxxxx/xxxxxx")

作用:处理get请求,与@RequestMapping(value="/xxxxx/xxxxxx",method=“RequestMethod.GET”)相同。

@Id

作用:一般用于JPA的实体类中,表示标注的这是一个主键。

@Import(“xxxxxx.class”)

作用:给容器中导入xxxxxx组件。

@ImportResouce(locations={“classpath:XXX.xml”})

作用:导入Spring的配置文件,让配置文件里面的内容生效。
想让Spring的配置文件生效,加载进来,@ImportResouce标注在一个配置类上。

@ImportResouce(locations={"classpath:beans.xml"})
导入Spring的配置文件让其生效

@Mapper

作用:一般注解于一个Mapper接口上,指定这个接口是一个操作数据库的Mapper

@MapperScan(value=“com.xxxx.xxxx”)

作用:使用MapperScan批量扫描在com.xxxx.xxxx所有的Mapper接口

@PostMapping(value=“xxxxx/xxxx”)

作用:就是@RequestMapping(method=RequestMethod.POST)
事例:

@Controller
public class LoginController{
	//@RequestMapping(value="/user/login",method="RequestMethod.POST")
	@PostMapping(value="/user/login")//效果与上面@RequestMapping效果相同
	public String login(){
		return "main";
	}
}

@PathVariable

作用:映射 URL 绑定的占位符

@PropertySource(value={“classpath:xxxxxxxxxx”})

作用:加载指定的配置文件。 告诉Spring Boot加载类路径下的xxxxxx位置的配置,并绑定到对象中。

@PutMapping(value="/xxxxx/xxxxxx")

作用:处理put请求,与@RequestMapping(value="/xxxxx/xxxxxx",method=“RequestMethod.PUT”)相同。

@Repository

作用:将Dao类声明为Bean

@RequestBody

作用:用于处理前端传递给后端的JSON数据请求

@RequestMapping("/xxxxxx")

作用:接受来自浏览器的xxxx请求。
事例:

@Controller
public class HelloController{
	@ResponseBody
	@RequestMapping("/hello")
	public String hello(){
		return "Hello World!";
	}
}

@RequestParam(“xxxx”)

作用:直接从请求参数里面获取xxxx。
事例:

@Controller
public class LoginController{
	@PostMapping(value="/user/login")//效果与上面@RequestMapping效果相同
	public String login(@RequestParam("username")String username,
						@RequestParam("password")String password,
						Map<String,Object> map){
		if(!StringUtils.isEmpty(username)&&"123456".equals(password)){
			return "main";
		}else{
			map.put("msg","登录失败");
			return "login";
		}
	}
}

@ResponseBody

作用:将类的方法返回的数据直接写给浏览器。
事例:

@Controller
public class HelloController{
	@ResponseBody
	@RequestMapping("/hello")
	public String hello(){
		return "Hello World!";
	}
}

@RestController

作用:相当于@ResponseBody与@Controller结合起来的效果。
事例:

@RestController
public class HelloController{
	@RequestMapping("/hello")
	public String hello(){
		return "Hello World!";
	}
}

@SpringBootApplication

作用:来标注一个主程序类,说明这是一个Spring Boot应用。
事例:

@SpringBootApplication
public class HelloWorldMainApplication{
	public static void main(String[] args){
		//spring应用启动起来
		SpringApplication.run(HelloWorldMainApplication.class,args);
	}
}

@SpringBootConfiguration

作用:Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类。效果与@Configuration差不多。

@SpringBootTest

作用:声明标注的类是Spring Boot的单元测试类,可以在测试期间很方便的类似编码一样进行自动注入的容器的功能。

@Table(name="")

作用:来指定和哪个数据表相对应。如果省略默认表面为类名小写。

@Value("")

作用:给自变量赋值。支持${Spel}(自变量表达式语言),不支持JSR303数据校验。