2017-08-12 54 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); 
    } 
}