2017-08-12 113 views
0

这是我的项目目录结构。Spring引导,不创建bean @Controller,@Service

enter image description here

所有控制器和其它类和目录,这是豆子,是“WebPortalApplication”类下,并作为春季启动文档状态,我们并没有明确指定包扫描豆子,只要这些软件包可以找到“主”类目录,对吧? 因此,当我运行“WebPortalApplication”文件时,它会生成,但有这种例外。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRestController': Unsatisfied dependency expressed through field 'userService'; 

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'roleRepository'; 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.epam.webPortal.model.Role 

@RestController 公共类UserRestController {

@Autowired 
UserService userService; 
private static final Logger LOGGER = LoggerFactory.getLogger(UserRestController.class); 

//-------------------Retrieve All Users-------------------------------------------------------- 
@RequestMapping(value = "/user/", method = RequestMethod.GET) 
public String listAllUsers() { 
    String userAsJson = ""; 
    List<User> users = userService.findAllUsers(); 
    try { 
     userAsJson = JsonConvertor.toJson(users); 
    } catch (Exception ex) { 
     LOGGER.error("Something went wrong during converting json format"); 
    } 
    LOGGER.info("displaying all users in json format"); 
    return userAsJson; 

} 

package com.epam.webPortal.service.user; 

import com.epam.webPortal.model.User; 
import com.epam.webPortal.repository.role.RoleRepository; 
import com.epam.webPortal.repository.user.UserRepository; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional 

import java.util.Date; 
import java.util.HashSet; 
import java.util.List; 

@Service("userService") 
@Transactional 
public class UserServiceImpl implements UserService { 

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class); 

    @Autowired 
    private UserRepository userRepository; 
    @Autowired 
    private RoleRepository roleRepository; 
    @Autowired 
    private BCryptPasswordEncoder bCryptPasswordEncoder; 

    @Override 
    public void saveUser(User user) { 
     user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); 
     user.setRoles(new HashSet<>(roleRepository.findAll())); 
     user.setDateRegistered(new Date()); 
     userRepository.save(user); 
     LOGGER.info("user with username {} successfully saved", user.getUsername()); 
    } 

    @Override 
    public User findByUsername(String username) { 
     return userRepository.findByUsername(username); 
    } 

    @Override 
    public List<User> findAllUsers() { 
     return userRepository.findAllUsers(); 
    } 

    @Override 
    public User findById(Long Id) { 
     return userRepository.findById(Id); 
    } 

    @Override 
    public void updateUser(User user) { 
     final User entity = userRepository.findById(user.getId()); 
     if (entity != null) { 
      entity.setFirstName(user.getFirstName()); 
      entity.setLastName(user.getLastName()); 
      entity.setEmail(user.getEmail()); 
      entity.setSkypeID(user.getSkypeID()); 
      entity.setDateRegistered(new Date()); 
      entity.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); 
      userRepository.save(entity); 
      LOGGER.info("user with id {} successfully updated", user.getId()); 
     } 
    } 

    @Override 
    public void deleteUserById(Long id) { 
     userRepository.deleteById(id); 
     LOGGER.info("user with id {} successfully deleted", id); 
    } 
} 

package com.epam.webPortal.model; 

import javax.persistence.*; 
import java.util.Set; 

@Entity 
@Table(name = "role") 
public class Role { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 
    private String name; 

    @ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER) 
    private Set<User> users; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Set<User> getUsers() { 
     return users; 
    } 

    public void setUsers(Set<User> users) { 
     this.users = users; 
    } 
} 
+0

图片没有帮助。发布您的课程简化版。 – efekctive

+0

粘贴“UserRestController”,“UserService”和“Role”的“相关”代码(定义和构造函数)... –

回答

0

看起来你正在使用JPA。 所有JPA实体必须使用@Entity进行注释。

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.epam.webPortal.model.Role 

表示角色类缺失@Entity注释。

+0

是的,它们被注释为 –

+0

角色类别 –

0

你能分享一个RoleRepository.java的定义吗?你看到这是从CrudRepository派生的接口/类吗?

我期待你的RoleRepository的定义如下;共享此文件将有助于进一步分析缺少的内容。

公共接口RoleRepository实现CrudRepository { ... }

0

您已经启用了回购协议?

@SpringBootApplication 
@EnableJpaRepositories 
public class WebPortalApplication { 

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