有没有办法在不使用 spring boot 的情况下在普通 spring web mvc 应用程序中启用 yaml 配置(而不是属性文件)并支持 @Value 注释。
我搜索了很多,到处都使用弹簧靴。我对使用 spring boot 不感兴趣,因为它非常重,并且为自动设置配置了许多依赖项。
任何帮助,将不胜感激......
我想使用goolge/guice根据我提供的类注释注入一个值.
AutoConfig注释
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.FIELD })
public @interface AutoConfig {
// default null not possible
Class<? extends Provider<? extends ConfigLoader<?>>> provider() default XMLAutoConfigProvider.class;
}
Run Code Online (Sandbox Code Playgroud)
这是我的注释,它允许配置应该用于带注释字段的配置类型.
用例:
@AutoConfig()
ConfigLoader<?> defaultConfig;
@AutoConfig(provider = JsonConfigProvider)
ConfigLoader<?> jsonConfig;
Run Code Online (Sandbox Code Playgroud)
我想要两个配置,一个默认/ xml一个和一个json.它们可能永远不会同时出现在同一个类中.但我不知道何时使用这一个或另一个.我在类中使用了这个方法,因为它们是由一些依赖项/库提供的,这个注释将用于一些(可插入的)子模块.
MyGuiceModule
public class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(new TypeLiteral<ConfigLoader<?>>() {})
.annotatedWith(AutoConfig.class)
.toProvider(autoConfig.provider());
}
}
Run Code Online (Sandbox Code Playgroud)
这是关键部分,我无法想象如何实现它.
所以基本上我只想使用注释中指定的提供程序类.它也没有必要在这里使用提供程序类.因为autoConfig.provider().newInstance()基本上都是我需要的.(我需要在新实例上使用setter,但这就是我想在这个地方做的所有事情)
总而言之,我真正想做的是使用get(AutoConfig autoConfig)或在构造函数中将注释(或其值传递给提供者).目前我只使用构造函数来注入我想在新生成的配置实例上设置的configFile值.
我正在使用 guice 来注入依赖项。
我有这个提供商:
public class RequestUrlRepository implements Provider<IRequestUrlRepository> {
@Singleton
@Override
public IRequestUrlRepository get() {
IRequestUrlRepository answer = null;
if (System.getProperty("").equals(RouteOrCostRequest.cost.toString())){
answer = new CostRequestUrlRepository(...);
}
else
{
answer = new RoutingRequestUrlRepository(...);
}
return answer;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何使用 guice 注射器进行填充new CostRequestUrlRepository(...); 以及new RoutingRequestUrlRepository(...)它何时位于提供程序中?
如果不可能,我如何在不手动创建所有 ctor 参数的情况下初始化它们?
我从一个不受我控制的服务中得到一张可能为null的服务,并希望对其进行处理,比如说,过滤,映射和缩减为我需要的单个元素.
问题:从Optional到Stream是否有"链接"?
我试过(除其他外):
return Optional.ofNullable(getMap())
.map(Map::entrySet) // gets the entryset
.map(Stream::of)
.orElseGet(Stream::empty)
// i would then like to continue with
.filter(e -> e.getKey().startsWith("f")
.map(Entry::getValue)
.findFirst();
Run Code Online (Sandbox Code Playgroud)
但后来我没有... Stream<Entry>但Stream<Set<Entry>>有没有办法以某种方式flatMap一个集合或地图的可选?
注意:我对这里流畅,纯粹的流/可选方法感兴趣.当然,当我首先将地图保存到local var并确保它不为null时,它会起作用.
通过project-reactor 中的(同时已弃用的)reactor-bus,我有了 API eventBus.sendAndReceive(Event e, Consumer<?> callback)。
这允许通过发布事件来触发执行并自动订阅响应。
使用 Spring eventListeners,我可以从 EventListener 方法发布另一个事件,但我缺少直接订阅返回值的功能。
如何使用 spring 实现相同的行为?如何以编程方式注册/取消注册侦听器以及如何使主题动态化?
I want to modularize my build.gradle.kts files by using the precompiled script plugins feature introduced with gradle 5.3.1.
It works fine when I have my simple hello-world.gradle.kts file in buildSrc/src/main/kotlin directly
tasks.register("hello-world") { println("hello world") }
Run Code Online (Sandbox Code Playgroud)
and include it in the plugins section of my main build.gradle.kts:
plugins {
`hello-world`
}
Run Code Online (Sandbox Code Playgroud)
I can now use gradle hello-world and see the expected output.
But when I place the same script in buildSrc/src/main/kotlin/custom/hello-world-custom.gradle.kts (adding package custom to the script) it fails, …
我已经通过 application.yaml 和 spring configurationProperties 配置了我的兔子属性。因此,当我配置交换、队列和绑定时,我可以使用属性的 getter
@Bean Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(properties.getQueue());
}
@Bean Queue queue() {
return new Queue(properties.getQueue(), true);
}
@Bean TopicExchange exchange() {
return new TopicExchange(properties.getExchange());
}
Run Code Online (Sandbox Code Playgroud)
但是,当我配置 @RabbitListener 以记录队列中的消息时,我必须使用完整的属性名称,例如
@RabbitListener(queues = "${some.long.path.to.the.queue.name}")
public void onMessage(
final Message message, final Channel channel) throws Exception {
log.info("receiving message: {}#{}", message, channel);
}
Run Code Online (Sandbox Code Playgroud)
我想避免这种容易出错的硬编码字符串,并参考configurationProperties bean,如下所示:
@RabbitListener(queues = "${properties.getQueue()}")
Run Code Online (Sandbox Code Playgroud)
我曾经在 @EventListener 中遇到过类似的问题,其中使用 bean 引用“@bean.method()”有所帮助,但它在这里不起作用,bean 表达式只是解释为队列名称,这会失败,因为队列名称“@bean” ....“ 不存在。
是否可以使用 ConfigurationProperty-Beans 进行 RabbitListener 队列配置?
例如,我可以为 \xe2\x80\x9cretry 时间周期 \xe2\x80\x9d 配置许多不同的间隔表达式吗
\n类似的东西: \xe2\x80\x9cR6/PT10S, R2/PT30M\xe2\x80\x9d - 每 10 秒 6 次,然后 30 分钟后 2 次
\n提前致谢,\nWladi
\n对于一个小的新项目,我决定尝试一下JDBI(通常我使用 hibernate/jpa)。
我喜欢使用@SqlUpdate/@SqlQuery 创建基于注释的轻量级 dao。
但是:在某些情况下,我无法确定是要创建实体还是更新现有实体。我会放置一个“选择”语句,并根据其返回值使用插入或更新语句。
问题:jdbi 中的“仅接口”dao 是否以某种方式支持?还是我必须自己编写“createOrUpdate”方法(使自动生成的 dao 或多或少过时)?
感谢您的任何提示。
我使用springboot + jersey进行web restful实现.现在我要将swagger整合到我们的应用程序中.我做了以下.
@Configuration
@EnableSwagger2
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration(){
register(HelloworldAPI.class);
configureSwagger();
}
private void configureSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/");
beanConfig.setResourcePackage("com.cooltoo.api");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我在build.gradle上添加了以下依赖项:
compile('io.springfox:springfox-swagger2:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-petstore:'+springfoxSwaggerVersion)
compile('io.springfox:springfox-swagger-ui:'+springfoxSwaggerVersion)
compile('io.swagger:swagger-jersey2-jaxrs:1.5.8')
Run Code Online (Sandbox Code Playgroud)
我能够启动Web应用程序,但我徘徊哪个url是为了招摇?我尝试使用http:// localhost:8080,http:// localhost:8080/swagger和http:// localhost:8080/swagger-ui.html.但是没有一个可以被访问.
java ×6
guice ×2
spring ×2
spring-boot ×2
annotations ×1
camunda ×1
gradle ×1
inject ×1
java-stream ×1
jdbi ×1
jersey ×1
provider ×1
rabbitmq ×1
spring-mvc ×1
swagger ×1
swagger-ui ×1