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();
    }
    /*
     * 像有参数connectFactory,若spring容器中只有一个ConnectionFactory 类型的bean
     * 则不论参数取名为何都是按类型取bean ConnectionFactory 为参数,
     * 若有多个则参数取名必须为多个bean中的一个,否则报错。
     */
	@Bean
	public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
		return null;
	}

}
相当于xml这样配置

    

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类,否则启动时会报错

 

15.@Order

作用:定义了组件的加载顺序

@Order(value) value值是整数值,遵循从低到高的顺序,即最低值具有高优先级

它可以定义类的加载顺序

package com.wisely.spring4_2.order;
@Configuration
@Order(2)
public class Demo1Config {
    @Bean
    public Demo1Service demo1Service(){
        System.out.println("demo1config 加载了");
        return new Demo1Service();
    }
}
package com.wisely.spring4_2.order;
@Configuration
@Order(1)
public class Demo2Config {
    @Bean
    public Demo2Service demo2Service(){
        System.out.println("demo2config 加载了");
        return new Demo2Service();
    }
}
运行
public class Main {
    public static void main(String[] args) {
//扫描指定包中的类,刷新ApplicationContext上下文,也就是说这些类就交给spring管理了
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.spring4_2.order");
    }
}
输出结果
demo2config 加载了
demo1config 加载了

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(JAVA基础)