2017-10-21 80 views
0

我按照本指南将MySql添加到其现有的依赖管理处于渐进式的SpringBoot项目中。正当我加入教程作为遵循这三个类SpringBoot应用程序在控制器中找不到定义的存储库

主/ JAVA/NET /代码/模型/ Users.Java

package net.code.controller; 
import net.code.model.User; 
import net.code.repo.UserRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.*; 


@RestController // This means that this class is a Controller 
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path) 
public class MainController { 
    @Autowired // This means to get the bean called userRepository 
    // Which is auto-generated by Spring, we will use it to handle the data 
    private UserRepository userRepository; 

    @GetMapping(path="/add") // Map ONLY GET Requests 
    public @ResponseBody String addNewUser (@RequestParam String name 
      , @RequestParam String email) { 
     // @ResponseBody means the returned String is the response, not a view name 
     // @RequestParam means it is a parameter from the GET or POST request 

     User n = new User(); 
     n.setName(name); 
     n.setEmail(email); 
     userRepository.save(n); 
     return "Saved"; 
    } 

    @GetMapping(path="/all") 
    public @ResponseBody Iterable<User> getAllUsers() { 
     // This returns a JSON or XML with the users 
     return userRepository.findAll(); 
    } 
} 

和用户系统信息库为 主/ JAVA /网络/代码/repo/UserRepository.Java package net.code.repo;

import net.code.model.User; 
import org.springframework.data.repository.CrudRepository; 
import org.springframework.stereotype.Repository; 

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository 
// CRUD refers Create, Read, Update, Delete 

@Repository 
public interface UserRepository extends CrudRepository<User, Long> { 

} 

与一个web服务控制器 主/ JAVA /净/代码/控制器/ MainController.Java

package net.code.controller; 

import net.code.model.User; 
import net.code.repo.UserRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.*; 


@RestController // This means that this class is a Controller 
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path) 
public class MainController { 
    @Autowired // This means to get the bean called userRepository 
    // Which is auto-generated by Spring, we will use it to handle the data 
    private UserRepository userRepository; 

    @GetMapping(path="/add") // Map ONLY GET Requests 
    public @ResponseBody String addNewUser (@RequestParam String name 
      , @RequestParam String email) { 
     // @ResponseBody means the returned String is the response, not a view name 
     // @RequestParam means it is a parameter from the GET or POST request 

     User n = new User(); 
     n.setName(name); 
     n.setEmail(email); 
     userRepository.save(n); 
     return "Saved"; 
    } 

    @GetMapping(path="/all") 
    public @ResponseBody Iterable<User> getAllUsers() { 
     // This returns a JSON or XML with the users 
     return userRepository.findAll(); 
    } 
} 

我与@SpringBoot 主/ JAVA /净/代码类/ App.Java

package net.code; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

//@CrossOrigin(origins = "http://127.0.0.1:8080") 

@SpringBootApplication 

@ComponentScan("net.code") 
//@ComponentScan(basePackages = { "net.code","net.code.repo"}) 

//@RestController 
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 
public class App extends WebMvcConfigurerAdapter { 

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

} 

但是,任何时候我运行应用程序,我不断收到以下消息

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 
2017-10-21 15:11:59.674 ERROR 67424 --- [   main] o.s.b.d.LoggingFailureAnalysisReporter : 

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

Description: 

Field userRepository in net.code.controller.MainController required a bean of type 'net.code.repo.UserRepository' that could not be found. 


Action: 

Consider defining a bean of type 'net.code.repo.UserRepository' in your configuration. 


Process finished with exit code 1 

我寻找这样的 Spring Boot not autowiring @Repository@RestController in other package doesn't work相关的问题,但无法修复的那些链接的建议对我来说 我没有工作,也想在这里尝试接受的解决方案 Consider defining a bean of type 'package' in your configuration [Spring-Boot],但我发现,有没有这样的包@EnableJpaRepositories

请帮我在这,因为我一直在努力,现在解决这个问题的天

+0

尝试使用'@EnableJpaRepositories(basePackages =“”)' – imk

+0

您必须做错某事。这个相同的代码在我的系统上运行良好 – Olantobi

+0

我知道一些错误,但是错误?然而@EnableJpaRepositories不能在我的IDE中编译,就像缺少一个包并且我不知道如何获得 – olyjosh

回答

相关问题