1.聲明Bean的注解:
@Compent :用來標識組件,可以被掃描到
@Service :
@Repository
@Controller
@Bean : 注解到方法上,表示方法返回一個Bean,全局配置使用Java配置,如數(shù)據(jù)庫配置,業(yè)務(wù)配置使用上述的幾個注解
2.注入Bean的注解:
@Autowired 可以注解到set方法或?qū)傩陨?/p>
@Resource
@Inject
3.基礎(chǔ)注解:
@CompentScan :掃描組件路徑
@Configuration:說明這個類是配置文件
@scope:用來聲明Bean的Scope,有singleton,prototype,session,request。默認
@PropertySource:讀取配置文件地址
@ConfigurationProperties(prefix) 配置文件映射對象
@Value :作用于變量或方法上,將配置文件中的變量設(shè)置到該變量上
@Value("${data.user}") //注入配置文件中的變量
private String user;
@Value("1232323") //注入字符串
private String testStr;
@Value("#{systemProperties['os.name']}") //注入系統(tǒng)變量
private String osName;
@Value("classpath:com/test/aspect/test.txt") //注入文件
private Resource resource;
@Value("http://www.baidu.com") //注入網(wǎng)站資源
private Resource baidu;
@Value("#{service.other}") //注入其他組建的變量
private String other;
4.bean生命周期
@PostContruct 在構(gòu)造器后執(zhí)行,相當(dāng)于 @Bean 的 initMethod
@PreDestroy : 在銷毀前執(zhí)行 ,相當(dāng)于@Bean的destroyMethod
5.profile
@Profile(value="") 為 在不同的環(huán)境中使用不同的配置提供了支持;例如數(shù)據(jù)庫的配置
@Profile("dev") //環(huán)境為dev時初始化這個Bean
@Bean
public Service devService(){
return new Service();
}
@Profile("test")
@Bean
public Service testService(){
return new Service();
}
6.定時任務(wù)
@EnableScheduing 開啟定時任務(wù)支持
@Scheduled 有三個參數(shù),corn ,fixedDelay , fixedRate 后面?zhèn)z個單位為毫秒
7.測試
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes ={Application.class})
@ActiveProfiles("test")
8.SpringMvc
@requestBody:該注解常用來處理Content-Type: 不是application/x-www-form-urlencoded編碼的內(nèi)容,例如application/json, application/xml等;
@RequestHeader:獲取header中的值
@CookieValue:獲取cookie中的值
@Pathvariable :當(dāng)使用@RequestMapping URI template 樣式映射時, 即 someUrl/{paramId}, 這時的paramId可通過 @Pathvariable注解綁定它傳過來的值到方法的參數(shù)上。