日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【Java Web开发学习】Spring4条件化的bean

發布時間:2024/4/14 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java Web开发学习】Spring4条件化的bean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【Java Web開發學習】Spring4條件化的bean

轉載:https://www.cnblogs.com/yangchongxing/p/9071960.html

Spring4引入了@Conditional注解,可以用到帶有@Bean注解的地方,若給定的條件計算為true則創建bean,否則bean被忽略

用法:@Conditional(PlayerCondition.class),設置為@Conditional注解的類必須是任意實現了org.springframework.context.annotation.Condition接口的類。該接口只有一個方法

public boolean matches(ConditionContext context, AnnotatedTypeMetadata matadata) {},返回boolean

package cn.ycx.web.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration;import cn.ycx.web.model.Painter; import cn.ycx.web.model.PainterCondition; import cn.ycx.web.model.Player; import cn.ycx.web.model.PlayerCondition; @Configuration public class TestConfig {@Bean@Conditional(PlayerCondition.class)public Player player() {return new Player();}@Bean@Conditional(PainterCondition.class)public Painter painter() {return new Painter();} } package cn.ycx.web.model;import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata;public class PlayerCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata matadata) {Environment env = context.getEnvironment();return env.containsProperty("CONDITION_PALY");} } package cn.ycx.web.model;import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata;public class PainterCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata matadata) {Environment env = context.getEnvironment();return env.containsProperty("CONDITION_PAINT");} }

環境變量包含CONDITION_PALY就創建PlayerCondition

環境變量設置:

windows系統

spring.CONDITION_PALY=play

ubuntu系統

當前用戶

$ vim ~/.bashrc 追加 export CONDITION_PALY=play $ source ~/.bashrc

所有用戶

# vim /etc/profile 追加 export CONDITION_PALY=play # source /etc/profile

轉載于:https://www.cnblogs.com/yangchongxing/p/9071960.html

總結

以上是生活随笔為你收集整理的【Java Web开发学习】Spring4条件化的bean的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。