Java Spring Boot@Mapper Bean创建问题:应用程序无法启动。错误:考虑定义类型bean

Java Spring Boot@Mapper Bean创建问题:应用程序无法启动。错误:考虑定义类型bean,java,spring,spring-boot,maven,spring-mvc,Java,Spring,Spring Boot,Maven,Spring Mvc,我是spring新手,当我尝试执行项目的mvn clean install时,出现以下问题: 错误 *************************** APPLICATION FAILED TO START *************************** **Description**: Field userService in com.example.accessingdatamysql.rest.MainController required a bean of type '

我是spring新手,当我尝试执行项目的
mvn clean install
时,出现以下问题:

错误

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

**Description**:

Field userService in com.example.accessingdatamysql.rest.MainController required a bean of type 'com.example.accessingdatamysql.service.UserService' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.accessingdatamysql.service.UserService' in your configuration.

问题在于,
MainController
中导入了“UserService”:

这可能是一件小事,但我不能绕过这个问题,下面我插入“UserService”和MainStart

UserService.java

package com.example.accessingdatamysql.service;

import com.example.accessingdatamysql.model.dto.UserDto;

public interface UserService {

    UserDto findFirstByName(String name);
    
    void create(UserDto user);
        
}

package com.example.accessingdatamysql.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;
import com.example.accessingdatamysql.model.repo.UserRepository;
import com.example.accessingdatamysql.util.UserMapper;

@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
      private UserRepository userRepository;
    
    @Autowired
    UserMapper mapper;

    @Override
    public UserDto findFirstByName(String name) {
           UserEntity entity = userRepository.findFirstByName(name);
           
        return mapper.toDtoMapper(entity);
    }

    @Override
    public void create(UserDto user) {
         UserEntity entity = mapper.toEntityMapper(user);
         
         userRepository.create(entity);
        
    }
 
}

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(scanBasePackages = { "com.example.accessingdatamysql",
"com.example.accessingdatamysql.util"})
public class AccessingDataMysqlApplication {

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

}
package com.example.accessingdatamysql.util;

import org.mapstruct.Mapper;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;


@Mapper (componentModel = "spring")
public interface UserMapper {

    UserEntity toEntityMapper (UserDto user);
    
    UserDto toDtoMapper (UserEntity userEntity);
}

更新: 我插入了userserviceinpl和新的main和Mapper,以及新的错误

UserServiceImpl.java

package com.example.accessingdatamysql.service;

import com.example.accessingdatamysql.model.dto.UserDto;

public interface UserService {

    UserDto findFirstByName(String name);
    
    void create(UserDto user);
        
}

package com.example.accessingdatamysql.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;
import com.example.accessingdatamysql.model.repo.UserRepository;
import com.example.accessingdatamysql.util.UserMapper;

@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
      private UserRepository userRepository;
    
    @Autowired
    UserMapper mapper;

    @Override
    public UserDto findFirstByName(String name) {
           UserEntity entity = userRepository.findFirstByName(name);
           
        return mapper.toDtoMapper(entity);
    }

    @Override
    public void create(UserDto user) {
         UserEntity entity = mapper.toEntityMapper(user);
         
         userRepository.create(entity);
        
    }
 
}

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(scanBasePackages = { "com.example.accessingdatamysql",
"com.example.accessingdatamysql.util"})
public class AccessingDataMysqlApplication {

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

}
package com.example.accessingdatamysql.util;

import org.mapstruct.Mapper;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;


@Mapper (componentModel = "spring")
public interface UserMapper {

    UserEntity toEntityMapper (UserDto user);
    
    UserDto toDtoMapper (UserEntity userEntity);
}

访问DatamysqlApplication.java

package com.example.accessingdatamysql.service;

import com.example.accessingdatamysql.model.dto.UserDto;

public interface UserService {

    UserDto findFirstByName(String name);
    
    void create(UserDto user);
        
}

package com.example.accessingdatamysql.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;
import com.example.accessingdatamysql.model.repo.UserRepository;
import com.example.accessingdatamysql.util.UserMapper;

@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
      private UserRepository userRepository;
    
    @Autowired
    UserMapper mapper;

    @Override
    public UserDto findFirstByName(String name) {
           UserEntity entity = userRepository.findFirstByName(name);
           
        return mapper.toDtoMapper(entity);
    }

    @Override
    public void create(UserDto user) {
         UserEntity entity = mapper.toEntityMapper(user);
         
         userRepository.create(entity);
        
    }
 
}

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(scanBasePackages = { "com.example.accessingdatamysql",
"com.example.accessingdatamysql.util"})
public class AccessingDataMysqlApplication {

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

}
package com.example.accessingdatamysql.util;

import org.mapstruct.Mapper;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;


@Mapper (componentModel = "spring")
public interface UserMapper {

    UserEntity toEntityMapper (UserDto user);
    
    UserDto toDtoMapper (UserEntity userEntity);
}

UserMapper.java

package com.example.accessingdatamysql.service;

import com.example.accessingdatamysql.model.dto.UserDto;

