我可以在组件上使用@Profile标记而不在Spring Boot或Web.xml中创建@Configuration类吗?

斯科特·艾伦

我已经成功地避免迄今我的春节,引导Maven项目(除了POM)与他们所有的开发任何XML文件的编译生成,我希望通过运行命令中定义我的配置,如为保持这种方式在这里

通过简单地使用@ ComponentScan我的主类来启用组件的扫描并将DAO标记为@ Repository,我成功地设法自动装配了UserDAOmySQLImpl类(继承了UserDAO)。

    @Autowired
    private UserDAO userDAO;

然后期待在Live中使用db8实现时添加第二个DAO,我需要该应用程序来确定需要使用哪个UserDAO实现。配置文件似乎是答案。

经过一番阅读之后,似乎大多数情况下我需要添加某种配置类和外部xml来管理这些配置文件和组件,尽管这看起来很杂乱,我希望这是不必要的。

我已经用两种实现方式进行了标记:

@Repository
@Profile("dev")
public class UserDAOmySQLImpl implements UserDAO {...

--

@Repository
@Profile("dev")
public class UserDAOdb8Impl implements UserDAO {...

然后通过此处指定的Maven目标设置活动配置文件,希望这将是一个不错的解决方案,因此我可以分别在目标中为我们的开发脚本和实时构建脚本指定活动配置文件。

我当前的测试目标是:

  spring-boot:run -Drun.profiles=dev

但是,在构建时,我收到两个豆都被拾取的错误。

 *No qualifying bean of type [com.***.studyplanner.integration.UserDAO] is defined: expected single matching bean but found 2: userDAOdb8Impl,userDAOmySQLImpl*

问题

  • 使用@Profile标记时,是否要对照Maven目标中设置的配置文件?

  • 如果未正确设置概要文件,则为什么不选择两个实现,为什么Spring会选择两个实现?

  • 关于实现我正在寻找的一种很好的干净方法的任何建议吗?

奖金

我真的很想在应用程序内设置配置文件,因为很容易检查是否存在环境文件来决定要使用哪个配置文件,尽管我正在努力复制它(可在Spring Profiles示例中找到)

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();
    }

进入我正在开发的应用程序中不寻常的主类,我很犹豫要如何处理。

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

任何帮助将不胜感激。

干杯,斯科特。

斯科特·艾伦

愚蠢的我,证明阅读了我发现的问题的证据是,糟糕的复制和粘贴工作意味着两个DAO实现都将@profile设置为“ Dev”。将db8 DAO更改为@Profile(“ live”)之后,以上方法可以正常工作。

因此,在使用Maven和Spring Boot时,基于配置文件选择存储库实际上非常容易。

1)确保您的主类具有@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

希望这会对其他人有所帮助,因为我还没有像在网络上其他地方那样看到使用自动装配配置文件的完整示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Spring Boot创建RESTful Web服务

来自分类Dev

我可以使用REST Web服务和Spring Boot将Servlet中的应用程序转换为Spring吗?

来自分类Dev

Spring Boot转换web.xml侦听器

来自分类Dev

Spring Boot启用<async-supported>,如web.xml

来自分类Dev

如何在Spring-boot中添加静态Web内容

来自分类Dev

在Spring Boot Web应用程序中记录HTTP流量

来自分类Dev

Spring Boot Web应用程序中的角路由

来自分类Dev

我可以使用PropertiesLauncher启动Spring Boot WAR吗?

来自分类Dev

我可以在Spring Boot中使用MessageBodyReader吗?

来自分类Dev

如果我不在 DB 上执行 CRUD 操作,我应该使用 Web 服务吗?

来自分类Dev

未在 Spring Boot 应用程序的 WEB-INF 目录中创建“资源”文件夹

来自分类Dev

.war文件中带有web.xml的Spring Boot应用

来自分类Dev

@ComponentScan在Spring Boot AutoConfiguration类中不起作用吗?

来自分类Dev

我们可以使用Studio Studio组件来创建独立的HTML5 Web应用程序吗?

来自分类Dev

