Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

5.6. Properties

5.6.1. Application

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			
			
comments powered by Disqus