Automatic injection configured during spring assembly

configuration file

 

Automatic injection configured during spring assembly

 

 

1. Spring needs special classes to load the configuration files of this directory (manual configuration is required)

  1.1 used in java

Use factorybean way

 

<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="classpath:db.properties"/>

 

@Value("#{prop['server.cn']}")

private String cnWebUrl;

@RequestMapping(value = "/addConObj")

public String addConObj(Model model) {

 

String language = CASUtil.getCustomer().getLanguage();

 

if ("CN".equals(language)) {

return "redirect:" +cnWebUrl + "/offer/addConObj";

}

}

 

db.properties

server.cn=http://10.0.1.222:8080/web

server.en=http://10.0.1.214:8080/web

 

1.2 Used in configuration files

 

 

<context:property-placeholder location="classpath:db.properties" />

 

This applies to the use of wildcards in the configuration file

 

 

 

 

2, springboot automatically loads the configuration file in the corresponding directory (the loading class is automatically configured)

 

 

 

@Configuration

@ConfigurationProperties

@PropertySource("classpath:paprocesser.properties") //Only applicable to .properties, not yml

public class ProcesserProperty extends Properties{

 

}

 

 

 

//or

@ConfigurationProperties(prefix = "foo")

@Bean

public FooComponent fooComponent() {

...

}

 

 

paprocesser.propertie

1310=inMoneyProcesser

 

 

//Obtain

String processerBeanName = processerProperty.getProperty("1310");

 

 

3. Take it directly with @Value("${pa.url}")

 

 

@Value("${pa.url}")

private String paUrl ;

 

/////////////With the get and set methods for external calls, take the configuration

 

If springboot has introduced this property loading class, you can directly

@Component

public class CasProperties {

  @Value("${cas.server.host.url}")  

   private String casServerUrl;  

 

   @Value("${cas.server.host.login_url}")  

   private String casServerLoginUrl;  

 

   @Value("${cas.server.host.logout_url}")  

   private String casServerLogoutUrl;  

 

   @Value("${app.server.host.url}")  

   private String appServerUrl;  

 

   @Value("${app.login.url}")  

   private String appLoginUrl;  

 

   @Value("${app.logout.url}")  

   private String appLogoutUrl;

 

public String getCasServerUrl() {

return casServerUrl;

}

 

public void setCasServerUrl(String casServerUrl) {

this.casServerUrl = casServerUrl;

}

 

public String getCasServerLoginUrl() {

return casServerLoginUrl;

}

 

public void setCasServerLoginUrl(String casServerLoginUrl) {

this.casServerLoginUrl = casServerLoginUrl;

}

 

public String getCasServerLogoutUrl() {

return casServerLogoutUrl;

}

 

public void setCasServerLogoutUrl(String casServerLogoutUrl) {

this.casServerLogoutUrl = casServerLogoutUrl;

}

 

public String getAppServerUrl() {

return appServerUrl;

}

 

public void setAppServerUrl(String appServerUrl) {

this.appServerUrl = appServerUrl;

}

 

public String getAppLoginUrl() {

return appLoginUrl;

}

 

public void setAppLoginUrl(String appLoginUrl) {

this.appLoginUrl = appLoginUrl;

}

 

public String getAppLogoutUrl() {

return appLogoutUrl;

}

 

public void setAppLogoutUrl(String appLogoutUrl) {

this.appLogoutUrl = appLogoutUrl;

}  

   

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326326894&siteId=291194637