我正在尝试使用 Java 反射获取通用 Field 属性的实例,以便执行此 Field 的方法。
例如,如果我有一个带有方法 getValue() 的 MyType 类型类,并且我有另一个带有 MyType 类属性的 MyContainer 类,我正在寻找的是动态获取此字段并执行 getValue() 方法。这可能吗?
使用以下代码结构,我得到:
Exception in thread "main" java.lang.IllegalArgumentException: Can not set test.MyType field test.MyContainer.field1 to test.MyType
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at test.Main.main(Main.java:24)
Run Code Online (Sandbox Code Playgroud)
我的容器
public class MyContainer {
MyType field1;
MyType field2;
public MyContainer() {
super();
}
public MyContainer(MyType field1, MyType field2) {
super();
this.field1 = field1;
this.field2 = field2;
}
public MyType getField1() {
return field1;
}
public …Run Code Online (Sandbox Code Playgroud) 我想知道如何整合这三种技术。我目前正在使用 Spring MVC 和 Thymeleaf 模板左轮手枪为我的 Web 视图构建 Web 应用程序。我的 web MVC+Thymeleaf 配置完全没有 XML,这里是 web 配置最相关的部分:
@Configuration
@EnableWebMvc
public class ConfigWebMVC extends WebMvcConfigurerAdapter
{
/**
* Thymeleaf config - Spring will use Thymeleaf to render the HTML views
* @return The Thymeleaf resolver
*/
@Bean
public ServletContextTemplateResolver templateResolver() {
LOGGER.info("CREATING TEMPLATE RESOLVER");
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix( environment.getProperty("web.template_prefix") );
resolver.setSuffix( environment.getProperty("web.template_suffix") );
resolver.setTemplateMode( environment.getProperty("web.template_style") );
resolver.setCacheable(true);
return resolver;
}
/**
* Thymeleaf config - Thymeleaf will use templateEngine to …Run Code Online (Sandbox Code Playgroud) 您能提供在 Spring Integration 中路由消息的任何示例吗?按有效负载消息、标头或类似以下内容进行过滤:
<int:payload-type-router input-channel="routingChannel">
<int:mapping type="java.lang.String" channel="channel1" />
<int:mapping type="java.lang.Integer" channel="channel2" />
</int:payload-type-router>
Run Code Online (Sandbox Code Playgroud)
响应如何发挥作用?我的意思是,如果我发送:
通道->路由器->变压器->网关
很简单,但我正在寻找类似于此示例的内容:
<int:router input-channel="inChannel" expression="payload.paymentType">
<int:mapping value="CASH" channel="cashPaymentChannel"/>
<int:mapping value="CREDIT" channel="authorizePaymentChannel"/>
<int:mapping value="DEBIT" channel="authorizePaymentChannel"/>
</int:router>
Run Code Online (Sandbox Code Playgroud)
当我的网关收到消息并将响应发送到回复通道时。我如何在路由器中得知正确的回复通道是什么?
我正在尝试在我的 Spring Boot 应用程序中手动启动/停止 JMS 侦听器。我目前对我的容器工厂使用以下配置:
@EnableJms
public class ConfigJms {
...
@Bean(name = "queueContainerFactory")
public JmsListenerContainerFactory<?> queueContainerFactory(ConnectionFactory cf) {
ActiveMQConnectionFactory amqCf = (ActiveMQConnectionFactory) cf;
amqCf.setTrustAllPackages(true);
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(amqCf);
**factory.setAutoStartup(false);**
return factory;
}
...
}
Run Code Online (Sandbox Code Playgroud)
测试后factory.setAutoStartup(false);我很困惑,因为即使指示不为这个工厂容器启动任何监听器,监听器已经注册并在上下文启动时启动。
我通过使用jmsListenerEndpointRegistry.
jmsListenerEndpointRegistry.isAutoStartup() is true并
jmsListenerEndpointRegistry. isRunning () is true在执行之前jmsListenerEndpointRegistry.start();
是否需要配置其他东西?也许我忽略了覆盖一些自动配置。
编辑 1:JmsListenerEndpointRegistry 侦听器的无效状态
我在我的豆子中发现了一些不一致的地方:
jmsListenerEndpointRegistry.getListenerContainerIds().size()始终为 0。
jmsListenerEndpointRegistry.isAutoStartup()只是一个 return true 方法。
即使我使用这样的注释注册了几个侦听器:
@JmsListener(containerFactory="queueContainerFactory", destination = "${dest}")
Run Code Online (Sandbox Code Playgroud)
jmsListenerEndpointRegistry不显示有关这些侦听器状态的信息,但它们在启动时已连接到 ActiveMQ。(检查 ActiveMQ 管理控制台)
编辑 2:@JmsListener …
spring ioc-container spring-integration spring-jms spring-boot
spring ×2
annotations ×1
integration ×1
java ×1
reflection ×1
rest ×1
router ×1
spring-boot ×1
spring-jms ×1
spring-mvc ×1
swagger ×1
swagger-ui ×1
thymeleaf ×1