小编Ken*_*nco的帖子

Spring Boot - 在代码中设置的密钥存储密码

Spring Boot 版本: 1.5.4.RELEASE

我目前server.ssl.key-store-password在 Spring Boot 应用程序的代码中设置 my 时遇到问题。我们将密码存储在保险库中,我之前是通过-D属性传递密码的。然而,这对我们来说并不是一个理想的解决方案。

解决方案看起来很简单,下面是我所做的:

@Bean
public ServletContextInitializer initializer() {
    final String keyStorePassword;
    // ... Get Password
    return servletContext -> servletContext.setInitParameter("server.ssl.key-store-password", keyStorePassword);
}
Run Code Online (Sandbox Code Playgroud)

按照春季启动文档这应该是罚款,因为ServletConfig被加载之前application.properties

不幸的是,Tomcat 拒绝以server.ssl.key-store-password这种方式从集合开始。据我所知,它AbstractNestablePropertyAccessor构造了一个org.springframework.boot.context.embedded.Ssl对象,该对象提供给 Tomcat,并用于构造密钥库。这是在 期间完成的SpringApplication.run(),这显然是在构建ServletConfigbean之前完成的。

因此,似乎我需要“刷新”上下文(据我所知,它正在破坏/重新创建它),或者找到另一种方法。我可以使用以下内容设置属性:

public static void main(String[] args) {
    String keyStorePassword = getKeystorePassword();
    HashMap<String, Object> props = new HashMap<>();
    props.put("server.ssl.key-store-password", keyStorePassword);
    new …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat keystore spring-boot

9
推荐指数
1
解决办法
4689
查看次数

Spring @QuerydslPredicate问题

使用的图书馆

Spring Boot 1.3.2.RELEASE

QueryDSL 3.7.2

QueryDSL Maven插件1.1.3

Hibernate 4.3.11.Final

问题

目前,我有一个Spring Boot应用程序,它使用Spring Data JPA(由Hibernate支持)具有一些基本的CRUD功能,并使用Spring Data Envers进行审计.我还有以下端点来检索实体列表:

HTTP://本地主机:8080 /测试应用程序/名单

现在,我想使用Spring通过@QuerydslPredicate注释提供新QueryDSL支持.这适用于大多数字段或子实体,但它似乎不适用于子实体的集合.文档,博客文章等似乎不包括这种情况 - 我能找到的唯一信息是它支持简单集合的"in"(即字符串的集合等).

所以,我的实体设置如下:

Person.java

@Data
@Entity
@Audited
public class Person {

    @Id
    private long id;

    private String name;

    private List<Pet> pets = new ArrayList<>();

}
Run Code Online (Sandbox Code Playgroud)

Pet.java

@Data
@Entity
@Audited
public class Pet {

    @Id
    private long id;

    private int age;

}
Run Code Online (Sandbox Code Playgroud)

我使用the生成我的Q类com.mysema.maven:apt-maven-plugin,它QPerson使用以下字段生成我:

public final ListPath<com.test.Pet, com.test.QPet> pets = …
Run Code Online (Sandbox Code Playgroud)

java spring querydsl spring-data

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

由于代理而导致Spring Bean注入失败

Spring版本:3.2.4.RELEASE和3.2.9.RELEASE

Mockito版本:1.8.5

我一直在尝试将H2测试引入到旧项目进行集成测试,我遇到了一些问题.由于事务的传播方式,我需要模拟一个自动连接的类.我以前做过这个,但我现在遇到了严重的问题.初始化测试时抛出以下错误消息:

org.springframework.beans.factory.BeanCreationException:创建名为'com.stuff.XMLITCase'的bean时出错:注入资源依赖关系失败; 嵌套异常是org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'TheProcessor'的Bean必须是[com.stuff.XMLBatchFileProcessor]类型,但实际上是 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor中的[$ Proxy118]类型. postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)

深入研究这个问题,事实证明这个bean实际上是一个代理.如果我们检查AbstractBeanFactory(第239轮),我们可以看到代理:

sharedInstance = {$ Proxy117 @ 7035}"com.stuff.XMLBatchFileProcessor@66c540d0"h = {org.springframework.aop.framework.JdkDynamicAopProxy@7039}

唯一的问题是,我不知道这是从哪里来的.我已经查看了配置和依赖项,并且无法找到应该发生的任何地方.

项目设置

不幸的是,我无法为此提供示例项目,但我将介绍我的测试配置.我有一个我为测试扩展的根类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/spring-test-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public abstract class AbstractIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

这只是在一些spring配置中加载并在每次测试后回滚事务.

spring配置也没什么奇怪的,虽然我的另一个模块和这个模块之间有一个区别.这是事务管理器和会话工厂:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
</bean>
Run Code Online (Sandbox Code Playgroud)

在我的其他模块中,我使用的是entityManagerFactory,以及不同的事务管理器:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
</bean>
Run Code Online (Sandbox Code Playgroud)

实际的类有一些自动装配的字段,以及通常的@Service注释:

@Service(value = "TheProcessor")
public final class XMLBatchFileProcessor extends BatchFileProcessor implements IXMLBatchProcessor …
Run Code Online (Sandbox Code Playgroud)

java junit spring hibernate mockito

0
推荐指数
1
解决办法
3208
查看次数