Search code examples
jakarta-eecdiweld

How to Get Updated Value from Injected Instance


A named bean, listProjects, requires authentication which is provided by the container. I want to get the username from the named loginMBean and use it in the listProjects bean.

I have injected the authentication bean into listProjects, but when I access the username, it it null even though the user has authenticated.

@Named("loginMBean")
@SessionScoped
@PermitAll
public class LoginMBean implements Serializable {
private String username;

public String getUsername() {
    return username;
} 



@Named
@SessionScoped
@RolesAllowed({"Users"})
public class ListProjectsMBean implements Serializable {
@Inject
private LoginMBean wLoginMBean;

public void getList(ActionEvent actionEvent) {
        String testUserName = wLoginMBean.getUsername();
  l     
}

wildfly 8.2 weld 2.2 java 1.7


Solution

  • The injection is ok, the username is probably not set that is why it is null.

    Try to inject the principal in the ListProjectsMBean to see if the authentication was successful.

    @Inject Principal principal;