能@Component
,@Repository
并@Service
注释互换在Spring中,还是他们提供任何特殊的功能,除了作为一个符号设备?
换句话说,如果我有一个Service类并且我将注释更改@Service
为@Component
,它仍然会以相同的方式运行吗?
或者注释是否也会影响类的行为和功能?
我收到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.
Action:
Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
Run Code Online (Sandbox Code Playgroud)
我之前从未见过这个错误,但@Autowire无法正常工作,这很奇怪.这是项目结构:
申请人界面
public interface Applicant {
TApplicant findBySSN(String ssn) throws ServletException;
void deleteByssn(String ssn) throws ServletException;
void createApplicant(TApplicant tApplicant) throws ServletException;
void updateApplicant(TApplicant tApplicant) throws ServletException;
List<TApplicant> getAllApplicants() throws ServletException;
}
Run Code Online (Sandbox Code Playgroud)
ApplicantImpl
@Service
@Transactional
public class ApplicantImpl implements Applicant {
private static Log …
Run Code Online (Sandbox Code Playgroud) 我正在阅读 Spring Boot 的 JPA 入门教程,但我很挣扎。我知道这里有时会问这个问题('Field required a bean of type that could not be found.' error spring restful API using mongodb)
但是这些问题和我遇到的有点不同。
结构
java
|
helloWorld
|
web/ -- HelloWorldController
Application
Customer
CustomerRepository
ServletInitializer
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我所有与 JPA 相关的包都与我的应用程序文件处于同一级别。根据教程(https://spring.io/guides/gs/accessing-data-jpa/)这应该有效
我的应用类
package helloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
CustomerRepository customerRepository;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder …
Run Code Online (Sandbox Code Playgroud) 我使用 JPA Hibernate 开发了一个 SpringBoot 应用程序,它运行得很好。现在我必须开始测试,老实说我不知道如何解决这个错误:
APPLICATION FAILED TO START
***************************
Description:
Field playersRepository in model.RestService required a bean of type 'model.PlayersRepository' that could not be found.
Action:
Consider defining a bean of type 'model.PlayersRepository' in your configuration.
2018-08-23 13:16:51.009 ERROR 7327 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@50f6ac94] to prepare test instance [testing.ApplicationTestSuite@475b7792]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) …
Run Code Online (Sandbox Code Playgroud)