小编hub*_*blo的帖子

Springboot单元测试Autowired

我有以下课程:

ApplicationAndConfiguration类

package mypackage.service;

import mypackage.service.util.MyUtility;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ApplicationAndConfiguration {


    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ApplicationAndConfiguration.class, new String[]{});
    }

    @Bean(initMethod="init")
    public MyUtility birtUtil() {
        return new MyUtility();
    }
}
Run Code Online (Sandbox Code Playgroud)

MyRestController类

package mypackage.service.controllers;

import mypackage.service.util.MyUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @Autowired
    private MyUtility util;

    @RequestMapping("/getLibraryName")
    public String getMessageFromRest(@RequestParam String name) {

        return "name was " + name + "//" + …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing autowired spring-boot

24
推荐指数
2
解决办法
5万
查看次数

Spring @Scheduler并行运行

我有以下3个班级:

ComponantA

package mytest.spring.test.spring;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ComponentA {

    Logger log = Logger.getLogger(ComponentB.class);

    @Scheduled(fixedRate=2000)
    public void sayHello() {
        for(int i=1 ; i<=5 ; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            log.info("Hello from ComponentA " + i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以componentB

package mytest.spring.test.spring;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ComponentB {

    Logger log = Logger.getLogger(ComponentB.class);

    @Scheduled(fixedRate=2000)
    public void sayHello() {
        for(int i=1 …
Run Code Online (Sandbox Code Playgroud)

java spring schedule spring-boot

13
推荐指数
1
解决办法
7332
查看次数

Spring StopWatch并发

我有一个Bean配置如下:

    @Bean(name = "myStopWatch")
    @Scope(value = "prototype")
    public MyStopWatch myStopWatch() {
       return new MyStopWatch();
    }
Run Code Online (Sandbox Code Playgroud)

而MyStopWatch类如下:

import org.springframework.util.StopWatch;

public class MyStopWatch {

   private StopWatch sw = new StopWatch();

   public void start() {
       if(!sw.isRunning()) {
           sw.start();
       }
   }

   public void stop() {
       if(sw.isRunning()) {
           sw.stop();
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

我们在高度并发的环境中使用这个bean.如果我的理解是正确的,那么MyStopWatch类永远不应该在线程之间共享,对吧?

但是,我们有时(很少)会收到以下错误:

java.lang.IllegalStateException:无法启动StopWatch:它已经在org.springframework.util.StopWatch.start(StopWatch.java:116)的org.springframework.util.StopWatch.start(StopWatch.java:127)上运行

到目前为止,我们无法通过测试重现此行为.我正在寻找有关如何正确定义我的sw变量(或ma bean)的更多信息,以避免此错误.

java concurrency spring stopwatch

5
推荐指数
1
解决办法
1590
查看次数

用于复制任务的 Gradle 排除模块

我有一个复制任务集如下:

task copyToLib( type: Copy ) {
   into "$buildDir/myapp/lib"
   from configurations.runtime

   // We only want jars files to go in lib folder
   exclude "*.exe"
   exclude "*.bat"
   exclude "*.cmd"
   exclude "*.dll"

   // We exclude some lib
   exclude group: "org.slf4j", name: "slf4j-api", version: "1.6.2"
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Could not find method exclude() for arguments [{group=org.slf4j, name=slf4j-api, version=1.6.2}] on task ':copyToLib' of type org.gradle.api.tasks.Copy
Run Code Online (Sandbox Code Playgroud)

我觉得这只是一个语法问题,有什么提示吗?

java copy gradle build.gradle

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