配置類相關:
@PropertySource(value = "classpath:test.properties") //我們都把配置文件寫到application.yml中。有時我們不願意把配置都寫到application配置文件中,這時需要我們自定義配置文件,比如test.properties:
@ConfigurationProperties(prefix = "com.forezp") //在配置文件中的前綴,屬性自動注入
@EnableConfigurationProperties //@ConfigurationProperties注解主要用來把properties配置文件轉化為bean來使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是獲取不到properties配置文件轉化的bean的。一般不用加,因為在我們的的appliacation的啟動類@SpringBootApplication注解中已經包含了@ComponentScan和@EnableConfigurationProperties注解。
@EntityScan("com.ihrm") 實體類掃描包
@SpringBootApplication(scanBasePackages = "com.ihrm") 啟動類掃描
@ComponentScan(basePackages={"com.xuecheng.manage_cms"})//掃描本項目下的所有類
自動配置 (Spring-boot-autoconfigure包下的)
https://www.jianshu.com/p/68a75c093023
@ConditionalOnBean 配置了某個特定bean (僅僅在當前上下文中存在某個對象時,才會實例化一個Bean)
@ConditionalOnBean(Xxxx.class) 僅僅在當前上下文中存在Xxxx對象時,才會實例化一個Bean 也就是 只有當Xxxx.class 在spring的applicationContext中存在時 這個當前的bean才能夠創建
------------------第二種用法:用name來指定
@ConditionalOnBean(name="redisTemplate")
@ConditionalOnMissingBean 沒有配置特定bean (僅僅在當前上下文中不存在某個對象時,才會實例化一個Bean)
@ConditionalOnMissingBean(Xxxx.class) 當前上下文中不存在Xxxx對象時,才會實例化一個Bean 也就是 只有當Xxxx.class 在spring的applicationContext中不存在時 這個當前的bean才能夠創建
@ConditionalOnClass (xxx.class) (當xxx.class存在時,才會實例化一個Bean)
@ConditionalOnMissingClass (當xxx.class不存在時,才會實例化一個Bean)
@ConditionalOnProperty 給定配置屬性中包含某個值
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @Conditional(OnPropertyCondition.class) public @interface ConditionalOnProperty { String[] value() default {}; //數組,獲取對應property名稱的值,與name不可同時使用 String prefix() default "";//property名稱的前綴,可有可無 String[] name() default {};//數組,property完整名稱或部分名稱(可與prefix組合使用,組成完整的property名稱),與value不可同時使用 String havingValue() default "";//可與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置 boolean matchIfMissing() default false;//缺少該property時是否可以加載。如果為true,沒有該property也會正常加載;反之報錯 boolean relaxedNames() default true;//是否可以松散匹配,至今不知道怎么使用的 } }
如果該值為空,則返回false;
如果值不為空,則將該值與havingValue指定的值進行比較,如果一樣則返回true;否則返回false。
如果返回值為false,則該configuration不生效;為true則生效。
@ConditionalOnProperty(prefix = "guns", name = "spring-session-open", havingValue = "true")
@ConditionalOnResource classpath有指定資源
當指定的資源文件出現在classpath中生效 @Bean @ConditionalOnResource(resources="classpath:shiro.ini") protected Realm iniClasspathRealm()
@ConditionalOnWebApplication 是一個web應用程序
@ConditionalOnNotWebApplication 不是一個web應用程序