小编roh*_*hit的帖子

模拟中的弹簧值注入

我正在尝试为以下方法编写测试类

public class CustomServiceImpl implements CustomService {
    @Value("#{myProp['custom.url']}")
    private String url;
    @Autowire
    private DataService dataService;
Run Code Online (Sandbox Code Playgroud)

我在类中的一个方法中使用了注入的url值.为了测试这个,我写了一个junit类

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
public CustomServiceTest{
    private CustomService customService;
    @Mock
    private DataService dataService;
    @Before
    public void setup() {
        customService = new CustomServiceImpl();
        Setter.set(customService, "dataService", dataService);
    }    
    ...
}

public class Setter {
    public static void set(Object obj, String fieldName, Object value) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(obj, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

在applicationContext-test.xml中我正在使用加载属性文件

    <util:properties id="myProp" location="myProp.properties"/>
Run Code Online (Sandbox Code Playgroud)

但是在运行测试时,不会在CustomService中加载url值.我想知道是否有办法完成这项工作.

谢谢

java spring properties spring-mvc mockito

20
推荐指数
3
解决办法
4万
查看次数

Spring数据mongodb - 'cursor'选项是必需的

我正在尝试使用Spring Data MongoDB 3.6-rc4执行聚合操作.

Aggregation agg = newAggregation(
    lookup("orders", "orderId", "_id", "order") 
);
List<BasicDBObject> results = mongoOperations.aggregate(agg, "transactions", BasicDBObject.class).getMappedResults();
Run Code Online (Sandbox Code Playgroud)

但是在运行查询时遇到以下错误

2017-11-24 17:03:41,539 WARN  org.springframework.data.mongodb.core.MongoTemplate : Command execution of { "aggregate" : "transactions" , "pipeline" : [ { "$lookup" : { "from" : "orders" , "localField" : "orderId" , "foreignField" : "_id" , "as" : "order"}}]} failed: The 'cursor' option is required, except for aggregate with the explain argument
2017-11-24 17:03:41,574 ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with …
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-data-mongodb

20
推荐指数
2
解决办法
3万
查看次数

Spring数据mongodb在多个字段上排序

我想使用 MongoDB 的 Spring 数据对 MongoDB 中的多个字段进行排序。目前我正在尝试使用聚合来实现这一点:

    Aggregation agg = newAggregation( 
            match(Criteria.where("userId").is(userId)), 
            sort(Sort.Direction.DESC, "type", "createdDate"), 
    );
    AggregationResults<MyBean> results = mongoOperations.aggregate(agg, MyBean.class, MyBean.class);
Run Code Online (Sandbox Code Playgroud)

当我这样做,它是排序的type,并createdDateDESC订单。但我想DESCtypeASCcreatedDate

我试过,

    sort(Sort.Direction.DESC, "type");
    sort(Sort.Direction.ASC, "createdDate");
Run Code Online (Sandbox Code Playgroud)

但这只是对createdDate.

java mongodb mongodb-query aggregation-framework spring-data-mongodb

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

使用spring-data-mongodb进行审计

我试图使弹簧数据MongoDB的自动审计领域的解释在这里.下面是我的配置类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.abc")
@EnableMongoRepositories(basePackages = "com.abc.xyz.repository")
@EnableMongoAuditing
public class ApplicationConfiguration {

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
        MongoCredential mongoCredential = MongoCredential.createCredential("user", "test", "abc123".toCharArray());
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(mongoCredential));
        return new SimpleMongoDbFactory(mongoClient, "test");
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我添加@EnableMongoAuditing时,我在启动服务器时遇到以下错误.

Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#6dca0c34' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting …
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-data-mongodb spring-boot

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

XSLT 函数获取节点的 xpath

我需要一个 XSLT 函数,它将返回到它调用的节点的 xpath。

XML

    <root>
      <node>
        <subnode />
        <subnode />
        <subnode />
      </node>
      <node>
        <subnode>
          <subsubnode >
            <xsl:value-of select="fn:generateXPath()" />
          </subsubnode >
        </subnode>
      </node>
    </root>
Run Code Online (Sandbox Code Playgroud)

XSL

    <xsl:template match="root/node/subnode/sub" >
        <xsl:value-of select="fn:generateXPath()" />
    </xsl:template>

    <xsl:function name="fn:generateXPath" >
      <xsl:for-each select="ancestor::*">
      <xsl:value-of select="name()" />
      </xsl:for-each>
      <xsl:value-of select="name()" /> 
    </xsl:function>
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的函数,但它抛出了一个错误:

XPDY0002:无法在此处选择节点:上下文项未定义

但是当我在命名模板中尝试这个时,我能够得到结果。这可以使用xslt:function.

xslt xpath xmlnode

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

获取当前节点xpath

我需要获取当前节点的xpath,我已经编写了一个xsl函数

<func:function name="fn:getXpath">
    <xsl:variable name="xpath">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat($xpath, name())" />
            <xsl:if test="not(position()=last())">
                <xsl:value-of select="concat('/', $xpath)" />
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <func:result select="$xpath" />
</func:function>
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到以下错误

file:///D:/test.xsl; Line #165; Column #63; Variable accessed before it is bound!
file:///D:/test.xsl; Line #165; Column #63; java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

我正在使用xalan 2.7.0.请帮忙.

reflection xslt xpath xalan xslt-1.0

3
推荐指数
1
解决办法
3242
查看次数