包中方法setUserService的参数0需要另一个包类型的Bean

时间:2018-12-30 17:28:07

标签: java spring-boot

此代码没有错误,但是当我运行它时,出现此错误: 说明:

  

com.pardis.server.bootstrap.SpringJpaBootstrap中的setUserService方法的参数0需要一个类型为'com.pardis.server.services.UserService'的bean。

操作:

考虑在您的配置中定义类型为“ com.pardis.server.services.UserService”的bean。

SpringJpaBootstrap类:

@Component
public class SpringJpaBootstrap implements ApplicationListener<ContextRefreshedEvent> {


private UserService userService;
private RoleService roleService;

private Logger log = Logger.getLogger(SpringJpaBootstrap.class);



@Autowired
public void setUserService(UserService userService) {
    this.userService = userService;
}

@Autowired
public void setRoleService(RoleService roleService) {
    this.roleService = roleService;
}


@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    loadUsers();
    loadRoles();
    assignUsersToUserRole();
    assignUsersToAdminRole();
}



private void loadUsers() {
    User user1 = new User();
    user1.setUsername("user");
    user1.setPassword("user");
    userService.saveOrUpdate(user1);

    User user2 = new User();
    user2.setUsername("admin");
    user2.setPassword("admin");
    userService.saveOrUpdate(user2);

}

private void loadRoles() {
    Role role = new Role();
    role.setRole("USER");
    roleService.saveOrUpdate(role);
    log.info("Saved role" + role.getRole());
    Role adminRole = new Role();
    adminRole.setRole("ADMIN");
    roleService.saveOrUpdate(adminRole);
    log.info("Saved role" + adminRole.getRole());
}
private void assignUsersToUserRole() {
    List<Role> roles = (List<Role>) roleService.listAll();
    List<User> users = (List<User>) userService.listAll();

    roles.forEach(role -> {
        if (role.getRole().equalsIgnoreCase("USER")) {
            users.forEach(user -> {
                if (user.getUsername().equals("user")) {
                    user.addRole(role);
                    userService.saveOrUpdate(user);
                }
            });
        }
    });
}
private void assignUsersToAdminRole() {
    List<Role> roles = (List<Role>) roleService.listAll();
    List<User> users = (List<User>) userService.listAll();

    roles.forEach(role -> {
        if (role.getRole().equalsIgnoreCase("ADMIN")) {
            users.forEach(user -> {
                if (user.getUsername().equals("admin")) {
                    user.addRole(role);
                    userService.saveOrUpdate(user);
                }
            });
        }
    });
}
}

userService类:

package com.pardis.server.services;

import com.pardis.server.model.User;

public interface UserService extends CRUDService<User> {

    User findByUsername(String username);

}

CRUDservice类:

包com.pardis.server.services;

import java.util.List;

公共接口CRUDService {     列出listAll();

T getById(Integer id);

T saveOrUpdate(T domainObject);

void delete(Integer id);

}

0 个答案:

没有答案