我的Spring-Boot应用程序中有2个类:
-任务
-Runner
runner类包含我的main方法,我尝试从Tasks类中调用一个方法:
亚军:
@Component
public class Runner {
Tasks tasks;
@Autowired
public void setTasks(Tasks tasks){
this.tasks=tasks;
}
public static void main(String[] args){
//error being caused by below line
tasks.createTaskList();
}
Run Code Online (Sandbox Code Playgroud)
任务类:
@Service
public class Tasks {
public void createTaskList() {
//my code
}
//other methods
}
Run Code Online (Sandbox Code Playgroud)
在我的Runner中,当我尝试在Tasks类中调用createTaskList()方法时,我收到以下错误:
Non static field 'tasks' cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
以下方法使用Java 实体管理器的createNativeQuery()方法:
public List<Dog> findDogById(String id) {
List<Dog> resultList = new ArrayList<>();
try {
resultList = persistence.entityManager().createNativeQuery(" SELECT * FROM DOG WHERE ID = '" + id+ "' ", DogEntity.class).getResultList();
} catch (Exception e) {
}
return resultList;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我期望该方法返回结果时,它没有返回结果。即,当我直接运行查询时,SQL Developer我得到结果,但该方法不会返回相同的结果。
我的Syntax正确吗?我对此不确定:
" SELECT * FROM DOG WHERE ID = '" + id+ "' "
Run Code Online (Sandbox Code Playgroud)
即我需要 the'和 the吗"?
我收到以下异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value did not match expected type. [java.util.Date (n/a)];
nested exception is java.lang.IllegalArgumentException: Parameter value did not match expected type [java.util.Date (n/a)]
Run Code Online (Sandbox Code Playgroud)
这是我的存储库中的查询方法:
@Query("SELECT COUNT(entity) FROM Person entity WHERE trunc(entity.date) BETWEEN :startTime AND :endTime")
int getPersonBetweenDates(@Param("startTime") DateTime var1, @Param("endTime") DateTime var2);
Run Code Online (Sandbox Code Playgroud)
我在组件中的实现:
int totalPersons = personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay(), DateTime.now());
Run Code Online (Sandbox Code Playgroud)
为什么我收到这个错误?实现中的两个DateTime参数似乎与我的方法中的参数相匹配?
目前我的Spring-boot应用程序记录到一个名为:的文件myLog.log,这是按预期工作的,但是我希望日志文件timestamp在它的末尾有一个并在每次运行时创建一个新文件.
我试图在logback-test.xml下面显示的文件中实现它,但它只是给我文件名:myLog.log 没有时间戳.
我怎样才能解决这个问题?
的logback-的test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>path/to/my/file/mylog.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>mylog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="com.my.package" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" /> …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的课程设置为在Junit.
但是,当我尝试执行以下操作时,出现错误。
当前测试类:
public class PersonServiceTest {
@Autowired
@InjectMocks
PersonService personService;
@Before
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
assertThat(PersonService, notNullValue());
}
//tests
Run Code Online (Sandbox Code Playgroud)
错误:
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'personService'
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我的问题与这个问题类似,但它指的是Gradle“不是” Maven。
我已使用注释标记了项目中的多个测试@Category,并创建了我的测试套件(见下文)。
如何使用 Gradle 运行它?
测试套件:
@RunWith(Categories.class)
@Categories.IncludeCategory(MyTestSuite.class)
@Suite.SuiteClasses(ClassUnderTest.class)
public interface MyTestSuite {
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用最新版本的gradle. 由于模块文件中的以下行,构建失败build.gradle:
project.setProperty("doNow", true)
Run Code Online (Sandbox Code Playgroud)
出现此错误:
No such property: doNow for class: org.gradle.api.internal.project.DefaultProject_Decorated
Run Code Online (Sandbox Code Playgroud)
我是否需要在导致错误的行之前在 build.gradle 中声明此属性?
来自 gradle 文档setProperty:
设置该项目的属性。此方法在以下位置搜索具有给定名称的属性,并在找到该属性的第一个位置设置该属性。
我试图从我的项目中删除所有过时/不必要的东西 dependencies.
在查看allDeps任务的输出时,我可以看到有几个依赖项被复制但输出很难理解.
是否有一个特定的Gradle tool / task可以用来更清楚地了解我可以删除哪些依赖项?
我正在尝试使用此答案dependencies中描述的技术在我的 gradle 项目中声明。
但是,当我这样做时,我收到此错误:
No such property: libraries for class
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
在顶层build.gradle中声明为属性的依赖项如下:
ext.libraries = [
junit: 'junit:junit:4.10'
]
Run Code Online (Sandbox Code Playgroud)
尝试检索模块级别 build.gradle中的依赖关系(其中抛出错误):
testCompile([
libraries.junit
])
Run Code Online (Sandbox Code Playgroud)
抛出错误:
No such property: libraries for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
Run Code Online (Sandbox Code Playgroud) 我的应用程序由多个Java模块组成,使用Gradle.
我在其中创建了一个新模块,但无法从其他模块导入任何类。我对 Gradle/Java 很陌生,所以也许我遗漏了一些非常明显的东西。
当我尝试导入一个类时,出现以下错误:
"Cannot resolve symbol [import class name]"
Run Code Online (Sandbox Code Playgroud)
我能做些什么来解决这个问题?我需要更改我的build.gradle脚本吗?会不会是我的package structure?
注:我是能够导入标准的Java库,如
java.util.HashMap
Run Code Online (Sandbox Code Playgroud)
我的 Build.gradle 如下:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
Run Code Online (Sandbox Code Playgroud) java ×10
gradle ×5
dependencies ×2
junit ×2
spring ×2
autowired ×1
class ×1
groovy ×1
import ×1
jdbc ×1
jodatime ×1
logback ×1
logging ×1
methods ×1
mockito ×1
nativequery ×1
oracle11g ×1
package ×1
persistence ×1
properties ×1
spring-data ×1
static ×1
test-suite ×1
timestamp ×1
unit-testing ×1
xml ×1