springboot實現多環境配置
@configuration
public class datasourceconfig
@bean(name = "secondarydatasource")
@qualifier("secondarydatasource")
@primary
@configurationproperties(prefix="spring.datasource.secondary")
public datasource secondarydatasource()
}
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.driver
對jdbctemplate的支援比較簡單,只需要為其注入對應的datasource即可,如下例子,在建立jdbctemplate的時候分別注入名為primarydatasource
和secondarydatasource
的資料來源來區分不同的jdbctemplate。
@bean(name = "primaryjdbctemplate")
public jdbctemplate primaryjdbctemplate(
@qualifier("primarydatasource") datasource datasource)
@bean(name = "secondaryjdbctemplate")
public jdbctemplate secondaryjdbctemplate(
@qualifier("secondarydatasource") datasource datasource)
接下來通過測試用例來演示如何使用這兩個針對不同資料來源的jdbctemplate。
@runwith(springjunit4classrunner.class)
@autowired
@qualifier("primaryjdbctemplate")
protected jdbctemplate jdbctemplate1;
@autowired
@qualifier("secondaryjdbctemplate")
protected jdbctemplate jdbctemplate2;
@before
public void setup()
@test
public void test() throws exception
}
java springboot常用註解
表示事物唯讀 transactional readonly true 表示這是個service service playerservice 注入playerservice或者呼叫 dao的impl類 autowired api注釋 apioperation value login notes 登入 ...
java SpringBoot上傳附件
uploadpath c images spring.mvc.static path pattern spring.resources.static locations classpath meta inf resources classpath resources classpath static...
JAVA Spring boot相關技巧
1.註冊多例項。scope prototype 2.手工方式獲取註冊的例項。autowired private servletcontext servletcontext private t resolve classtype 3.多個同名類處理。註冊加別名,如 service abc 實現直接 a...