我有一个简单的模型和存储库,我想测试“保存”方法:
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "TEST")
public class TestModel {
@Id
@NonNull
@Basic(optional = false)
@Column(name = "id")
private String id;
@NonNull
@Basic(optional = false)
@Column(name = "testValue")
private String testValue;
}
@Repository
public interface TestRepository extends JpaRepository<TestModel, String> {
}
...在主类上:
@SpringBootApplication
public class DemoApplication {
@Autowired private static TestRepository testRepository;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
TestModel testModel = new TestModel("1", "randomValue");
testRepository.save(testModel);
}
}
当我尝试使用存储库保存一个简单的对象时,它给了我指向自动连接存储库的NullPointerException。
我使用了很多注解,比如ComponentScan(指向仓库包),EntityScan(指向实体包)EnabledAutoConfiguration,但没有一个对我有用。
我使用Sping Boot 2.7.10,Spring Data JPA和Spring Web作为pom.xml中的依赖项。
有人知道它应该是什么吗?谢谢!
1条答案
按热度按时间tquggr8v1#
您不能使用
@Autowired
将依赖项注入Sping Boot 中的静态字段。