Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

5.6. Properties

5.6.1. @EnableConfigurationProperties 引用自定義 *.properties 配置檔案

Application.java 涮鍋配置NetkillerProperties.java是 @ComponentScan 掃瞄範圍,可以不用聲明下面註解。

				@EnableConfigurationProperties(NetkillerProperties.class)
			
			
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.Mongo;

import pojo.NetkillerProperties;

@Configuration
@SpringBootApplication
@EnableConfigurationProperties(NetkillerProperties.class)
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan({ "web", "rest" })
@EnableMongoRepositories
public class Application {
	
	@SuppressWarnings("deprecation")
	public @Bean MongoDbFactory mongoDbFactory() throws Exception {
		UserCredentials userCredentials = new UserCredentials("finance", "your_password");
		return new SimpleMongoDbFactory(new Mongo("mdb.netkiller.cn"), "finance", userCredentials);
	}

	public @Bean MongoTemplate mongoTemplate() throws Exception {
		return new MongoTemplate(mongoDbFactory());
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
			
			

NetkillerProperties.java

			
package pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix="netkiller")
public class NetkillerProperties {
	private String name;
	private String email;
	private String home;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getHome() {
		return home;
	}
	public void setHome(String home) {
		this.home = home;
	}
	@Override
	public String toString() {
		return "NetkillerProperties [name=" + name + ", email=" + email + ", home=" + home + "]";
	}
}
			
			

IndexController.java

			
package web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.City;
import pojo.NetkillerProperties;
import repository.CityRepository;

@Controller
public class IndexController {
	
	@Autowired
	private CityRepository repository;

	@Autowired
	private NetkillerProperties propertie;
	
	@RequestMapping("/index")
	@ResponseBody
	public String index() {
	//public ModelAndView index() {

		String message = "Hello";
		//return new ModelAndView("home/welcome", "variable", message);
		return message;
	}
	
	@RequestMapping("/config")
	@ResponseBody
	public String config() {
		return propertie.toString();
	}
}
			
			

src/main/resource/application.properties

			
netkiller.name=Neo
netkiller.email=netkiller@msn.com
netkiller.home=http://www.netkiller.cn
			
			

@ConfigurationProperties 預設配置是 application.properties

你可以通過 locations 指向特定配置檔案

				@ConfigurationProperties(prefix = "message.api",locations = "classpath:config/message.properties")
			

@EnableConfigurationProperties 可以導入多個配置檔案

				@EnableConfigurationProperties({NetkillerProperties.class, NeoProperties.class})
			

5.6.2. spring.profiles.active 參數切換配置檔案

首先我們準備三個配置檔案

				src/main/resource/application-development.properties
				src/main/resource/application-testing.properties
				src/main/resource/application-production.properties
			

使用下面--spring.profiles.active參數切換運行環境配置檔案

				java -jar application.jar --spring.profiles.active=development
				java -jar application.jar --spring.profiles.active=testing
				java -jar application.jar --spring.profiles.active=production
			

分別為三個環境打包

				mvn clean package -Pdevelopment
				mvn clean package -Ptesting
				mvn clean package -Pproduction
			

5.6.3. SpringApplicationBuilder.properties() 方法添加配置項


			
			
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class)
        .properties("spring.config.name=client").run(args);
  }