spring boot讀取配置檔案
package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.env.environment;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class configcontroller
public string getconfigenvironment()
}
2)通過@configurationproperties配合@propertysource讀取配置檔案裡的配置資訊。
package com.ivan.config.entity;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.propertysource;
@configuration
@propertysource("classpath:config/druid.properties")
@configurationproperties(prefix = "druid")
public class druidconfig
public void setinitialsize(int initialsize)
public int getminidle()
public void setminidle(int minidle)
public int getmaxactive()
public void setmaxactive(int maxactive)
public int getmaxwait()
public void setmaxwait(int maxwait)
public string getvalidationquery()
public void setvalidationquery(string validationquery)
public boolean istestwhileidle()
public void settestwhileidle(boolean testwhileidle)
public boolean istestonborrow()
public void settestonborrow(boolean testonborrow)
public boolean istestonreturn()
public void settestonreturn(boolean testonreturn)
@override
public string tostring()
}
2:對應的配置檔案:
druid.initialsize=5
druid.minidle=5
druid.maxactive=20
druid.maxwait=60000
druid.validationquery=select 'x'
druid.testwhileidle=true
druid.testonborrow=true
druid.testonreturn=true
3:在需要用到的類通過@autowired注入
package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.druidconfig;
@restcontroller
public class druidconfigcontroller
}
3)通過@value註解
1:需要得到配置屬性的類如下,可以在任何需要得到配置的地方用@value註解
package com.ivan.config.entity;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.configuration;
@configuration
public class valuetest ")
private string username;
public string getusername()
public void setusername(string username)
}
2:測試controller類通過@autowired注入實體類
package com.ivan.config.controller;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;
import com.ivan.config.entity.valuetest;
@restcontroller
public class valuecontroller
}
5分鐘學會springboot讀取配置檔案
本文是基於hadoop ha基礎搭建下storm集群的搭建過程 之前文章有搭建過程 搭建過程超級簡單如下 2 安裝zookeeper集群 準備三颱機器,分別為weekend01,weekend02,weekend03,並在這三颱機器上安裝zookeeper,我們現在weekend01上安裝,然後zo...
spring boot 配置檔案讀取
如圖所示,可以重新賦值予以覆蓋。mail setting 設定郵箱主機 email.host smtp.163.com email.port 25 設定使用者名稱 email.from xx xx.com 設定密碼 email.frompassword 設定是否需要認證,如果為true,那麼使用者名...
springBoot讀取配置檔案
1.讀核心配置檔案 2.讀自定義配置檔案 建立乙個配置檔案對應的實體類新增如下註解 configuration propertysource value classpath mail.properties configurationproperties prefix mail locations c...