spring + maven上有应用程序
我想要通过命令行传递的不同配置文件具有不同的application.properties。
我的数据库配置是:
import org.apache.log4j.Logger;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"data"})
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = {"data.persistence"})
public class PersistenceConfig {
private static final String PROP_DATABASE_DRIVER = "db.driver";
private static final String PROP_DATABASE_URL = "db.url";
private static final String PROP_DATABASE_USERNAME = "db.username";
private static final String PROP_DATABASE_PASSWORD = "db.password";
private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";
@Resource
private Environment env;
protected final Logger log = Logger.getLogger(getClass());
...
}
如果我想对不同的服务器(qa和at)使用不同的配置文件,则需要创建这样的两个类,并将标记为@Profile(“ qa”)和@Profile(“ at”)
是否存在创建两个application.properties,将参数传递到命令行并在@(PropertySource)路径中插入qa或application.properties的方法?
是否可以在启动应用程序之前将application.properties文件名插入@PropertySource中?
答案 0 :(得分:1)
在Spring Boot中,您可以使用个人资料名称创建多个应用程序属性(或yaml)文件:
application-qa.properties
application-at.properties
每个人都有特定于该环境的属性。然后只需将--spring.profiles.active=qa
传递到您的应用程序,就只会加载相应的属性文件。这些使您不必维护多个Java类。