3

In the following snippet, when I give the @ManagedBean a specific name, userService and authenticationService are not injected. But if I use only @ManagedBean without name, the injection works fine. Any idea?

@Component
@ManagedBean(name="user") // the injection doesn't work
//@ManagedBean // the injection works
@SessionScoped
public class UserManagedBean implements Serializable {

    // Spring User Service is injected...
    @Autowired
    UserService userService;
    @Autowired
    private AuthenticationService authenticationService;
Sasue Nie
  • 61
  • 1
  • 9

1 Answers1

2

when you use @ManagedBean(name="user") try specify the same name for component as well and see if that works.

@Component("user")
spiritwalker
  • 2,257
  • 14
  • 9
  • It works like a boss. Thanks a lot. I wish I could vote up, but I don't have enough reputation. Can you please explain why aslo? Another question is I cannot access that UserManagedBean from other managed bean with ManagedProperty. For example, ManagedProperty(value="#{user}"). It returns null. But it works fine with @Autowired. Any explanation? – Sasue Nie Mar 21 '13 at 09:52
  • don't worry about vote, as long as it works. about your first question. when you declare beans through spring stereotype annotations like **@Component**, if there is no name specified, spring takes class name(lower case of first character) as bean name, in your case it would be **userManagedBean**. It might confuse Spring when you specify the name in **@ManagedBean(name="user")**. So if you need to specify name, do it for both @ManagedBean and Spring stereotype annotation and keep the value same. – spiritwalker Mar 21 '13 at 10:37
  • I'm not sure about your second question, I need a bit more context to figure it out. But since you use Spring, it is always good to use **Spring approach** for IOC including bean declaration and injection, such as using @Autowired. – spiritwalker Mar 21 '13 at 11:05