我有一个Spring服务:
@Service
@Transactional
public class SomeService {
@Async
public void asyncMethod(Foo foo) {
// processing takes significant time
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个集成测试SomeService:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@Transactional
public class SomeServiceIntTest {
@Inject
private SomeService someService;
@Test
public void testAsyncMethod() {
Foo testData = prepareTestData();
someService.asyncMethod(testData);
verifyResults();
}
// verifyResult() with assertions, etc.
}
Run Code Online (Sandbox Code Playgroud)
这是问题所在:
SomeService.asyncMethod(..)与注释@Async和SpringJUnit4ClassRunner坚持@Async语义该testAsyncMethod线程将呼叫分叉someService.asyncMethod(testData)到自己的工作线程,然后直接继续执行verifyResults(),以前的工作线程完成其工作可能之前.
someService.asyncMethod(testData)在验证结果之前,我该如何等待完成?请注意,如何使用Spring 4和注释编写单元测试来验证异步行为?不要在这里申请,作为someService.asyncMethod(testData) …
我正在启动一个Spring Boot应用程序mvn spring-boot:run.
我@Controller的一个需要有关应用程序正在侦听的主机和端口的信息,即localhost:8080(或127.x.y.z:8080).在Spring Boot文档之后,我使用了server.address和server.port属性:
@Controller
public class MyController {
@Value("${server.address}")
private String serverAddress;
@Value("${server.port}")
private String serverPort;
//...
}
Run Code Online (Sandbox Code Playgroud)
在启动应用程序时mvn spring-boot:run,我得到以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: ... String ... serverAddress; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'server.address' in string value "${server.address}"
Run Code Online (Sandbox Code Playgroud)
双方server.address并server.port不能自动装配. …
使用Eclipse IDE,我想获得一个用特定Java注释注释的所有"东西"的列表.
例如,我想获得一个在Spring Framework的源代码或JAR中使用@Bean注释的所有方法的列表.
我知道有可能以编程方式查找注释用法,正如本问题所指出的那样.相反,我想在Eclipse IDE中的开发时查找注释用法.
Spring AOP有一个名为的方法级跟踪器CustomizableTraceInterceptor.使用Spring的XML配置方法,可以像这样设置此跟踪器:
<bean id="customizableTraceInterceptor" class="
org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
我想使用Spring的JavaConfig样式设置上述配置(即利用Java注释,尤其是@EnableAspectJAutoProxy在JavaConfig中激活AspectJ).
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
什么是@EnableAspectJAutoProxy风格的等价物<aop:advisor advice-ref="customizableTraceInterceptor" ...>?
我想用?决定我想要调用哪种方法,但我不需要分配变量.我的问题:有没有办法使用三元运算符而不分配变量?
(something i dont need) = (x == 1)? doThisMethod():doThatMethod()
Run Code Online (Sandbox Code Playgroud)
代替
if(x == 1) {
doThisMethod()
} else {
doThatMethod()
}
Run Code Online (Sandbox Code Playgroud) 在Maven中,我如何找出目标的默认阶段(如果此特定目标存在任何默认阶段)?
我正在使用一个名为Jetty Maven插件的Maven插件.它包含一个目标jetty:run.运行命令mvn jetty:run(注意此命令仅包含目标,而不是阶段)首先构建一个指定的pom.xmlWeb应用程序,直到默认test-compile阶段,然后将其部署在Jetty服务器中.
正如Mojo API规范中所指出的,目标可以在其源代码(via @phase或via @execute phase)中为其分配默认阶段.如果是jetty:run,默认阶段是@execute phase="test-compile".
但是找到源代码文件会变得非常复杂.有没有更简单的方法来找出默认阶段?
我的一个Spring Boot应用程序在Maven 测试阶段出现问题.
在测试和"常规"应用程序运行时,Spring Boot应用程序都使用非常类似的logback配置文件src/main/resources/logback-spring.xml.此配置文件(传递)包括logback配置文件base.xml和file-appender.xml.这些配置文件设置了一个logback属性 LOG_FILE=/tmp/spring.log.
我想最好的做法是文件/tmp/server.log归用户和组所有${MY_SPRING_BOOT_APPLICATION}.
Jenkins以用户身份运行jenkins.jenkins没有写入权限/tmp/server.log.因此,Jenkins执行时JUnit测试失败.
/var/log//tmp/spring.log如果有两个或更多Spring Boot应用程序同时运行,是否会同时修改文件(因此会被破坏)?我想让Eclipse的Java调试器在特定类型(类/接口)的对象被实例化时调试暂停受监视的进程.
作为一种解决方法,我可以设置断点
但
我想在我的Spring 3.1 Web应用程序中提供自定义404错误页面,但我无法停用Jetty 8的默认404错误页面.
开箱即用的Jetty 8提供了一个默认的404错误页面:当访问Jetty托管的网站,并提供一个不由任何servlet处理的URL路径时(例如通过访问http://www.example.com/nonexisting),Jetty响应有自己的默认HTML错误页面:
HTTP ERROR 404
Problem accessing /nonexisting. Reason:
Not Found
Powered by Jetty://
Run Code Online (Sandbox Code Playgroud)
要替换此默认行为,
DefaultHandler从我的码头XML文件,web.xml的包括指向的Servlet 2.5和Servlet 3.0错误处理程序位置/error,@Controller处理请求的人/error,但我的网站仍然返回Jetty自己的默认HTML错误页面.
Jetty 8的官方文档讨论了如何设置"自定义错误页面",但有人建议
@Controller,如上所述)/URI 的"根"Web应用程序." (我不想这样做,因为我web.xml已经将Spring MVC映射DispatcherServlet到/.如何关闭Jetty的默认错误处理程序并按照上面的指示完成错误处理?
假设我有以下enum类型:
public enum Country {
CHINA,
JAPAN,
FRANCE,
// ... and all other countries
AUSTRIA,
POLAND;
}
Run Code Online (Sandbox Code Playgroud)
现在我想创建这个枚举的子集,在概念上如下:
public enum EuropeanUnion constraints Country {
FRANCE,
// ... and all other countries within the European Union
AUSTRIA,
POLAND;
}
public enum LandlockedCountries constraints Country {
// ... all landlocked countries
AUSTRIA;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个enum类型的子集,以便我可以编写诸如的方法
Set<Person> getNationalMembersOfEuropeanParliament(EuropeanUnion euCountry);
Run Code Online (Sandbox Code Playgroud)
使用EuropeanUnion参数的子类型可以euCountry防止API用户意外地传入无效的国家/地区,例如非欧盟国家/地区JAPAN.
有没有办法"约束"有效枚举值的范围,以便人们可以从静态类型系统中受益?
java ×6
eclipse ×2
spring ×2
spring-boot ×2
annotations ×1
aspectj ×1
breakpoints ×1
debugging ×1
jetty ×1
jetty-8 ×1
junit ×1
logback ×1
logging ×1
maven-3 ×1
maven-plugin ×1
spring-aop ×1
spring-async ×1
spring-mvc ×1
ternary ×1
web.xml ×1