Spring注解
在Spring Core注解中,主要讨论Spring DI和Spring IOC中使用的Spring核心注释。众所周知,Spring DI和Spring IOC是Spring框架的核心概念。所以介绍org.springframework.beans.factory.annotation 和org.springframework.context.annotation 包中的注解。这两个包中注解有很多,就抽取其中的15个注解。
Spring Core Annotations:
● @Autowired● @Qualifier● @Bean● @Required● @Value● @DependsOn● @Lazy● @Lookup● @Primary● @Scope● @Profile● @Import● @ImportResource● @PropertySource● @PropertySources
Spring注解
1.1 @Autowired
@Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,@Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上,也可以放在任意方法上表示,自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。
这里必须明确:@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Qualifier使用;
1.1.1 构造器注入
@RestControllerpublic class UserController {private UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}}
1.1.2 setter方法注入
@RestControllerpublic class UserController {private UserService userService;@Autowiredpublic void setUserService(UserService userService) {this.userService = userService;}}
1.1.3 field反射注入
@RestControllerpublic class UserController {@Autowiredprivate UserService userService;}
1.2 @Qualifier
上面已经说到@Autowired按类型装配Spring Bean。如果容器中有多个相同类型的bean,则框架将抛出NoUniqueBeanDefinitionException, 以提示有多个满足条件的bean进行自动装配。程序无法正确做出判断使用哪一个,通过将@Qualifier注解与我们想要使用的特定Spring bean的名称一起进行装配,Spring框架就能从多个相同类型并满足装配要求的bean中找到我们想要的,
@Component("studentInfo")public class StudentInfo implements UserInfo {public String userName() {return "student";}}@Component("teacherInfo")public class TeacherInfo implements UserInfo {public String userName {return "teacher";}}@Componentpublic class UserService {@Autowired@Qualifier("studentInfo")private UserInfo userInfo;//todo}
1.3 @Bean
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名。
@Configurationpublic class BeanConfig {@Beanpublic Person userInfo() {return new UserInfo("toutou", 18);}}
这个配置就等同于之前在xml里的配置:
<bean id="userInfo" class="com.test.UserInfo"><property name="age" value="18"/><property name="name" value="请叫我头头哥 http://toutou.cnblogs.com"/></bean>
1.4 @Required
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。
@Required
void setUserName(String name) {
this.name = name;
}
<bean class="com.test.UserInfo">
<property name="name" value="张三" />
</bean>
1.5 @Value
@Value将外部的值动态注入到Bean中。”注入外部的值”可以有很多种,它可以注入普通字符串、注入java 系统变量、注入表达式结果、注入其他Bean属性、将配置文件 .properties 或 .yml (*.yaml) 配置的 属性、文件资源和url资源等注入。
Spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///数据库名
username: root
password: root
@Configuration
public class DataSourceProperties{
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
}
1.6 @DependsOn
Spring容器载入bean顺序是不确定的,Spring框架也没有约定特定载入顺序逻辑规范。@DependsOn注解可以定义在类和方法上,比如说A组件要依赖于B组件,那就是B组件需要比A组件先注册到IOC容器中。
public class FirstBean {
@Autowired
private SecondBean secondBean;
}
public class SecondBean {
public SecondBean() {
System.out.println("SecondBean init");
}
}
@Configuration
public class BeanConfig {
@Bean("firstBean")
@DependsOn(value = {"secondBean"})
public FirstBean firstBean() {
return new FirstBean();
}
@Bean("secondBean")
public SecondBean secondBean() {
return new SecondBean();
}
}
1.7 @Lazy
@Lazy注解用于标识bean是否需要延迟加载。Spring IoC容器一般都会在启动的时候实例化所有单实例bean,如果想要Spring在启动的时候延迟加载A,即在调用B的时候再去初始化,则可以使用@Lazy注解。
public class FirstBean {
public void test() {
System.out.println("FirstBean Class");
}
}
public class SecondBean {
public void test() {
System.out.println("SecondBean Class");
}
}
@Configuration
public class AppConfig {
@Lazy(value = true)
@Bean
public FirstBean firstBean() {
return new FirstBean();
}
@Bean
public SecondBean secondBean() {
return new SecondBean();
}
}
1.8 @Lookup
@Lookup的注解是一个作用在方法上的注解,被其标注的方法会被重写,然后根据其返回值的类型,容器调用BeanFactory的getBean()方法来返回一个bean。
1.9 @Primary
@Primary与@Qualifier类似,都是解决@Autowired时容器中有多个相同类型bean的问题,Primary可以理解为默认优先选择,同时不可以同时设置多个。
@Component("studentInfo")
public class StudentInfo implements UserInfo {
public String userName() {
return "student";
}
}
@Component("teacherInfo")
@Primary
public class TeacherInfo implements UserInfo {
public String userName {
return "teacher";
}
}
@Component
public class UserService {
@Autowired
@Qualifier("studentInfo")
private UserInfo userInfo;
//todo
}
1.10 @Scope
@Scope注解是springIoc容器中的一个作用域,在 Spring IoC 容器中具有以下几种作用域:基本作用域singleton(单例)(默认作用域)、prototype(多例),Web 作用域(reqeust、session、globalsession),自定义作用域
1.11 @Profile
@profile注解的作用是为了应对多环境开发,比如开发环境使用dev, 生产环境使用prod,就可以使用@Profile注解实现不同的开发环境使用不同的数据源。spring3.2之前 @Profile注解用在类上,spring3.2 之后 @Profile注解用在方法上
1.12 @Import
@Import用于注入指定的类,导入组件id默认是组件的全类名。
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
1.13 @ImportResource
@ImportResource注解用于导入Spring的配置文件,让配置文件里面的内容生效;(就是以前写的springmvc.xml、applicationContext.xml)
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}
1.14 @PropertySource
@PropertySource注解加载指定的配置文件
@Configuration
@PropertySource("classpath:config.properties")
public class ProperySourceDemo implements InitializingBean {
@Autowired
Environment env;
@Override
public void afterPropertiesSet() throws Exception {
setDatabaseConfig();
}
private void setDatabaseConfig() {
DataSourceConfig config = new DataSourceConfig();
config.setDriver(env.getProperty("jdbc.driver"));
config.setUrl(env.getProperty("jdbc.url"));
config.setUsername(env.getProperty("jdbc.username"));
config.setPassword(env.getProperty("jdbc.password"));
System.out.println(config.toString());
}
}
1.15 @PropertySources
@PropertySources顾名思义就是可以指定多个@PropertySource来导入配置文件。
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:db.properties")
})
public class AppConfig {
//todo...
}
SpringWeb注解
2.1 @RequestBody
主要用来接收前端传递给后端的json字符串中的数据的。
@RestController
@RequestMapping("/api/v1")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/user")
public UserInfo createUser(@Valid @RequestBody UserInfo user) {
return userService.save(user);
}
}
2.2 @RequestMapping
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。也就是通过它来指定控制器可以处理哪些URL请求。
@Controller
class UserController {
@RequestMapping(value = "/user/index", method = RequestMethod.GET)
String index() {
return "index";
}
}
2.3 @GetMapping
@GetMapping注释将Http GET请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.GET)的快捷方式。
@GetMapping("/users")
public List<user> getAllUser() {
//...
}
@GetMapping("/users/{id}")
public ResponseEntity<User> getUserById(@PathVariable(value = "id") Long userId)
throws ResourceNotFoundException {
user user = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("user not found for this id :: " + userId));
return ResponseEntity.ok().body(user);
}
2.4 @PathVariable
@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
2.5 @PostMapping
@PostMapping注释将HTTP POST请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.POST)的快捷方式。
@PostMapping("/user/add")
public User addUser(@Valid @RequestBody User user) {
return userRepository.save(user);
}
其它扩展: @PutMapping、@DeleteMapping、@PatchMapping(这三种相对用的比较少,一笔带过。)
2.6 @ControllerAdvice
增强型控制器,对于控制器的全局配置放在同一个位置。可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,可处理全局异常处理、全局数据绑定和全局数据预处理。
@ControllerAdvice(basePackages = {"com.toutou.controller"} )
public class GlobalControllerAdvice {
@InitBinder
public void dataBinding(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
}
@ModelAttribute
public void globalAttributes(Model model) {
model.addAttribute("msg", "Hello World!");
}
@ExceptionHandler(FileNotFoundException.class)
public ModelAndView myError(Exception exception) {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", exception);
mav.setViewName("error");
return mav;
}
}
2.7 @ExceptionHandler
@ExceptionHandler统一处理某一类异常,从而能够减少代码重复率和复杂度。
2.8 @InitBinder
@InitBinder只在@Controller中注解方法来为这个控制器注册一个绑定器初始化方法,方法只对本控制器有效。
2.9 @ModelAttribute
@ModelAttribute注解用于将方法的参数或方法的返回值绑定到指定的模型属性上,并返回给Web视图。
2.10 @ResponseBody
@ResponseBody将controller里方法返回的对象通过适当的转换器转换为Json写入到response对象的body区.
2.11 @Controller
@Controller用于标记在一个类上,使用它标记的类就是一个SpringMvc Controller对象,分发处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。
2.12 @RestController
@RestController在Spring中的作用等同于@Controller + @ResponseBody。
2.13 @RequestParam
@RequestParam将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)
2.14 @CrossOrigin
@CrossOrigin支持跨域,可用于Controller上,也可用于方法上。
@CrossOrigin(origins = "http://toutou.com", maxAge = 3600)
@RequestMapping("/index")
String index() {
return "Hello World!";
}
SpringBoot注解
3.1 @SpringBootApplication
@SpringBootApplication是Sprnig Boot项目的核心注解,目的是开启自动配置。由于@Configuration,@EnableAutoConfiguration和@ComponentScan三个注解一般都是一起使用,于是spring boot提供了一个统一的注解@SpringBootApplication。即:@SpringBootApplication=@Configuration + @EnableAutoConfiguration + @ComponentScan。
@SpringBootApplication // 等于:@Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.2 @EnableAutoConfiguration
可以根据classpath中的jar依赖,自动注册bean,一般用于类或接口上,它尝试根据您添加的jar依赖项自动配置Spring应用程序。自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.3 @ConditionalOnClass、@ConditionalOnMissingClass
3.4 @ConditionalOnBean、@ConditionalOnMissingBean
@ConditionalOnBean // 当给定的在bean存在时,则实例化当前Bean
@ConditionalOnMissingBean // 当给定的在bean不存在时,则实例化当前Bean
@ConditionalOnClass // 当给定的类名在类路径上存在,则实例化当前Bean
@ConditionalOnMissingClass // 当给定的类名在类路径上不存在,则实例化当前Bean
3.5 @ConditionalOnProperty
@ConditionalOnProperty可以通过配置文件中的属性值来判定configuration是否被注入。
3.6 @ConditionalOnResource
@ConditionalOnResource是注解在Configuration bean上,在其加载之前对指定资源进行校验,是否存在,如果不存在,抛出异常;该注解支持传入多个变量,
3.7 @ConditionalOnWebApplication、@ConditionalOnNotWebApplication
@ConditionalOnWebApplication主要的用处是: 当Spring为web服务时,才使注解的类生效;通常是配置类;@ConditionalOnNotWebApplication不是web应用。
3.8 @Conditional
@Conditional的作用是按照一定的条件进行判断,满足条件给容器注册bean。
回到顶部
SpringScheduling注解
4.1 @Scheduled
@Scheduled可以作为一个触发源添加到一个方法中。
@Scheduled(fixedDelay=1000)
public void doSomething() {
//...
}
4.2 @EnableScheduling
@EnableScheduling 在配置类上使用,开启计划任务的支持(类上)。
@Configuration
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
public class TaskScheduleConfig {
//...
}
4.3 @Async
@Async标注的方法,称之为异步方法;这些方法将在执行的时候,将会在独立的线程中被执行,调用者无需等待它的完成,即可继续其他的操作。有时候我们会调用一些特殊的任务,任务会比较耗时,重要的是,我们不管他返回的后果。这时候我们就需要用这类的异步任务啦。
4.4 @EnableAsync
@EnableAsync注解启用了Spring异步方法执行功能
4.5 @Schedules
@Schedules作用跟@Scheduled一样,@Schedules内部包含多个@Scheduled注解,可以表示一个方法可以存在多个调度设置。
SpringCloud
@Controller 控制层,里面有多个连接
@Service 业务层,一般对于接口和实现
@Qualifier 如果一个接口有多个实现,那么注入时候加上唯一标示
@Repository 一般的dao层
@Autowired 自动注入依赖
@Resource bean的注入,同Autowired 有相同的功能。
说明:
共同点:@Resource和@Autowired都可以作为注入属性的修饰,在接口仅有单一实现类时,两个注解的修饰效果相同,可以互相替换,不影响使用。
不同点:
@Resource是Java自己的注解,@Resource有两个属性是比较重要的,分别是name和type;Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Autowired是spring的注解,是spring2.5版本引入的,Autowired只根据type进行注入,不会去匹配name。如果涉及到type无法辨别注入对象时,那需要依赖@Qualifier或@Primary注解一起来修饰。
@Component定义其它组件(比如访问外部服务的组件)
@RequestMapping (value=’’,method={RequestMethod。GET或者POSt})绑定url
@RequestParam (value=’’ required=false)绑定参数,将客户端请求中的参数值映射到相应方法的参数上;
@ModelAttribute 一般用于controller层,被注解的方法会在所以mapping执行之前执行,并且可以绑定参数到Model model里面。
@Transactional (readOnly=true)注解式事务
@TransactionalEventListener用于配置事务的回调方法,可以在事务提交前、提交后、完成后以及回滚后几个阶段接受回调事件。
@Value(“${}”)可以注入properties里面的配置项
@ControllerAdvice 是spring3提供的新注解
@ExceptionHandler 如果在controller方法遇到异常,就会调用含有此注解的方法。
@InitBinder 一般用于controller 可以将所以form 讲所有传递进来的string 进行html编码,防止xss攻击,比如可以将字符串类型的日期转换成date类型
@EnableCaching 注解自动化配置合适的缓存管理器。
@EnableWebSecurity 注解开启spring security的功能,集成websercrityconfigureadapter。
@SringBootApplication相当于@configuration,@EnableAutoConfiguation @ComponentScan三个注解合用。
@EnableDiscoveryclient 注册应用为Eureka客户端应用,以获得服务发现的能力
@EnableAdminServer 使用admin监控应用。
@EnableEurekaClient配置本应用将使用服务注册和服务发现,注意:注册和发现用这个注解。
@EnableEurekaServer 启动一个服务注册中心
@EnableHystrix表示启动断路器,断路器依赖于服务注册和发现。
@HystrixCommand注解方法失败后,系统将西东切换到fallbackMethod方法执行。指定回调方法
@EnableAutoConfiguration spring boot自动配置,尝试根据你添加的jar依赖自动配置你的spring应用。
@ComponentScan 表示将该类自动发现并注册bean 可以自动收集所有的spring组件
@Comfiguration 相当于传统的xml配置文件
@Import 导入其他配置类
@ImportResource用来 加载xml配置文件
@FeignClient注解中的fallbank属性指定回调类
@RestController 返回json字符串的数据,直接可以编写RESTFul的接口;
@CrossOrigin 可以处理跨域请求,让你能访问不是一个域的文件;
@ApiOperation 首先@ApiOperation注解不是Spring自带的,它是是swagger里的注解@ApiOperation是用来构建Api文档的@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”;
@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan;
@RefreshScope 如果代码中需要动态刷新配置,在需要的类上加上该注解就行。但某些复杂的注入场景下,这个注解使用不当,配置可能仍然不动态刷新;
@FeignClient springboot调用外部接口:声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
url: url一般用于调试,可以手动指定@FeignClient调用的地址decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contractfallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码path: 定义当前FeignClient的统一前缀
@EnableFeignClients 开启Spring Cloud Feign的支持
@EnableCircuitBreaker 开启断路器功能
@LoadBalanced 开启客户端负载均衡
@WebAppConfiguration 开启Web 应用的配置,用于模拟ServletContext
@RibbonClient,这个注解用来为负载均衡客户端做一些自定义的配置,可以进一步配置或自定义从哪里获取服务端列表、负载均衡策略、Ping也就是服务鉴活策略等等
@PathVariable 获取参数。
@JsonBackReference 解决嵌套外链问题。
@RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用
lombok
@EqualsAndHashCode 实现equals()方法和hashCode()方法 @ToString:实现toString()方法
@Data 注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter 注解在属性上;为属性提供 setting 方法
@Getter 注解在属性上;为属性提供 getting 方法
@Log4j 注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor 注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor 注解在类上;为类提供一个全参的构造方法
@Cleanup 关闭流
@Synchronized: 对象同步
@SneakyThrows: 抛出异常
@ConfigurationProperties 把同类的配置信息自动封装成实体类:可以使属性文件中的值和类中的属性对应起来;使用方式有两种 : 1、在类上使用该注解 2、在工厂方法上使用该注解 (@bean)
注意:在springBoot中除了使用这个注解读取属性文件值外,还可以用@Value注解。
jpa
1、@Entity:@Table(name=”“):表明这是一个实体类。一般用于jpa这两个注解一般一块使用,但是如果表名和实体类名相同的话,@Table可以省略。
2、@MappedSuperClass:用在确定是父类的entity上。父类的属性子类可以继承。
3、@NoRepositoryBean:一般用作父类的repository,有这个注解,Spring不会去实例化该repository。
4、@Column:如果字段名与列名相同,则可以省略。
5、@Id:表示该属性为主键。
6、@GeneratedValue(strategy=GenerationType.SEQUENCE,generator= “repair_seq”):表示主键生成策略是sequence (可以为Auto、IDENTITY、native等,Auto表示可在多个数据库间切换),指定sequence的名字是repair_seq。
7、@SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize =
1):name为sequence的名称,以便使用,sequenceName为数据库的sequence名称,两个名称可以一致。
8、@Transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性。
如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic。
9、@Basic(fetch=FetchType.LAZY):标记可以指定实体属性的加载方式。
10、@JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。
11、@JoinColumn(name=”loginId”):一对一:本表中指向另一个表的外键。一对多:另一个表指向本表的外键。
12、@OneToOne、@OneToMany、@ManyToOne:对应hibernate配置文件中的一对一,一对多,多对一。
注解集合
@ComponentScan:表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。
@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。
@Inject:等价于默认的@Autowired,只是没有required属性;
@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@JsonBackReference:解决嵌套外链问题。
@JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。
@ConfigurationProperties:Spring Boot可使用注解的方式将自定义的properties文件映射到实体bean中,比如config.properties文件。
@ConditionalOnSingleCandidate:组合@Conditional注解,当指定的class在容器中只有一个Bean,或者同时有多个但为首选时才开启配置。
@ConditionalOnCloudPlatform:组合 @Conditional 注解,当指定的云平台激活时才开启配置。
@ConditionalOnJndi:组合 @Conditional 注解,当指定的 JNDI 存在时才开启配置。
@ConditionalOnJava:组合@Conditional 注解,当运行的 Java JVM 在指定的版本范围时才开启配置。
@ConditionalOnExpression:组合 @Conditional 注解,当 SpEL 表达式为 true 时才开启配置。
@WiselyConfiguration: 组合注解可以替代@Configuration和@ComponentScan
@Transcational: 事务处理
@Target (ElementType.TYPE):元注解,用来指定注解修饰类的那个成员 -->指定拦截规则
@Cacheable: 数据缓存
@ActiveProfiles: 用来声明活动的 profile
@RunWith: 运行器
