silent-box
silent-box

Reputation: 1666

Spring MVC: User object bean best practive

I have a small Spring MVC application, where a few classes depends on User object.

I'd like to declare it as @Component, so i can access this bean from any place with @Autowired annotation.

@Component
public class MyUser implements User {

    // private fields
    // getters and setters

    public void fillByName(String username) {
        userDao.select(username);
    }

}

Obviously, User object should be unique for each user. I use Spring Security, so there is username in my SecurityContextHolder, which i can use to initialize User object.

What is the best practice for the initialization? Should i declare it like session scoped bean? Or should i keep it as singleton class and just update all fields for each request? Or maybe i should create a new User object from the Controller class for each request?

Upvotes: 1

Views: 251

Answers (1)

Stan
Stan

Reputation: 1430

Personally I don't like idea to have domain object as component. I would suggest to have MyUser just as class and get this object where needed via UserService (UserDao) which is a component. Also it's not a good design when domain object aware about DAO thought.

Upvotes: 2

Related Questions