Spring Boot组件需要找不到类型为'javax.persistence.EntityManagerFactory'的bean

时间:2020-05-21 18:30:59

标签: java spring hibernate spring-boot entitymanager

我用spring boot创建了不同的微服务。

我的RequestController:

@RestController
@RequestMapping(value = "/xyz", produces = "application/json")
public class EmoController {

    @Autowired
    private UserServiceImpl userServiceImpl;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ResponseEntity<User> getUser() {
        return new ResponseEntity<>(userServiceImpl.findByUsername("emoleumassi"), HttpStatus.OK);
    }

    ...

UserServiceImpl在另一个项目test-service中。我将其添加为依赖项。

我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.emo.test</groupId>
        <artifactId>test-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <artifactId>test-registration</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>test-registration</name>
    <url>http://maven.apache.org</url>

    <properties>
        <oauth.version>2.3.5.RELEASE</oauth.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>${oauth.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.emo.test</groupId>
            <artifactId>test-service</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- API, java.xml.bind module -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>

        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>

</project>

我的主要班级:

@SpringBootApplication(scanBasePackages= {"com.test.api.serviceImpl"}, exclude = {DataSourceAutoConfiguration.class})
public class RegistrationApplication {

    public static void main(String[] args) {
        SpringApplication.run(RegistrationApplication.class, args);
    }
}

com.test.api.serviceImplUserServiceImpl

的软件包

UserServiceImpl类:

@Transactional
@Service("userServiceImpl")
public class UserServiceImpl {

    @PersistenceContext
    private EntityManager entityManager;

    public User findByUsername(String username) throws EmptyResultDataAccessException {
        String query = "FROM User WHERE username = :username";
        return (User) entityManager.createQuery(query).setParameter("username", username).getSingleResult();
    }
}

我的父母pom

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.test.kazi</groupId>
    <artifactId>kazi-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- GrantedAuthority wird in kazi project bei Role verwendet -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-plugin-releases</id>
            <url>https://repo.spring.io/plugins-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

开始时,我收到此错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-21 20:18:34.343 ERROR 4281 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:51998', transport: 'socket'

Process finished with exit code 0

0 个答案:

没有答案