SpringBoot注解

1.声明Bean的注解:

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

@Service :

@Repository

@Controller

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

2.注入Bean的注解:

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

@Resource

@Inject

3.基础注解:

@CompentScan :扫描组件路径

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

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

@PropertySource:读取配置文件地址

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

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

@Value("${data.user}")//注入配置文件中的变量privateString user;@Value("1232323")//注入字符串privateString testStr;@Value("#{systemProperties['os.name']}")//注入系统变量privateString osName;@Value("classpath:com/test/aspect/test.txt")//注入文件privateResource resource;@Value("http://www.baidu.com")//注入网站资源privateResource baidu;@Value("#{service.other}")//注入其他组建的变量privateString other;

4.bean生命周期

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

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

5.profile

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

@Profile("dev")//环境为dev时初始化这个Bean@BeanpublicServicedevService(){returnnewService();    }@Profile("test")@BeanpublicServicetestService(){returnnewService();    }

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注解绑定它传过来的值到方法的参数上。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容