服务bean没有getter和setter

时间:2013-12-30 05:55:43

标签: java spring spring-mvc

我使用的是spring mvc,我有一个名为UserManager的服务类。该类用于管理用户集合,例如添加用户和从集合中删除用户。 Basicaaly它提供了有关用户集合的所有信息。 控制器使用此类来访问用户集合信息。 现在的问题是我必须将它用作弹簧注射的豆子。但是bean应该只有getter和setter。 所以我对如何实现这个类感到困惑。

这是UserManager的代码

import com.bo.user.UserBO;

/*
 * UserManager class is a service class which provides service to Controller for managing the users in the system.
 * It has a collection _allUserMap which maintains the users inside the system all through the life of system.
 * It manages the addition, deletion and updation of users. 
 * UserBO is the service which helps UserManager access the users, individually, from Database 
 */
@Service
public class UserManager{
    @Autowired
    private UserBO userBo;
    private static Map<Integer,User> _allUserMap = new HashMap<Integer, User>();

    /*
     * Method populates the _allUserMap
     * using userBo
     */
    @PostConstruct
    public void loadAllUsers(){
        Integer id = null;
        List<User> _allUserList = userBo.listAllUser();
        System.out.println("<--------Initializing all user map--------->");
        for(User user : _allUserList){
            id = user.getId();
            _allUserMap.put(id, user);
        }
    }

    /*
     * Adds the user after checking if the user exists
     * @param User:Takes the User to add from the Controller
     * @Return boolean User added or not
     * Beta 1.1 validation for correct user addition form input
     */
    public boolean addUser(User user){
        boolean userAdded = false;
        if (hasUser(user)){
            userAdded  = false;
        }else{
            userBo.save(user);
            userAdded = true;
        }
        return userAdded;
    }

    /*
     * Checks if the user is already present 
     * @Param User
     * @Return is user present 
     */
    private boolean hasUser(User formUser){
        boolean isUser = false;
        User user = null;
        for(Entry<Integer, User> entry: _allUserMap.entrySet()){
            user  = entry.getValue();
            if(user.equals(formUser)){
                isUser = true;
            }
            return isUser;
        }
        return isUser;
    }

    /*
     * @Param User
     * @Return String : message gives what feild is alreay in database
     */
    public String matchCredentails(User formUser){
        String message = "";
        User user = null;
        for(Entry<Integer, User> entry: _allUserMap.entrySet()){
            user  = entry.getValue();
            if(user.getEmail().equals(formUser.getEmail())){
                message = "Email alreay exists+";
            }
            if(user.getMobileNumber()== formUser.getMobileNumber()){
                message = message + "Mobile number alreay exists+";
            }

            if(user.getUserName().equals(formUser.getUserName())){
                message = message + "UserName alreay exists+";
            }
        }
        return message;
    }
}

这是我在控制器中访问它的方式

 @Controller
public class UserController {   
    //These are the instances of the service providing bean and not the state of the spring controller
    @Autowired
    private UserManager userManager;

我的问题很简单......我应该把这个类变成一个bean。因为根据定义,这个类不是一个简单的pojo。

1 个答案:

答案 0 :(得分:1)

不,由于您通过UserBO注入@Autowired字段,因此绝对不需要getter和setter。

Spring文档说

  

bean只是一个实例化,组装和实现的对象   否则由Spring IoC容器管理;除此之外,还有   豆子没什么特别的。[...]。

没有提到getter / setter,因此你不应该认为它们是 beans 所必需的。适当时使用它们。