Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自动连线导致spring启动应用程序中出现异常_Java_Spring_Spring Boot - Fatal编程技术网

Java 自动连线导致spring启动应用程序中出现异常

Java 自动连线导致spring启动应用程序中出现异常,java,spring,spring-boot,Java,Spring,Spring Boot,我在让自动连线注释在我的spring boot应用程序中工作时遇到问题 我收到的错误是“无法自动连接字段。没有类型为的限定bean” 我已经验证了控制器上的代码不是POJO,并且具有Spring注释 我也无法在包之外运行我的主方法。有什么建议吗 package com.xxx.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.S

我在让自动连线注释在我的spring boot应用程序中工作时遇到问题

我收到的错误是“无法自动连接字段。没有类型为的限定bean”

我已经验证了控制器上的代码不是POJO,并且具有Spring注释

我也无法在包之外运行我的主方法。有什么建议吗

package com.xxx.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SampleWebJspApplication.class);
    }

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

}
用户服务类

package com.xxx.service;

    import java.util.List;

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

    import com.xxx.entity.User;
    import com.xxx.repository.UserRepository;

    @Service
    public class UserService {

        @Autowired
        private UserRepository userRepository;

        public List<User> findAll() {
            return userRepository.findAll();
        }

    }
package com.xxx.service;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入com.xxx.entity.User;
导入com.xxx.repository.UserRepository;
@服务
公共类用户服务{
@自动连线
私有用户存储库用户存储库;
公共列表findAll(){
返回userRepository.findAll();
}
}
用户存储库类

package com.xxx.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.xxx.entity.User;

public interface UserRepository extends JpaRepository<User, Integer> {

}
package com.xxx.repository;
导入org.springframework.data.jpa.repository.JpaRepository;
导入com.xxx.entity.User;
公共接口用户存储库扩展了JpaRepository{
}

可能有两个原因:

  • 组件的包不包括在
    @ComponentScan
    包中
  • 存储库的包不包括在
    @EnableJPARepositories
    包中

    • 可能有两个原因:

      • 组件的包不包括在
        @ComponentScan
        包中
      • 存储库的包不包括在
        @EnableJPARepositories
        包中

      您的@SpringBootApplication注释位于包“com.xxx.controller”中,因此Spring将能够将此包中的组件扫描到其中的包中

      如果未定义特定的包,将从声明此注释的类的包中进行扫描

      尝试:

      • 在@SpringBootApplication下添加@ComponentScan({“com.xxx”}) 或者修改为@SpringBootApplication(scanBasePackages={“com.xxx”})

      • 只需将类“SampleWebJspApplication”移动到包“com.xxx”。这可能是最好的解决方案,只要你的这个类看起来不像控制器

      您的@SpringBootApplication注释位于包“com.xxx.controller”中,因此Spring将能够将此包中的组件扫描到其中的包中

      如果未定义特定的包,将从声明此注释的类的包中进行扫描

      尝试:

      • 在@SpringBootApplication下添加@ComponentScan({“com.xxx”}) 或者修改为@SpringBootApplication(scanBasePackages={“com.xxx”})

      • 只需将类“SampleWebJspApplication”移动到包“com.xxx”。这可能是最好的解决方案,只要你的这个类看起来不像控制器

      我删除了SpringBootApplication注释,并将其替换为ComponentScan和EnableJpaRepositories。然后,我添加必要的基本包。这并没有解决问题。还有其他建议吗?我删除了SpringBootApplication注释,并将其替换为ComponentScan和EnableJpaRepositories。然后,我添加必要的基本包。这并没有解决问题。还有其他建议吗?