Spring那些标签

1.@RestController

作用 : @Controller+@ResponseBody的作用

2.@Controller

作用 : 注解这个bean是MVC模型中的一个C 会被spring的auto-scan扫到纳入管理

3.@ResponseBody

作用: 将Controller的方法返回的对象通过适当的转换器转换为指定的格式后,写入到response对象的body区,通常用来返回json或xml

@RequestMapping("/login")

  @ResponseBody
  public User login(User user){
    return user;
  }
  User字段:userName pwd
  那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'

4.@Component

作用:在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释

这里就是说把这个类交给Spring管理,重新起个名字叫userManager,由于不好说这个类属于哪个层面,就用@Component

这里就是说把这个类交给Spring管理,重新起个名字叫userManager,由于不好说这个类属于哪个层面,就用@Component

5.@ConfigurationProperties(prefix="spring.redis")

作用:读取springBoot的默认配置文件信息中以spring.redis开头的信息

6.@EnableCaching

作用:当你在配置类(@Configuration)上使用@EnableCaching注解时,会触发一个post processor,这会扫描每一个spring bean,查看是否已经存在注解对应的缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。

7.@Bean

作用:是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里,添加的Bean的id为方法名

@Configuration
public class AppConfig {

    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }

}
相当于xml这样配置
<beans>
    <bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>

8.@EnableRedisHttpSession(maxInactiveIntervalInSeconfds = 86400*30)

作用:将session自动存储到redis里,超时时间通过maxInactiveIntervalInSeconds设置

9.@Modifying 

作用:在 @Query 注解中编写 JPQL 语句, 但必须使用 @Modifying 进行修饰. 以通知 SpringData, 这是一个 UPDATE 或 DELETE 操作

10.@Qualifier 

作用:@Qualifier("XXX") Spring的Bean注入配置注解,该注解指定注入的Bean的名称,Spring框架使用byName方式寻找合格的bean

11.@RabbitListener(queues = "hello")

作用: 监听hello这个队列

例子:

@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {
	@RabbitHandler
	public void process(String hello) {
		System.out.println("Receiver  : " + hello);
	}
}

也可以这样写

作用:当hello这个Queue有消息时,会调用wcbTest方法进行处理

@Component
public class HelloReceiver {
	@RabbitListener(queues = "hello")
	public void wcbTest(String hello) {
		System.out.println("Receiver  : " + hello);
	}
}

12.@RabbitHandler

作用:指定对消息的处理方法

13. @Scheduleduled

作用:控制方法定时执行,其中有三个参数可选择:

1、fixedDelay控制方法执行的间隔时间,是以上一次方法执行完开始算起,如上一次方法执行阻塞住了,那么直到上一次执行完,并间隔给定的时间后,执行下一次。

2、fixedRate是按照一定的速率执行,是从上一次方法执行开始的时间算起,如果上一次方法阻塞住了,下一次也是不会执行,但是在阻塞这段时间内累计应该执行的次数,当不再阻塞时,一下子把这些全部执行掉,而后再按照固定速率继续执行。

3、cron表达式可以定制化执行任务,但是执行的方式是与fixedDelay相近的,也是会按照上一次方法结束时间开始算起。

14.@RefreshScope

作用:动态刷新配置,在需要的类上加上该注解就行

要先引进spring-boot-starter-actuator这个包

使用POST 请求http://localhost:7031/refresh 来刷新配置

注解会生成代理类,如果不识别代理请不要使用

注解作用的类,不能是final类,否则启动时会报错

猜你喜欢

转载自blog.csdn.net/AAA821/article/details/79354393