spring實戰 條件裝配bean

2021-08-06 01:10:45 字數 3323 閱讀 8978

在做大型專案時,我們的系統會有多個執行環境,如開發人員自己的本地環境dev,測試人員的測試環境sit,上線前的預生產環境pre,線上環境prd

在不同環境中我們可能需要不同的配置,如資料庫配置,mq配置等,在不同的環境都有相應的不同的配置,這時候我們需要根據不同的環境來建立不同的配置

強大的spring為我們提供了條件化裝配bean和profile的bean裝配

如下:testmain

package com.halfworlders.test;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.test.context.activeprofiles;

import org.springframework.test.context.contextconfiguration;

import org.springframework.test.context.junit4.springjunit4classrunner;

import com.halfworlders.dao.datasource;

import com.halfworlders.dao.datasourceconfig;

/** * profile的需要兩個引數:spring.profile.active和spring.profile.default

* 有多種方式配置這兩個屬性

* 1,作為dispatcherservlet的初始化函式

* 2,作為web應用的上下文引數

* 3,作為jndi條目

* 4,作為環境變數

* 5,作為jvm的系統屬性

* 6,在整合測試環境上,使用@activeprofiles註解

*/@runwith(springjunit4classrunner.class)

@contextconfiguration(classes=datasourceconfig.class)

@activeprofiles("prd")

public class testmain

}

datasourceconfig

package com.halfworlders.dao;

import org.springframework.context.annotation.configuration;

import org.springframework.context.annotation.import;

@configuration

@import()

public class datasourceconfig

mysqldatasourceconfig

package com.halfworlders.dao;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.context.annotation.profile;

@configuration

public class mysqldatasourceconfig

}

oracledatasourceconfig

package com.halfworlders.dao;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.context.annotation.profile;

@configuration

public class oracledatasourceconfig

}

db2datasourceconfig

package com.halfworlders.dao;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.conditional;

import org.springframework.context.annotation.configuration;

import com.halfworlders.condition.mycondition;

@configuration

public class db2datasourceconfig

}

mycondition

package com.halfworlders.condition;

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 mycondition implements condition

}

datasource
package com.halfworlders.dao;

public inte***ce datasource

public class mysqldatasource implements datasource

public void shutdown()

}public class oracledatasource implements datasource

public void shutdown()

}public class db2datasource implements datasource

public void shutdown()

}

Spring實戰 8 自動裝配

本篇介紹一下自動裝配的知識,spring為了簡化配置檔案的編寫。採用自動裝配方式,自動的裝載需要的bean。自動裝配有以下幾種方式 1 byname 通過id的名字與屬性的名字進行判斷,要保證bean例項中屬性名字與該裝配的id名字相同。2 bytype 通過型別確定裝配的bean,但是當存在多個型...

Conditional 按照條件註冊bean

conditional 通過改註解能判讀根據條件動態建立bean person類 public class person override public string tostring public person string name,int age public string getname p...

spring高階裝配 條件化的bean

conditional註解的使用 bean conditional magicexistscondition.class public magicbean magicbean conditional將會通過condition介面進行條件對比 public inte ce conditionpacka...