使用Spring的Java Config,我需要使用只能在运行时获得的构造函数参数来获取/实例化原型范围的bean.请考虑以下代码示例(为简洁起见而简化):
@Autowired
private ApplicationContext appCtx;
public void onRequest(Request request) {
//request is already validated
String name = request.getParameter("name");
Thing thing = appCtx.getBean(Thing.class, name);
//System.out.println(thing.getName()); //prints name
}
Run Code Online (Sandbox Code Playgroud)
Thing类的定义如下:
public class Thing {
private final String name;
@Autowired
private SomeComponent someComponent;
@Autowired
private AnotherComponent anotherComponent;
public Thing(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
Run Code Online (Sandbox Code Playgroud)
注意事项name是final:它只能通过构造函数来提供,并保证不变性.其他依赖项是Thing类的特定于实现的依赖项,并且不应该知道(紧密耦合到)请求处理程序实现.
此代码与Spring XML配置完美配合,例如:
<bean id="thing", class="com.whatever.Thing" scope="prototype">
<!-- other post-instantiation properties omitted --> …Run Code Online (Sandbox Code Playgroud) 在使用Spring MVC开发REST服务的同时,我希望在开发中渲染JSON"漂亮打印",但在生产中正常(减少空白).
使用Groovy及其java.lang.Process支持,如何将多个shell命令一起管道?
考虑这个bash命令(并假设您的用户名是foo):
ps aux | grep ' foo' | awk '{print $1}'
Run Code Online (Sandbox Code Playgroud)
这将打印出用户名 - 与您的用户帐户相关的某些进程的一行.
使用Groovy,ProcessGroovyMethods文档和代码说我应该能够做到这一点来实现相同的结果:
def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print $1}'".execute()
p.waitFor()
println p.text
Run Code Online (Sandbox Code Playgroud)
但是,我无法获得除此之外的任何文本输出:
def p = "ps aux".execute()
p.waitFor()
println p.text
Run Code Online (Sandbox Code Playgroud)
一旦我开始配管,println就不会打印出任何东西.
思考?
在我的单元测试中,我想创建一个嵌入式(进程内/内存中)Hazelcast实例,它根本不会尝试启动或执行任何网络操作.
我该怎么做呢?
例如:
Config config = new Config();
// what goes here?
HazelcastInstance inProcessOnly = Hazelcast.newHazelcastInstance(config);
Run Code Online (Sandbox Code Playgroud) 我正在编写一个自定义的Spring Boot启动程序,其他开发人员将放入他们的应用程序,这个启动程序包含开箱即用的控制器和UI屏幕.
这些UI屏幕是国际化的,i18n键/值在包文件中:com/foo/wherever/i18n.properties.
我想确保在启动时加载我的启动器时,这些i18n.properties在应用程序中MessageSource自动可用,以便我的UI页面工作(通过普通的Spring Controller + ViewResolver + View实现呈现),而app开发人员不必指定这个归档自己.
换句话说,他们应该能够将我的启动器添加到他们的运行时类路径中,并且一切"正常工作"而无需配置任何东西.
现在,我发现应用程序开发人员可以创建自己的src/main/resources/messages.properties文件并手动配置其他消息文件application.properties:
spring.messages.basename = messages, com.foo.wherever.i18n
Run Code Online (Sandbox Code Playgroud)
这将有效.
但是,这需要以下两个方面:
spring.messages.basename属性 - 它不是自动的.和messages.properties在其应用程序类路径中拥有自己的文件.如果messages.properties文件不存在,spring.messages.basename甚至不起作用.即使他们不关心i18n,这仍然是必需的 - 不可取.我想我可以将我的i18n.properties文件移动到启动器.jar中的类路径:/messages.properties文件中,但这似乎不是一个好的解决方案:如果app dev有自己的messages.properties文件只有一个它们将被读取,导致消息值丢失.
看起来好像春天启动MessageSourceAutoConfiguration应该有一个概念CompositeMessageSource是在一个或多个迭代MessageSource实例可用(并且Order在Spring的ApplicationContext ED)和该所使用的DispatcherServlet的.这将允许任何启动器仅通过MessageSource在其自动配置中声明a来为可用消息做出贡献
有可能做我的要求吗?应用程序开发人员最"亲切"的解决方案是什么?
使用Sphinx 1.2.3并给出此RST片段:
.. code-block:: xml
<foo>
<bar>|version|</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
在conf.py我有:
version = '1.0.2'
Run Code Online (Sandbox Code Playgroud)
您如何确保上述RST片段呈现为:
<foo>
<bar>1.0.2</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
这个前面的问题表明,我们应该用.. parsed-literal::代替.. code-block::,但不工作,也没有在这个问题的工作所引用的链接要么.
我还想保留语法高亮.
在一个独立的Spring Boot Web应用程序(可执行jar)中,您如何告诉Spring Boot我们希望将嵌入式Tomcat实例的HTTP访问日志发送到stdout?
假设我有一个文件defaults.yaml:
pool:
idleConnectionTestPeriodSeconds: 30
idleMaxAgeInMinutes: 60
partitionCount: 4
acquireIncrement: 5
username: dev
password: dev_password
Run Code Online (Sandbox Code Playgroud)
和另一个文件,production.yaml:
pool:
username: prod
password: prod_password
Run Code Online (Sandbox Code Playgroud)
在运行时,我如何读取两个文件并将它们合并为一个,以便应用程序“看到”以下内容?
pool:
idleConnectionTestPeriodSeconds: 30
idleMaxAgeInMinutes: 60
partitionCount: 4
acquireIncrement: 5
username: prod
password: prod_password
Run Code Online (Sandbox Code Playgroud)
例如,SnakeYAML 可以实现吗?还有其他工具吗?
我知道一种选择是将多个文件作为 Map 读取,然后自己合并它们,将合并渲染到一个临时文件,然后读取它,但这是一个重量级的解决方案。现有的工具可以做到这一点吗?
我们的REST API允许用户将自定义无模式JSON添加到我们的一些REST资源中,我们需要它可以在Elasticsearch中进行搜索.这种自定义数据及其结构可以在相同类型的资源之间完全不同.
考虑这个示例文档:
{
"givenName": "Joe",
"username": "joe",
"email": "joe@mailinator.com",
"customData": {
"favoriteColor": "red",
"someObject": {
"someKey": "someValue"
}
}
}
Run Code Online (Sandbox Code Playgroud)
除customData遵守架构外的所有字段.customData始终是JSON对象,但该对象中的所有字段和值可能因资源而异.无法保证customData中的任何给定字段名称或值(甚至值类型)在任何两个资源中都是相同的,因为用户可以根据需要编辑这些字段.
支持搜索的最佳方法是什么?
我们认为解决方案是不创建customData索引创建时的任何映射,但随后它变得不可思议(这与ES文档所说的相反).如果对非映射属性的查询起作用,这将是理想的解决方案,并且此方法没有性能问题.但是,在针对该问题运行多个测试之后,我们无法使其工作.
这是需要任何特殊配置的东西吗?或者文档不正确?关于它为什么不起作用的一些澄清将不胜感激.
由于目前这不适合我们,我们已经考虑了几种替代解决方案:
重新索引:这将是昂贵的,因为我们需要重新索引包含该文档的每个索引,并且每次用户使用不同的值类型更新属性时都这样做.性能真的很糟糕,所以这可能不是一个真正的选择.
使用多匹配查询:我们将通过在每次customData对象发生更改时将随机字符串附加到customData字段名称来执行此操作.例如,这就是索引的文档看起来像:
{
"givenName": "Joe",
"username": "joe",
"email": "joe@mailinator.com",
"customData_03ae8b95-2496-4c8d-9330-6d2058b1bbb9": {
"favoriteColor": "red",
"someObject": {
"someKey": "someValue"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着ES将为每个"随机"字段创建一个新映射,我们将在执行查询时使用字段名称的"start with"通配符进行短语多匹配查询.例如:
curl -XPOST 'eshost:9200/test/_search?pretty' -d '
{
"query": {
"multi_match": {
"query" : "red",
"type" : "phrase",
"fields" : ["customData_*.favoriteColor"]
}
}
}' …Run Code Online (Sandbox Code Playgroud)我想在Cassandra 1.2.8中插入一行包含50,000列的单行.在插入之前,我已准备好整个行的所有数据(在内存中):
+---------+------+------+------+------+-------+
| | 0 | 1 | 2 | ... | 49999 |
| row_id +------+------+------+------+-------+
| | text | text | text | ... | text |
+---------+------+------+------|------+-------+
Run Code Online (Sandbox Code Playgroud)
列名是整数,允许切片进行分页.列值是该特定索引处的值.
CQL3表定义:
create table results (
row_id text,
index int,
value text,
primary key (row_id, index)
)
with compact storage;
Run Code Online (Sandbox Code Playgroud)
由于我已经在内存中拥有row_id和所有50,000个名称/值对,我只想在单个请求/操作中向Cassandra中插入一行,以便尽可能快.
我似乎唯一能找到的是执行以下50,000次:
INSERT INTO results (row_id, index, value) values (my_row_id, ?, ?);
Run Code Online (Sandbox Code Playgroud)
第一个?是索引计数器(i),第二个?是要存储在位置的文本值i.
这需要很多时间.即使我们将上述INSERT放入批处理中,也需要花费很多时间.
我们完整地拥有了我们需要的所有数据(完整的行),我认为很容易说"这里,Cassandra,将这些数据作为一行存储在一个请求中",例如:
//EXAMPLE-BUT-INVALID CQL3 SYNTAX:
insert into results …Run Code Online (Sandbox Code Playgroud) java ×4
json ×2
spring ×2
spring-boot ×2
bash ×1
block ×1
cassandra ×1
cql3 ×1
groovy ×1
hazelcast ×1
jackson ×1
logging ×1
merge ×1
pretty-print ×1
process ×1
prototype ×1
python ×1
schemaless ×1
scope ×1
shell ×1
snakeyaml ×1
spring-mvc ×1
stdout ×1
substitution ×1
tomcat ×1
variables ×1
yaml ×1