我有以下配置文件:
@Configuration
public class PropertyPlaceholderConfigurerConfig {
@Value("${property:defaultValue}")
private String property;
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(new ClassPathResource("properties/" + property + ".properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下VM选项运行我的应用程序:
-Dproperty=propertyValue
Run Code Online (Sandbox Code Playgroud)
所以我希望我的应用程序在启动时加载特定的属性文件.但由于某些原因,在此阶段@Value注释不会被处理,属性也是如此null.另一方面,如果我PropertyPlaceholderConfigurer通过xml文件配置 - 一切都按预期完美.Xml文件示例:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="location">
<value>classpath:properties/${property:defaultValue}.properties</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
如果我尝试在另一个Spring配置文件中注入属性值 - 它被正确注入.如果我将PropertyPlaceholderConfigurerbean创建移动到该配置文件 - 字段值再次为null.
作为解决方法,我使用这行代码:
System.getProperties().getProperty("property", "defaultValue")
Run Code Online (Sandbox Code Playgroud)
哪个也有效,但我想知道为什么会发生这种行为,也许有可能以其他方式重写它但没有xml?
我知道我需要注册@Controller在我的servlet上下文中注释的类,以使我的webapp可访问.通常,我按照以下方式进行:
@Configuration
@EnableWebMvc
@ComponentScan({"foo.bar.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {
//other stuff like ViewResolvers, MessageResolvers, MessageConverters, etc.
}
Run Code Online (Sandbox Code Playgroud)
我添加到根应用程序上下文中的所有其他配置类.以下是我的dispetcher初始化程序通常如下所示:
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class, ServiceConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我开始使用WebSockets时,事情变得越来越有趣.要使websockets正常工作,您必须将WebSoketConfig.class放入servlet上下文.这是我的WebSocketConfig示例:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) …Run Code Online (Sandbox Code Playgroud) 在使用Java之前,我对Scala很新,而且大部分时间都是.现在,我的代码中都有警告说我应该"避免可变的局部变量",我有一个简单的问题 - 为什么?
假设我有一个小问题 - 确定四个中的最大值.我的第一个方法是:
def max4(a: Int, b: Int,c: Int, d: Int): Int = {
var subMax1 = a
if (b > a) subMax1 = b
var subMax2 = c
if (d > c) subMax2 = d
if (subMax1 > subMax2) subMax1
else subMax2
}
Run Code Online (Sandbox Code Playgroud)
在考虑此警告消息后,我找到了另一种解决方案:
def max4(a: Int, b: Int,c: Int, d: Int): Int = {
max(max(a, b), max(c, d))
}
def max(a: Int, b: Int): Int = {
if (a > b) a
else b
} …Run Code Online (Sandbox Code Playgroud) 这是我的意见:
Main.java
package com;
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("greeting.language"));
}
}
Run Code Online (Sandbox Code Playgroud)
的build.gradle
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.7
version = '1.0'
mainClassName = "com.Main"
applicationDefaultJvmArgs = ["-Dgreeting.language=en"]
Run Code Online (Sandbox Code Playgroud)
我的问题:如何配置另一个运行任务配置以使用不同的jvm参数,例如:-Dgreeting.language = ch.
我从maven搬到gradle,所以对于这种情况我会使用maven配置文件.gradle中是否有任何与配置文件相对应的结构?
更新1
让我澄清一点任务.我的项目比上面给出的例子复杂得多.我想有一组gradle任务(或者profile /或者在gradle中调用的任何东西),每个任务都应该是自包含的自定义变量集.作为输出,我想有一套gradle任务:
gradle <enableAllFeatures>
gradle <disableFeatureA>
...
Run Code Online (Sandbox Code Playgroud)
这些变量不是JvmArgs的要求.它可以是常量(或者任何可能的gradle)但是需要具有不同的任务,并且每个任务都应该指定它自己的一组变量
更新2
我在网上冲浪,发现非常有趣的话题 - gradle构建变种,但它似乎只适用于android项目.是否可以在java项目中使用构建变体?
我想通过 JMX 远程连接到我的应用程序,因此我在 main 方法中创建了以下配置:
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:7890/jmxrmi");
Map<String, Object> envConf = new HashMap<>();
//My custom authenticator
envConf.put(JMXConnectorServer.AUTHENTICATOR, new MyAuthenticator(jmxUsername, jmxPassword));
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
Run Code Online (Sandbox Code Playgroud)
以下是我开始申请的方式:
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=7890
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
Main
Run Code Online (Sandbox Code Playgroud)
但似乎缺少了一些东西,我得到以下异常:
Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
java.io.IOException: Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server …Run Code Online (Sandbox Code Playgroud) 我已使用以下命令向 Oozie 提交了一份作业:
oozie job -config ${config_file} -submit
Run Code Online (Sandbox Code Playgroud)
我的作业计划在每天 5 UTC 运行(频率 = 1440)。我的问题是 - 如何在这个时间范围之外触发执行?假设我已在 7 UTC 提交了一份作业,但不想等到第二天 5 UTC,并且希望在提交后立即手动触发它。
我尝试过开始工作:
oozie job -oozie host -start coordinatior-job-id-C
Run Code Online (Sandbox Code Playgroud)
但得到:
Error: E0303 : E0303: Invalid parameter value, [action] = [start]
Run Code Online (Sandbox Code Playgroud)
属性文件内容:
nameNode=hdfs://<namenode>:8020
jobTracker=http://<namenode>:23140
queueName=root.oozie
user=${user.name}
oozie.libpath=/user/oozie/share/lib
oozie.use.system.libpath=true
oozie.coord.application.path=${nameNode}/user/${user.name}/<job.location>
appPath=${oozie.coord.application.path}
initTime=2020-04-20T00:15Z
interval=0
frequency=1440
start=2020-04-20T00:50Z
oozie.launcher.mapreduce.map.cpu.vcores=1
Run Code Online (Sandbox Code Playgroud)
谢谢
我在应用程序中实现了一种生产者-消费者模式。一方面,生产者推动实体处理从不同来源接收到的数据,另一方面,我让消费者将事件从队列中移出并进行处理。
生产者和使用者都是弹簧豆,并且都是自动发现的,都需要链接到该共享队列。我知道我可以在xml文件或Java配置中定义我的bean,并将此Queue作为构造函数参数或通过setter传递为参数,但是有一种自动导入它的方法。我想到的唯一想法是为该队列创建一个包装器,然后注入该包装器:
@Component
public class QueueWrapper {
private final BlockingQueue<MyObject> sharedQueue = new LinkedBlockingQueue<>();
public void put(MyObject toPut) {
sharedQueue.put(toPut);
}
public MyObject take() {
return sharedQueue.take();
}
}
@Component
public class Producer {
@Autowire
private QueueWrapper queue;
....
}
@Component
public class Consumer {
@Autowire
private QueueWrapper queue;
....
}
Run Code Online (Sandbox Code Playgroud)
创建这个包装程序值得吗?我知道@Resource注释,但是我仅将其与列表,地图和集合一起使用,而实际上不知道如何配置资源Java配置文件。Spring文档页面中列表的XML示例:
<util:list id="emails">
<value>pechorin@hero.org</value>
<value>raskolnikov@slums.org</value>
<value>stavrogin@gov.org</value>
<value>porfiry@gov.org</value>
</util:list>
Run Code Online (Sandbox Code Playgroud)
然后是Java类:
@Component
public class SomeClass {
@Resource(name="emails")
private List<String> emails;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以在Java配置中将队列创建为此类资源?还是有其他方法可以将共享队列注入不同的bean?
java ×5
spring ×3
autowired ×1
gradle ×1
immutability ×1
jmx ×1
oozie ×1
scala ×1
scheduling ×1
spring-mvc ×1
websocket ×1
xml ×1