设置有效的个人资料,例如context.getEnvironment().setActiveProfiles( "DEV" );
这可以通过使用
public class SpringWebInitializer implements WebApplicationInitializer
{
public void onStartup( final ServletContext servletContext ) throws ServletException
{
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.getEnvironment().setActiveProfiles("DEV" )
}
}
但是在扩展AbstractAnnotationConfigDispatcherServletInitializer时。 我们如何才能设置有效的个人资料?
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
}
答案 0 :(得分:5)
使用class LanguageQuiz(models.Model):
name = models.CharField(max_length=40)
language = models.OneToOneField(sfl_models.Language)
max_questions = models.IntegerField()
def __str__(self):
return '{} test'.format(self.name)
def __unicode__(self):
return self.__str__()
class Question(models.Model):
language_quiz = models.ForeignKey(LanguageQuiz,related_name='questions')
text = models.TextField()
def get_answers_list(self):
return self.answers.all()
class Answer(models.Model):
question = models.ForeignKey(Question,related_name='answers')
text = models.TextField()
correct = models.BooleanField()
属性激活您的个人资料。
spring.profiles.active
答案 1 :(得分:1)
您有几个选择..
您可以尝试使用上下文初始化程序从类路径上的属性文件加载弹簧配置文件,如下所示:
public class ContextProfileInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(ContextProfileInitializer.class);
private static final String DEFAULT_SPRING_PROFILE = "local";
@Override
public void initialize(final ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:conf/application.properties"));
if (environment.getProperty("spring.profiles.active") == null) {
environment.setActiveProfiles(DEFAULT_SPRING_PROFILE);
}
LOGGER.info("Activated Spring Profile: " + environment.getProperty("spring.profiles.active"));
} catch (IOException e) {
LOGGER.error("Could not find properties file in classpath.");
}
}
}
以下是一些提供更多信息的指南:
https://gist.github.com/rponte/3989915
http://www.java-allandsundry.com/2014/09/spring-webapplicationinitializer-and.html
或者(更简单的方法!)使用Spring Boot。
您只需在类路径中的spring.profiles.active
文件中定义application.properties
即可。这将自动被选中并加载到您的环境中。
更多信息:
答案 2 :(得分:0)
您可以对某些@ActiveProfiles("DEV")
课程使用@Configuration
,但可能更有用的是从外部传递个人资料 - 只需使用.jar
之类的附加参数运行您的-Dspring.active.profiles=DEV
答案 3 :(得分:0)
我认为应该是:-Dspring.profiles.active = ...(春季4.1.5)