小编tho*_*s77的帖子

在Configuration中由ImportResource覆盖的主Spring bean

我有一个Spring测试配置类,它应该覆盖xml-config中的现有bean.但我的问题是xml bean覆盖了在test-config中使用primary注释的bean.我尝试用不同的名称命名test-bean,但这对我来说也没有用.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CamelJmsTest.TestConfig.class})
public class CamelJmsTest {

    @Configuration
    @ImportResource("classpath:production-beans-camel-jms.xml")
    public static class TestConfig {

    @Primary
    @Bean
    public JmsTemplate jmsTemplate() {
        return new JmsTemplate(new ActiveMQConnectionFactory("", "", ACTIVE_MQ_HOST));
    }

    @Primary
    @Bean // ideally i just want this bean to override the bean imported from the xml config
    public RouteConfigBuilder routeConfig() {
        return RouteConfigBuilder.builder().autoStart(true).build();
    }

    @Primary
    @Bean
    public RouteBuilder routeBuilder(@Value("${amq.endpoint}") String endpoint,
                                                              @Autowired Processor processor) {
        return new RouteBuilder(routeConfig(), "", endpoint, processor);
    }
}

    private static final String ACTIVE_MQ_HOST = …
Run Code Online (Sandbox Code Playgroud)

java spring integration-testing dependency-injection

7
推荐指数
1
解决办法
396
查看次数

配置 IntelliJ 以使用 Java 14 和记录

我试图让 IntelliJ 与 SDK-14 一起工作,但我显然在让记录工作时遇到了问题。由于发布日期是在 3 月,我认为 Jetbrains 的好人已经接近拥有一个适用于 SDK-14 的版本。我需要使用 --enable-preview 运行 SDK 14。我在谷歌上搜索了几天没有任何运气。

我在 Sublime 上有一些运气,但我错过了代码完成。如果还有其他选择,请随时提及。

java editor intellij-idea java-14

4
推荐指数
1
解决办法
1741
查看次数

Java 8 - equals和hashcode的默认方法

我已经default在接口中创建了用于实现equals(Object)hashCode()以可预测方式的方法.我使用反射来迭代类型(类)中的所有字段以提取值并进行比较.代码依赖于Apache Commons Lang及其HashCodeBuilderEqualsBuilder.

问题是我的测试告诉我,第一次调用这些方法时,第一次调用需要花费更多的时间.计时器使用System.nanoTime().以下是日志中的示例:

Time spent hashCode: 192444
Time spent hashCode: 45453
Time spent hashCode: 48386
Time spent hashCode: 50951
Run Code Online (Sandbox Code Playgroud)

实际代码:

public interface HashAndEquals {

    default <T> int getHashCode(final T type) {
        final List<Field> fields = Arrays.asList(type.getClass().getDeclaredFields());
        final HashCodeBuilder builder = new HashCodeBuilder(31, 7);
        fields.forEach( f -> {
            try {
                f.setAccessible(true);
                builder.append(f.get(type));
            } catch (IllegalAccessException e) {
                throw new GenericException(e.toString(), 500);
            }
        });
        return builder.toHashCode();
    }

    default <T, …
Run Code Online (Sandbox Code Playgroud)

behavior equals hashcode java-8 default-method

2
推荐指数
1
解决办法
5308
查看次数

在Kotlin中创建Spring的ParameterizedTypeReference实例

我正在努力学习Kotlin,并测试它如何与弹簧靴一起工作.我的应用程序使用mongo数据库来存储数据,我有一个用于检索数据的Jersey资源.我正在使用spring-boot-test和测试它RestTestTemplate.

RestTestTemplate具有exchange这需要一个方法ParameterizedTypeReference.该类有一个受保护的构造函数.所以我设法从Kotlin使用它的唯一方法是这样的:

class ListOfPeople : ParameterizedTypeReference<List<Person>>()

这是我的测试方法:

@Test
fun `get list of people`() {
    // create testdata
    datastore.save(Person(firstname = "test1", lastname = "lastname1"))
    datastore.save(Person(firstname = "test2", lastname = "lastname2"))
    datastore.save(Person(firstname = "test3", lastname = "lastname2"))
    datastore.save(Person(firstname = "test4", lastname = "lastname2"))

    val requestEntity = RequestEntity<Any>(HttpMethod.GET, URI.create("/person"))

    // create typereference for response de-serialization
    class ListOfPeople : ParameterizedTypeReference<List<Person>>() // can this be done inline in the exchange method?
    val responseEntity : ResponseEntity<List<Person>> …
Run Code Online (Sandbox Code Playgroud)

generics spring-test kotlin spring-boot

2
推荐指数
2
解决办法
1325
查看次数