public interface UserService {

    UserDto findFirstByName(String name);
    
    void create(UserDto user);
        
}

package com.example.accessingdatamysql.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;
import com.example.accessingdatamysql.model.repo.UserRepository;
import com.example.accessingdatamysql.util.UserMapper;

@Service
public class UserServiceImpl implements UserService{
    
    @Autowired
      private UserRepository userRepository;
    
    @Autowired
    UserMapper mapper;

    @Override
    public UserDto findFirstByName(String name) {
           UserEntity entity = userRepository.findFirstByName(name);
           
        return mapper.toDtoMapper(entity);
    }

    @Override
    public void create(UserDto user) {
         UserEntity entity = mapper.toEntityMapper(user);
         
         userRepository.create(entity);
        
    }
 
}

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(scanBasePackages = { "com.example.accessingdatamysql",
"com.example.accessingdatamysql.util"})
public class AccessingDataMysqlApplication {

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

}
package com.example.accessingdatamysql.util;

import org.mapstruct.Mapper;

import com.example.accessingdatamysql.model.dto.UserDto;
import com.example.accessingdatamysql.model.entity.UserEntity;


@Mapper (componentModel = "spring")
public interface UserMapper {

    UserEntity toEntityMapper (UserDto user);
    
    UserDto toDtoMapper (UserEntity userEntity);
}

新错误

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

Description:

Field mapper in com.example.accessingdatamysql.service.UserServiceImpl required a bean of type 'com.example.accessingdatamysql.util.UserMapper' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.accessingdatamysql.util.UserMapper' in your configuration.

POM

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>accessingdatamysql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
            </properties>

    <dependencies>
            <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


4.0.0
org.springframework.boot
spring启动程序父级
2.3.4.1发布
com.example
访问DataMySQL
0.0.1-快照
项目
SpringBoot的演示项目
1.8
org.mapstruct
映射结构
1.3.1.最终版本
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
mysql
mysql连接器java
运行时
org.springframework.boot
弹簧起动试验
测验
org.junit.vintage
朱尼特老式发动机
org.springframework.boot
springbootmaven插件

@Service
@Component
注释您的UserService类实现[如
UserServiceImpl.java
]。还要确保此类位于子包中

这是您的主类包:
com.example.accessingdatamysql
您的UserService类和所有其他类应保存在一个包中,如:
com.example.accessingdatamysql.xxxxxx
。确保遵循此策略

另外,删除主类上不必要的注释。@SpringBootApplication注释相当于使用以下3个: :

  • @配置
  • @启用自动配置和
  • @使用属性扫描组件
  • 这就足够了:

    @SpringBootApplication (scanBasePackages = "com.example.accessingdatamysql")
    
    当你自动连线任何豆子注射时,不要留空隙。这不会造成任何伤害。但是你的代码应该被正确的组织和缩进

    也替换如下:

     @Autowired 
             
    private UserService userService;
    
    为此:

     @Autowired          
     private UserService userService;
    
    更新-1

    在修复spring引导配置后,执行maven clean安装

    mvn清洁安装

    更新-2

    您的映射器bean不完全符合Springbean的条件。您需要使用下面的插件编译您的项目(请参阅我使用的第二个插件)

    您的主类应该只有以下内容:
    @SpringBootApplication(scanBasePackages=“com.example.accessingdatamysql”)
    ,而没有其他注释

    然后保存您的项目。然后运行:mvn清洁安装-X

    使您的包结构如下所示:

    您的课程安排如下:


    您需要定义一个bean,让Spring使用它们,比如DI(依赖注入)
    @Autowired

    定义bean有多种方法,但在您的例子中,在服务类上使用
    @Service
    注释,以便Spring可以在初始化期间找到它

    您不需要声明
    @ComponentScan
    注释
    @springbootplication
    将处理所有事情

    正如@Som所说,您需要实现
    Userservice
    类,因为它是接口,还没有实现类

    尝试删除
    @ComponentScan
    注释并实现
    UserService
    界面,如下所示

    @服务
    公共类UserServiceImpl实现UserService{
    //代码的其余部分
    }
    
    Hi-Som!我加载了UserServiceImpl类、映射器和新的Main,但现在我有一个类似的映射器问题,即使Autowired在我看来放置正确。进行maven clean安装,这将解决您的问题。例如:
    mvn clean install
    你能粘贴你在@Mapper中得到的确切错误吗?我把错误放在更新中,如果你是指maven返回给我的内容,我会在你的Mapper类中插入并保留这个构造函数
    public UserMapper实例=Mappers.getMapper(UserMapper.class)我认为您的配置是正确的。maven干净的构建应该可以解决您的问题。还要检查您是否正在使用映射器的任何实现,或者您只是在使用默认配置。您好!我加载了UserServiceImpl类、映射器和新的Main,但是现在我有一个类似的映射器问题,尽管Autowired在我看来似乎被正确地放置了,但是尝试了,什么都没有changes@Jacket对不起,我上次的评论是错的。查看