我可以在组件上使用 @Profile 标签而不在 Spring 引导或 Web.xml 中创建 @Configuration class
Can I use @Profile Tags on Components without creating an @Configuration class in Spring Boot or a Web.xml
到目前为止,我已经设法避免为我的 Spring Boot Maven 项目(除了 pom)开发任何 xml 文件,它们都是在编译时生成的,我希望保留通过在 运行 命令中定义我的个人资料,如指定 here.
通过简单地使用@ComponentScan my main class to enable the scanning of components and tagging my DAO as @Repository,我已经成功地自动装配了我的 UserDAOmySQLImpl class(它继承了 UserDAO)。
@Autowired
private UserDAO userDAO;
然后期待在我们使用 db8 实现的 Live 中添加第二个 DAO,我需要应用程序确定需要使用哪个 UserDAO 实现。 Profiles 似乎是答案。
在 some reading 之后,似乎我主要需要添加某种配置 classes 和外部 xml 来管理这些配置文件和组件,尽管这看起来很乱而且我希望没有必要。
我已经被两个实现标记为:
@Repository
@Profile("dev")
public class UserDAOmySQLImpl implements UserDAO {...
-
@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...
然后通过指定的 Maven 目标设置活动配置文件 here 希望这将是一个很好的干净解决方案,这样我就可以分别在我们的开发和实时构建脚本的目标中指定活动配置文件.
我目前的测试目标是:
spring-boot:run -Drun.profiles=dev
但是,在构建时我收到一个错误消息,指出两个 bean 都被拾取了。
*No qualifying bean of type [com.***.studyplanner.integration.UserDAO] is defined: expected single matching bean but found 2: userDAOdb8Impl,userDAOmySQLImpl*
问题
Maven 目标中设置的配置文件是否是使用@Profile 标记时要检查的配置文件?
为什么 Spring 选择这两个实现,如果配置文件没有正确设置,那么肯定不应该选择两个实现?
有什么关于实现我正在寻找的简洁方法的建议吗?
奖金
我真的很想在应用程序中设置配置文件,因为它很容易简单地检查是否存在环境文件来决定使用哪个配置文件,尽管我正在努力复制它(在 Spring Profiles Examples
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//Enable a "live" profile
context.getEnvironment().setActiveProfiles("live");
context.register(AppConfig.class);
context.refresh();
((ConfigurableApplicationContext) context).close();
}
进入我正在处理的应用程序中不寻常的主要 class,我犹豫要不要玩。
public class StudyPlannerApplication {
...
public static void main(String[] args) {
SpringApplication.run(StudyPlannerApplication.class, args);
}
...
}
如有任何帮助,我们将不胜感激。
干杯,
斯科特
愚蠢的我,校对问题我注意到糟糕的复制和粘贴工作意味着两个 DAO 实现都将 @profile 设置为 "Dev"。将 db8 DAO 更改为 @Profile("live") 后,上述工作正常。
因此,在使用 maven 和 spring 引导时,根据配置文件选择存储库实际上非常容易。
1) 确保您的主要 class 具有 @ComponentScan 注释
@ComponentScan
public class StudyPlannerApplication {
2) 根据您希望为它们选择的配置文件使用@Profile 标签标记您的组件
@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...
-
@Repository
@Profile("live")
public class UserDAOdb8Impl implements UserDAO {...
3)
将您的个人资料与您的 maven 目标一起发送
spring-boot:run -Drun.profiles=dev
希望这会对其他人有所帮助,因为我还没有像我在网络其他地方所做的那样看到使用自动装配配置文件的完整示例
到目前为止,我已经设法避免为我的 Spring Boot Maven 项目(除了 pom)开发任何 xml 文件,它们都是在编译时生成的,我希望保留通过在 运行 命令中定义我的个人资料,如指定 here.
通过简单地使用@ComponentScan my main class to enable the scanning of components and tagging my DAO as @Repository,我已经成功地自动装配了我的 UserDAOmySQLImpl class(它继承了 UserDAO)。
@Autowired
private UserDAO userDAO;
然后期待在我们使用 db8 实现的 Live 中添加第二个 DAO,我需要应用程序确定需要使用哪个 UserDAO 实现。 Profiles 似乎是答案。
在 some reading 之后,似乎我主要需要添加某种配置 classes 和外部 xml 来管理这些配置文件和组件,尽管这看起来很乱而且我希望没有必要。
我已经被两个实现标记为:
@Repository
@Profile("dev")
public class UserDAOmySQLImpl implements UserDAO {...
-
@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...
然后通过指定的 Maven 目标设置活动配置文件 here 希望这将是一个很好的干净解决方案,这样我就可以分别在我们的开发和实时构建脚本的目标中指定活动配置文件.
我目前的测试目标是:
spring-boot:run -Drun.profiles=dev
但是,在构建时我收到一个错误消息,指出两个 bean 都被拾取了。
*No qualifying bean of type [com.***.studyplanner.integration.UserDAO] is defined: expected single matching bean but found 2: userDAOdb8Impl,userDAOmySQLImpl*
问题
Maven 目标中设置的配置文件是否是使用@Profile 标记时要检查的配置文件?
为什么 Spring 选择这两个实现,如果配置文件没有正确设置,那么肯定不应该选择两个实现?
有什么关于实现我正在寻找的简洁方法的建议吗?
奖金
我真的很想在应用程序中设置配置文件,因为它很容易简单地检查是否存在环境文件来决定使用哪个配置文件,尽管我正在努力复制它(在 Spring Profiles Examples
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//Enable a "live" profile
context.getEnvironment().setActiveProfiles("live");
context.register(AppConfig.class);
context.refresh();
((ConfigurableApplicationContext) context).close();
}
进入我正在处理的应用程序中不寻常的主要 class,我犹豫要不要玩。
public class StudyPlannerApplication {
...
public static void main(String[] args) {
SpringApplication.run(StudyPlannerApplication.class, args);
}
...
}
如有任何帮助,我们将不胜感激。
干杯, 斯科特
愚蠢的我,校对问题我注意到糟糕的复制和粘贴工作意味着两个 DAO 实现都将 @profile 设置为 "Dev"。将 db8 DAO 更改为 @Profile("live") 后,上述工作正常。
因此,在使用 maven 和 spring 引导时,根据配置文件选择存储库实际上非常容易。
1) 确保您的主要 class 具有 @ComponentScan 注释
@ComponentScan
public class StudyPlannerApplication {
2) 根据您希望为它们选择的配置文件使用@Profile 标签标记您的组件
@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...
-
@Repository
@Profile("live")
public class UserDAOdb8Impl implements UserDAO {...
3) 将您的个人资料与您的 maven 目标一起发送
spring-boot:run -Drun.profiles=dev
希望这会对其他人有所帮助,因为我还没有像我在网络其他地方所做的那样看到使用自动装配配置文件的完整示例