我无法 运行 spring 启动
I am not able to run spring boot
我已经使用 gradle 创建了 spring 引导数据 jpa 应用程序,我的项目结构如下所示。
com.duregesh
--TestSpringBootDataJpaApplication.java
com.durgesh.controller
--UserController.java
com.durgesh.model
--User.java
com.durgesh.repositories
--UserJpaRepository.java
com.durgesh.services
--UserServiceImpl.java
当我运行时 spring 引导低于异常正在触发
说明:
com.durgesh.services.UserServiceImpl 中的字段 userJpaRepository 需要一个名为 'emf' 的 bean,但找不到。
行动:
考虑在您的配置中定义一个名为 'emf' 的 bean。
@SpringBootApplication
public class TestSpringBootDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}
-------------
public interface UserJpaRepository extends JpaRepository<User, Long> {}
-------------
@Entity
@Table(name = "USER")
public class User implements Serializable{
@Id
private Long id;
@Column(unique = true)
private String uid;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
----------------
@Service
@EnableJpaRepositories(basePackages="com.construction.de.*", entityManagerFactoryRef="emf")
public class UserServiceImpl implements UserService {
@Autowired
private UserJpaRepository userJpaRepository;
@Override
public User add(final User user) {
return userJpaRepository.save(user);
}
@Override
public User findById(final Long id) {
final User user = userJpaRepository.findOne(id);
return user;
}
}
----
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value ="/",method = RequestMethod.POST)
public User add( @RequestBody final User user){
return userService.add(user);
}
@RequestMapping(value ="/{id}",method = RequestMethod.GET)
public User findById(@PathVariable("id") final Long id){
return userService.findById(id);
}
}
问题出在您的数据库 entityManager
配置上,您需要按照以下步骤操作:
(1) 如下所示向应用程序 class 提供 @EnableJpaRepositories
(将其从服务中删除)。
@SpringBootApplication
@EnableJpaRepositories(basePackages="com.duregesh.*")
public class TestSpringBootDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}
(2) 提供在application.properties
中配置的数据库驱动程序和连接属性(在eclipse中的src/main/resources
或服务器中的WEB-INF/classes
文件夹下),如下所示:
spring.datasource.url: YOUR_DB_URL
spring.datasource.driverClassName: YOUR_DB_DRIVER_CLASS
spring.datasource.username: YOUR_DB_USER
spring.datasource.password: YOUR_DB_PWD
我已经使用 gradle 创建了 spring 引导数据 jpa 应用程序,我的项目结构如下所示。
com.duregesh
--TestSpringBootDataJpaApplication.java
com.durgesh.controller
--UserController.java
com.durgesh.model
--User.java
com.durgesh.repositories
--UserJpaRepository.java
com.durgesh.services
--UserServiceImpl.java
当我运行时 spring 引导低于异常正在触发 说明:
com.durgesh.services.UserServiceImpl 中的字段 userJpaRepository 需要一个名为 'emf' 的 bean,但找不到。
行动: 考虑在您的配置中定义一个名为 'emf' 的 bean。
@SpringBootApplication
public class TestSpringBootDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}
-------------
public interface UserJpaRepository extends JpaRepository<User, Long> {}
-------------
@Entity
@Table(name = "USER")
public class User implements Serializable{
@Id
private Long id;
@Column(unique = true)
private String uid;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
----------------
@Service
@EnableJpaRepositories(basePackages="com.construction.de.*", entityManagerFactoryRef="emf")
public class UserServiceImpl implements UserService {
@Autowired
private UserJpaRepository userJpaRepository;
@Override
public User add(final User user) {
return userJpaRepository.save(user);
}
@Override
public User findById(final Long id) {
final User user = userJpaRepository.findOne(id);
return user;
}
}
----
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value ="/",method = RequestMethod.POST)
public User add( @RequestBody final User user){
return userService.add(user);
}
@RequestMapping(value ="/{id}",method = RequestMethod.GET)
public User findById(@PathVariable("id") final Long id){
return userService.findById(id);
}
}
问题出在您的数据库 entityManager
配置上,您需要按照以下步骤操作:
(1) 如下所示向应用程序 class 提供 @EnableJpaRepositories
(将其从服务中删除)。
@SpringBootApplication
@EnableJpaRepositories(basePackages="com.duregesh.*")
public class TestSpringBootDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}
(2) 提供在application.properties
中配置的数据库驱动程序和连接属性(在eclipse中的src/main/resources
或服务器中的WEB-INF/classes
文件夹下),如下所示:
spring.datasource.url: YOUR_DB_URL
spring.datasource.driverClassName: YOUR_DB_DRIVER_CLASS
spring.datasource.username: YOUR_DB_USER
spring.datasource.password: YOUR_DB_PWD