我们可以使用Studio Studio组件来创建独立的HTML5 Web应用程序吗?

来自分类Dev

为什么我的Spring Boot Web应用程序不能在Gradle中完全运行?

来自分类Dev

在spring-boot中删除Web API之一上的Auth过滤器

来自分类Dev

Spring Boot IntegrationTest Web配置

来自分类Dev

Spring Boot Configuration跳过在多个@Profile上的注册

来自分类Dev

使用web.xml配置spring-boot应用程序

来自分类Dev

如何使用Spring Shell在Spring Boot Web应用程序中构建控制台命令?

来自分类Dev

是否已将类'org.springframework.boot.autoconfigure.web.HttpMapperProperties'标记为已弃用?

来自分类Dev

我可以在Inno Setup中创建自己的类或单元吗?

来自分类Dev

我可以在Typescript中创建一个实现接口的类,并且仍然可以在其上使用装饰器吗?

来自分类Dev

Spring-Boot 无法为 @Configuration 类创建的 bean 列表执行 @Schedule 任务

来自分类Dev

为什么在spring-boot中必须为Web应用程序提供单独的@Controller类文件?

来自分类Dev

使用MVC的Spring Boot SOAP Web服务

来自分类Dev

如何为部署在 aws ec2 实例中的 spring boot rest api web 服务创建 IAM 角色

来自分类Dev

如何从SOAP CXF web.xml配置迁移到Spring-Boot?

Related 相关文章

  1. 1

    使用Spring Boot创建RESTful Web服务

  2. 2

    我可以使用REST Web服务和Spring Boot将Servlet中的应用程序转换为Spring吗?

  3. 3

    Spring Boot转换web.xml侦听器

  4. 4

    Spring Boot启用<async-supported>,如web.xml

  5. 5

    如何在Spring-boot中添加静态Web内容

  6. 6

    在Spring Boot Web应用程序中记录HTTP流量

  7. 7

    Spring Boot Web应用程序中的角路由

  8. 8

    我可以使用PropertiesLauncher启动Spring Boot WAR吗?

  9. 9

    我可以在Spring Boot中使用MessageBodyReader吗?

  10. 10

    如果我不在 DB 上执行 CRUD 操作,我应该使用 Web 服务吗?

  11. 11

    未在 Spring Boot 应用程序的 WEB-INF 目录中创建“资源”文件夹

  12. 12

    .war文件中带有web.xml的Spring Boot应用

  13. 13

    @ComponentScan在Spring Boot AutoConfiguration类中不起作用吗?

  14. 14

    我们可以使用Studio Studio组件来创建独立的HTML5 Web应用程序吗?

  15. 15

    我们可以使用Studio Studio组件来创建独立的HTML5 Web应用程序吗?

  16. 16

    为什么我的Spring Boot Web应用程序不能在Gradle中完全运行?

  17. 17

    在spring-boot中删除Web API之一上的Auth过滤器

  18. 18

    Spring Boot IntegrationTest Web配置

  19. 19

    Spring Boot Configuration跳过在多个@Profile上的注册

  20. 20

    使用web.xml配置spring-boot应用程序

  21. 21

    如何使用Spring Shell在Spring Boot Web应用程序中构建控制台命令?

  22. 22

    是否已将类'org.springframework.boot.autoconfigure.web.HttpMapperProperties'标记为已弃用?

  23. 23

    我可以在Inno Setup中创建自己的类或单元吗?

  24. 24

    我可以在Typescript中创建一个实现接口的类,并且仍然可以在其上使用装饰器吗?

  25. 25

    Spring-Boot 无法为 @Configuration 类创建的 bean 列表执行 @Schedule 任务

  26. 26

    为什么在spring-boot中必须为Web应用程序提供单独的@Controller类文件?

  27. 27

    使用MVC的Spring Boot SOAP Web服务

  28. 28

    如何为部署在 aws ec2 实例中的 spring boot rest api web 服务创建 IAM 角色

  29. 29

    如何从SOAP CXF web.xml配置迁移到Spring-Boot?

热门标签

归档