springboot 注解

1.声明Bean的注解:

@Compent :用来标识组件,可以被扫描到

@Service : 

@Repository

@Controller

@Bean : 注解到方法上,表示方法返回一个Bean,全局配置使用Java配置,如数据库配置,业务配置使用上述的几个注解

              具有initMedhod和destroyMethod属性,用来指定Bean创建时和销毁时执行的方法

2.注入Bean的注解:

@Autowired    可以注解到set方法或属性上

@Resource

@Inject

3.基础注解:

@CompentScan :扫描组件路径

@Configuration:说明这个类是配置文件

@scope:用来声明Bean的Scope,有singleton,prototype,session,request。默认

@PropertySource:读取配置文件地址

@ConfigurationProperties(prefix)  配置文件映射对象

//@ConfigurationProperties 有俩种用法:

//例如:配置文件为 hello.name=123

@Data
@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String name;
}


或者使用:
@Data
public class HelloProperties {
    private String name;
}

@Configuration
public class HelloConfig {

    @Bean
    @ConfigurationProperties(prefix = "hello")
    public HelloProperties helloProperties(){
        return new HelloProperties();
    }
}

@Value :作用于变量或方法上,将配置文件中的变量设置到该变量上

 

   @Value("${data.user}")   //注入配置文件中的变量
    private String user;

    @Value("1232323")      //注入字符串
    private String testStr;

    @Value("#{systemProperties['os.name']}")   //注入系统变量
    private String osName;

    @Value("classpath:com/test/aspect/test.txt")   //注入文件
    private Resource resource;

    @Value("http://www.baidu.com")   //注入网站资源
    private Resource baidu;

    @Value("#{service.other}")   //注入其他组建的变量
    private String other;

    资源使用Resource,不然会被当成普通字符串
    只有注入配置文件变量时,使用的是$,其他的是#

 

4.bean生命周期

@PostContruct  在构造器后执行,相当于 @Bean 的 initMethod

@PreDestroy : 在销毁前执行  ,相当于@Bean的destroyMethod

 

5.profile

@Profile(value="") 为 在不同的环境中使用不同的配置提供了支持;例如数据库的配置

    @Profile("dev")   //环境为dev时初始化这个Bean
    @Bean
    public Service devService(){
        return new Service();
    }

    @Profile("test")
    @Bean
    public Service testService(){
        return new Service();
    }

参考 : 多环境配置  ,多数据源配置

6.定时任务

@EnableScheduing 开启定时任务支持

@Scheduled  有三个参数,corn  ,fixedDelay , fixedRate  后面俩个单位为毫秒

7.测试


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes ={Application.class})
@ActiveProfiles("test")

8.SpringMvc

 

@requestBody:该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;

@RequestHeader:获取header中的值

@CookieValue:获取cookie中的值

@Pathvariable :当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值