因为我一直在使用Spring,如果我要编写一个具有依赖项的服务,我会执行以下操作:
@Component
public class SomeService {
@Autowired private SomeOtherService someOtherService;
}
Run Code Online (Sandbox Code Playgroud)
我现在遇到了使用另一个约定来实现相同目标的代码
@Component
public class SomeService {
private final SomeOtherService someOtherService;
@Autowired
public SomeService(SomeOtherService someOtherService){
this.someOtherService = someOtherService;
}
}
Run Code Online (Sandbox Code Playgroud)
我理解这两种方法都有效.但是使用选项B有一些优势吗?对我来说,它在类和单元测试中创建了更多代码.(必须编写构造函数而不能使用@InjectMocks)
有什么我想念的吗?除了在单元测试中添加代码之外,还有其他任何自动装配的构造函数吗?这是一种更优先的依赖注入方式吗?
假设您在xTend中有以下代码:
class StackOverflowGenerator {
def generate()'''
«var counter = 0»
«FOR i : 0 ..<10»
Line Numnber: «i»
«counter = counter + 1»
«ENDFOR»
'''
}
Run Code Online (Sandbox Code Playgroud)
这将以以下格式生成输出:
Line Numnber: 0
1
Line Numnber: 1
2
Line Numnber: 2
3
Line Numnber: 3
...
Run Code Online (Sandbox Code Playgroud)
如何让xTend不打印仅使用计数器的行,只打印行号行,使得输出如下所示:
Line Numnber: 0
Line Numnber: 1
Line Numnber: 2
Line Numnber: 3
...
Run Code Online (Sandbox Code Playgroud) 我有一个使用 3 个假客户端的服务。每次启动应用程序时,我都会在第一次调用任何 feign 客户端时收到 TimeoutException。
在一切稳定之前,我必须至少触发每个假客户端一次。在网上环顾四周,问题在于 feign 或 hystrix 内部的某些内容是延迟加载的,解决方案是创建一个覆盖 spring 默认值的配置类。我已经用下面的代码试过了,但它仍然没有帮助。我仍然看到同样的问题。任何人都知道解决这个问题?是通过 hystrix 回调两次调用 feignclient 的唯一解决方案吗?
@FeignClient(value = "SERVICE-NAME", configuration =ServiceFeignConfiguration.class)
@Configuration
public class ServiceFeignConfiguration {
@Value("${service.feign.connectTimeout:60000}")
private int connectTimeout;
@Value("${service.feign.readTimeOut:60000}")
private int readTimeout;
@Bean
public Request.Options options() {
return new Request.Options(connectTimeout, readTimeout);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Cloud - Brixton.SR4 Spring Boot - 1.4.0.RELEASE
这一切都在 docker Ubuntu - 12.04 Docker - 1.12.1 Docker-Compose - 1.8 中运行
我已经在stackoverflow和网络上寻找了解决方案。我没有看到任何可行的解决方案,因为也许所有帖子都不完全适合我的用例,其中包含文件内的列表以及对象结构。
这是一个样品
teddy.list:
-
name: Red
price: Five
-
name: Blue
price: One
-
name: Yellow
price: Two
-
name: Green
price: Three
Run Code Online (Sandbox Code Playgroud)
这是与属性文件相同的示例
teddy.list[0].name=Red
teddy.list[0].price=Five
teddy.list[1].name=Blue
teddy.list[1].price=One
teddy.list[2].name=Yellow
teddy.list[2].price=Two
teddy.list[3].name=Green
teddy.list[3].price=Three
Run Code Online (Sandbox Code Playgroud)
我希望能够向我的应用程序提供teddy.yml或teddy.properties文件以进行配置。
这是我的课程:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource(name = "props", value = "classpath:teddy.yml", ignoreResourceNotFound = false)
@ConfigurationProperties(prefix = "teddy")
public class TeddyBearConfig {
@Autowired
Environment env;
@Value("${teddy.list}")
private TeddyBear[] teddyBears;
public TeddyBear[] getTeddyBears() {
return teddyBears;
}
public void setTeddyBears(TeddyBear[] …
Run Code Online (Sandbox Code Playgroud) 我有这个BAT文件"iARP.BAT"
Content Begin
@ECHO OFF
npg -vv -f %1 -d %2
Content End
Run Code Online (Sandbox Code Playgroud)
我正在尝试将文件名(在循环中)作为第一个参数和设备名称(先前声明的变量)作为第二个参数传递.我正在尝试这样做:
for (int i = 1; i < arpFiles.Count; i++) {
p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.StartInfo.FileName = Application.StartupPath + "\\iARP.bat";
String argument1 = Application.StartupPath + "\\" + arpFiles[0].Name;
p.StartInfo.Arguments = argument1 + deviceName;
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = true;
p.Start();
}
Run Code Online (Sandbox Code Playgroud)
BTW arpFiles = List但它没有执行任何人都可以帮我这个吗?
尝试进行单元测试以从Spring Boot接收页面。如果与javascript一起使用,则可以很容易地反序列化页面,但是对于java,页面将失败。为spring添加了默认的构造函数(这是另一个stackoverflow帖子中的可接受答案),但是在这里不起作用。
单元测试
@Test
public void test_read_pagination_happy(@Autowired ObservationSet set) {
repository.save(set);
final HttpEntity<String> authHeaders = authentication.convert("", authSuccess);
final ParameterizedTypeReference<RestResponsePage<ObservationSet>> responseType = new ParameterizedTypeReference<RestResponsePage<ObservationSet>>() {
};
// final ResponseEntity<String> result = restTemplate.exchange(base + "/api/v1/observationset", HttpMethod.GET, authHeaders, String.class);
final ResponseEntity<RestResponsePage<ObservationSet>> result = restTemplate.exchange(base + "/api/v1/observationset", HttpMethod.GET, authHeaders,
responseType);
System.out.println(result.getBody());
assertSame(HttpStatus.OK, result.getStatusCode(), "incorrect status code");
}
Run Code Online (Sandbox Code Playgroud)
RestRespongePage类
class RestResponsePage<T> extends PageImpl<T> {
private static final long serialVersionUID = 3248189030448292002L;
public RestResponsePage(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public RestResponsePage(List<T> …
Run Code Online (Sandbox Code Playgroud) spring ×3
spring-boot ×2
.net ×1
autowired ×1
batch-file ×1
c# ×1
constructor ×1
hystrix ×1
java ×1
pagination ×1
properties ×1
spring-cloud ×1
xtend ×1
yaml ×1