簡體   English   中英

春季啟動,不創建bean @ Controller,@ Service

[英]Spring boot, does not create beans @Controller, @Service

這是我的項目目錄結構。

在此處輸入圖片說明

所有控制器以及其他類和目錄(即bean)都在“ WebPortalApplication”類下,並且如Spring Boot doc所述,只要這些包位於“ main”類目錄中,我們就不會顯式指定要掃描bean的包。 , 對? 因此,當我運行“ 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;
    }
}

看來您正在使用JPA。 所有JPA實體必須使用@Entity注釋。

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

表示,角色類缺少@Entity批注。

我希望您的RoleRepository達到以下定義; 共享此文件將有助於進一步分析丟失的內容。

public interface RoleRepository implements CrudRepository<Role, Long> {
...
}

您啟用了回購協議了嗎?

@SpringBootApplication
@EnableJpaRepositories
public class WebPortalApplication {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM