尽量全都小写支持松散绑定(小写数字下划线)@Value不支持松散绑定
注入的三种方式
同一个Bean只能有一个注入,多次注入会报错(Bean是单例的)
@Component注入1,config类
@Component//加载配置属性必须先要受spring管控,先注入bean@Data//加载配置属性必须要有set@ConfigurationProperties(prefix = "aliyossphoto")public class OSSphotoConfig { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName;}
2,启动类
可以直接 run.getBean(OSSphotoConfig.class)
@SpringBootApplicationpublic class BootAjaxtestApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);System.out.println(bean);}
@Bean + @ConfigurationProperties1,config类不能注入@Component
@Data//加载配置属性必须要有setpublic class OSSphotoConfig { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName;}
2,启动类中需要@Bean注入,至于@ConfigurationProperties(prefix = “”)
写在哪都可以,只要在config类或者启动类中有绑定即可
@SpringBootApplicationpublic class BootAjaxtestApplication {@Bean@ConfigurationProperties(prefix = "aliyossphoto")public OSSphotoConfig getbean(){OSSphotoConfig osSphotoConfig = new OSSphotoConfig();return osSphotoConfig;}public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);System.out.println(bean);}
@EnableConfigurationProperties({class,class})这个注解可以清晰的看见到底注入了什么Bean,同时与@Component冲突,也与@Bean冲突;不过需要在config类中进行配置绑定{}中利用反射,可以同时注入多个Bean
1,config类
@ConfigurationProperties(prefix = "aliyossphoto")@Data//加载配置属性必须要有setpublic class OSSphotoConfig { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName;}
2,启动类中
@EnableConfigurationProperties({OSSphotoConfig.class})@SpringBootApplicationpublic class BootAjaxtestApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);System.out.println(bean);}}
Bean的操作run.getBean(xxx.class)获得的对象由于@Data的存在可以调用get set方法获取属性值除了用get方法还可以用@Value("${xx.xx}")在主类,测试类,MVC层都可以直接@Autowired获取,但在普通类需要使用工具类 工具类实现ApplicationContextAware接口
任意找一个包下创建一个工具类,这个工具类可以通过反射获取bean,不需要通过@Autowired或@Bean即可在普通类中使用bean
并且这个工具类是通用的
使用的时候直接SpringContext.getBean(xxx.class)获取bean即可
@Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { applicationContext = context; } //静态加载applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过反射获取Bean public static
@XxxxxUnit
属性校验validation+validator 1,maven坐标,二者缺一不可
1,1 JSR303规范
1,2 Hibernate validator属性校验器
2,开启属性注入校验@Validated+@Max等
@ConfigurationProperties(prefix = "aliyossphoto")@Data//加载配置属性必须要有set@Validatedpublic class OSSphotoConfig { @Max(value = 200, message = "最大值不超过200") @Min(value = 0 , message = "最小值不能低于0") private Integer age;}
校验规则有很多,有具体需要的时候搜一下就好