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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java spring 条件注解_【Spring】Spring高级话题-条件注解-@Condition

發布時間:2025/3/12 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java spring 条件注解_【Spring】Spring高级话题-条件注解-@Condition 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

進行本示例的演示,需要先配置好Maven和Spring哦、

見:

【Spring】基于IntelliJ IDEA搭建Maven

分析

通過profile,我們可以獲得不同的profile,我們可以獲得不同的Bean。Spring4提供了一個更通用的基于條件的Bean的創建,即使用@Condition注解。

@Condition根據滿足某一個特定條件創建一個特定的Bean。

比如說,當某一個jar包在一個類路徑下的時候,自動配置一個或多個Bean;或者只有某個Bean被創建才會創建另外一個Bean。

總的來說,就是根據特定條件來控制Bean的創建行為,這樣我們可以利用這個特性來進行一些自動的配置。

下面這個示例將以不同的操作系統來作為條件,通過實現Condition接口,并重寫其matches方法來構造判斷條件。

若在Windows系統下運行程序,則輸出列表命令為dir;若在Linux操作系統下運行程序,則輸出列表命令為ls。

示例

先需要定義判斷條件

判定Windows的條件

package cn.hncu.p3.p4_conditional;

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:24. * Explain:判斷Windows的條件 */

public class WindowsCondition implements Condition{

@Override

public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){

return context.getEnvironment().getProperty("os.name").contains("Windows");

}

}

判定Linux的條件

package cn.hncu.p3.p4_conditional;

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:28. * Explain:判定Linux的條件 */

public class LinuxCondition implements Condition{

@Override

public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

return context.getEnvironment().getProperty("os.name").contains("Linux");

}

}

不同系統下Bean的類

首先需要一個接口,接下來不同的Bean都需要實現這個接口。

接口

package cn.hncu.p3.p4_conditional;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:31. * Explain:接口-Bean需要實現的接口 */

public interface ListService {

public String showListCmd();

}

Windows下所要創建的Bean的類

package cn.hncu.p3.p4_conditional;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:41. * Explain:Windows下所要創建的Bean的類 */

public class WindowsListService implements ListService {

@Override

public String showListCmd() {

return "dir";

}

}

Linux下所要創建的Bean的類

package cn.hncu.p3.p4_conditional;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:42. * Explain:Linux下所要創建的Bean的類 */

public class LinuxListService implements ListService {

@Override

public String showListCmd() {

return "ls";

}

}

配置類

package cn.hncu.p3.p4_conditional;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Conditional;

import org.springframework.context.annotation.Configuration;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:48. * Explain:配置類 */

@Configuration

public class ConditionConfig {

//matches方法返回true的,就運行哪個方法

@Bean

@Conditional(WindowsCondition.class)//通過@Condition注解,符合Windows條件則實例化windowsListService

public ListService windowsListService(){

return new WindowsListService();

}

@Bean

@Conditional(LinuxCondition.class)//通過@Condition注解,符合Linux條件則實例化linuxListService

public ListService linuxListService(){

return new LinuxListService();

}

}

運行

package cn.hncu.p3.p4_conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/** * Created with IntelliJ IDEA. * User: 陳浩翔. * Date: 2016/12/7. * Time: 下午 7:57. * Explain:運行類 */

public class Main {

public static void main(String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);

ListService listService = context.getBean(ListService.class);

System.out.println(context.getEnvironment().getProperty("os.name")

+"系統下的列表命令為:"

+listService.showListCmd()

);

}

}

運行結果

本文章由[諳憶]編寫, 所有權利保留。

總結

以上是生活随笔為你收集整理的java spring 条件注解_【Spring】Spring高级话题-条件注解-@Condition的全部內容,希望文章能夠幫你解決所遇到的問題。

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