我有一个spring-boot应用程序,我想用外部配置文件运行.当我以jar形式运行它(带有嵌入式servlet容器)时,一切都很好.但我想在外部servlet容器(Tomcat)下运行它,这里我有外部配置问题.我尝试过@PropertySource,但在这种情况下,应用程序只获取war文件配置中不存在的属性:外部配置不会覆盖内部配置.所以问题是:如何配置外部配置以覆盖内部配置?
为简单起见,我们采用一个非常简单的类:
public class TestingClass {
public void method1(){
System.out.println("Running method 1");
method2();
}
public void method2(){
System.out.println("Running method 2");
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在编写一个简单的测试,它检查当我们调用method1()时,调用method2():
class TestingClassSpec extends Specification {
void "method2() is invoked by method1()"() {
given:
def tesingClass = new TestingClass()
when:
tesingClass.method1()
then:
1 * tesingClass.method2()
}
}
Run Code Online (Sandbox Code Playgroud)
通过执行此测试,我收到以下错误:
运行方法1运行方法2
调用次数太少:
1*tesingClass.method2()(0次调用)
为什么我收到此错误?打印日志显示调用了method2().
我实现了spring-boot应用程序,现在我想将其用作非spring应用程序的库。我如何初始化lib类以使自动装配的依赖项按预期方式工作?显然,如果我使用“ new”创建类实例,则所有自动装配的依赖项都将为null。
我想在我的Spring Boot应用程序中配置log4j.xml文件的位置。为此,我已将logging.config属性添加到我的application.properties配置中,指示log4j.xml文件路径。但是似乎此属性被忽略。但是它应该可以根据春季启动文档工作:
logging.config= # location of config file (default classpath:logback.xml for logback)
Run Code Online (Sandbox Code Playgroud)
我做错什么了吗?
最简单的例子:我有以下方法:
public String testMethod(String arg){
.....
}
Run Code Online (Sandbox Code Playgroud)
我想模拟这个方法作为结果返回传递的参数.例如:
testMethod("aString") returns "aString"
testMethod("anotherString") returns "anotherString"
Run Code Online (Sandbox Code Playgroud)
我知道我可以硬编码这种行为,但我希望它是通用的.