小编Sam*_*eer的帖子

无法在Spring-Batch中自动运行JobLauncherTestUtils

我在春季批次中执行功能测试时遇到错误.得到以下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.batch.test.JobLauncherTestUtils] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
Run Code Online (Sandbox Code Playgroud)

以下是用于此目的的配置文件和测试文件.

custom-context.xml文件:

<batch:job id="custom.entities">
    <batch:step id="entity.processor">
        <batch:tasklet>
            <batch:chunk reader="customReader" writer="customWriter" commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="customReader" class="com.batch.custom.EntityReader" scope="step">
    <property name="providerId" value="#{jobParameters['providerId']}" />
</bean>

<bean id="customWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="file:c:/Temp/ledgers-output.txt"/>
    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

CustomJobTest.java文件

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Autowired …
Run Code Online (Sandbox Code Playgroud)

spring-batch

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

Spring RestTemplate UnsupportedOperationException with Collections $ UnmodifiableCollection.add(Unknown Source)

这是我们的休息模板配置

@Bean
    public RestTemplate infoBloxRestTemplate() {
        RestTemplate restTemplate=new RestTemplate();
        ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(httpBasicAuthenticationInterceptor());
        restTemplate.setInterceptors(interceptors);
        restTemplate.getMessageConverters().add(jacksonConverter());
        restTemplate.setRequestFactory(genericHttpRequestFactory());
        return restTemplate;
    }
Run Code Online (Sandbox Code Playgroud)

我们正在尝试进行POST调用,该调用与Postman一起成功运行并返回正确的响应.

final HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");

HttpEntity<Object> httpEntity = new HttpEntity<Object>(record, headers);

StringBuilder uri = new StringBuilder(infobloxRestClient.createUrl("/record:host"));
infobloxRestClient.getRestTemplate().exchange(uri.toString(), HttpMethod.POST, httpEntity, String.class);
Run Code Online (Sandbox Code Playgroud)

但是这个POST调用失败并出现以下错误.这是我的堆栈跟踪:

com.sun.xml.ws.server.sei.TieHandler createResponse
SEVERE: null
java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
    at org.springframework.http.HttpHeaders.add(HttpHeaders.java:558)
    at com.test.externalinterfaces.HTTPBasicAuthenticationInterceptor.intercept(HTTPBasicAuthenticationInterceptor.java:30)
    at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:81)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:67)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
Run Code Online (Sandbox Code Playgroud)

在这方面的任何帮助将非常有帮助.

java spring resttemplate

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

实例变量同步

在下面的示例中,锁定是在实例变量employee上获得的(不在此),但在进入synchronized块时,TestClass1的Threads仍然被锁定.任何建议为什么这种行为.据我所知,如果它同步就应该被锁定.

public class TestClass{
  public static void main(String args[]){
    TestClass1 obj = new TestClass1();
    Thread t1 = new Thread(obj, "T1");
    Thread t2 = new Thread(obj, "T2");
    t1.start();
    t2.start();
  }
}

class TestClass1 implements Runnable{

Employee employee = new Employee();

public void myMethod () {
    synchronized (employee) {
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void myOtherMethod() {
    synchronized (employee) {
        try {
            Thread.sleep(4000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-synchronization

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