我有两个 bean 。两者都实现了邮寄功能。一个只有在部署到应用程序服务器时才能工作。另一个用于测试。
我们有每个开发者和环境的资料。我只想在实际测试时连接测试 bean。不测试时应使用另一个 bean。我该如何存档?
@Component
@Profile("localtest")
public class OfflineMail implements Mailing {}
解决方法:
使用“默认”我在某处读到过这个,但是对于像“dev”这样的配置文件似乎没有退回到“默认”的方法:
@Component
@Profile("default")
public class OnlineMail implements Mailing {}
-> 没有找到用于连接的 bean 的异常。
省略个人资料:
@Component
public class OnlineMail implements Mailing {}
-> 在运行“localtest”配置文件时抛出一个独特的异常。
添加所有配置文件:
@Component
@Profile("prod")
@Profile("integration")
@Profile("test")
@Profile("dev1")
@Profile("dev2")
@Profile("dev3")
...
public class OnlineMail implements Mailing {}
这实际上是有效的,但是我们的开发人员没有编号他们使用“dev
使用@Profile("!localtest") 之类的方法似乎效果不佳。
有没有人知道更好的方法来获得“如果没有找到特定的 bean,默认情况下连线”?
最佳答案
我终于找到了一个简单的解决方案。
在线邮件默认是有线的。
@Component
public class OnlineMail implements Mailing {}
使用 @Primary
注释,离线邮件优先于 OnlineMail 并避免 Unique 异常。
@Component
@Profile("localtest")
@Primary
public class OfflineMail implements Mailing {}
关于java - 使用配置文件的 Spring 默认接线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224827/