私はこのガイドに従って、依存関係管理がgraddleである既存のSpringBootプロジェクトにMySqlを追加しました。私は以下の通りチュートリアルで使用されるこれらの3クラスを追加したちょうどその時SpringBootアプリケーションがコントローラに定義されたリポジトリを見つけることができません
メイン/ javaの/ネット/コード/モデル/ 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 パッケージnet.code.repo; Webサービスコントローラと
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> {
}
メイン/ 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);
}
}
しかし、私は、アプリケーションを実行するすべての時間が、私は以下のメッセージ
を得続けます私はこれらの Spring Boot not autowiring @Repository、 @RestController in other package doesn't workのような関連する問題を探していますが、私 私のために動作しませんでしたそれらのリンクからの提案として解決できませんでし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
もここ Consider defining a bean of type 'package' in your configuration [Spring-Boot]を受け入れた解決策を試してみたかったが、私はそれを見つけます@EnableJpaRepositories
'@EnableJpaRepositories(basePackages =" <リポジトリパッケージ名> ")' – imk
で試してください。間違ったことをする必要があります。この同じコードは私のシステムでうまく動作します – Olantobi
何かが間違っているのは分かっていますが間違っていますか?しかし、@EnableJpaRepositoriesは私のIDEでコンパイルされません。このパッケージは欠けていて、入手方法はわかりません – olyjosh