Почему бины с аннотациями @SessionScoped, @ViewScoped создаются при старте?

Есть приложение Spring security, если аннотировать бины @SessionScoped или @ViewScoped, то бины создаются сразу при старте приложения. Если же аннотировать бин @Scope("session"), то сперва я могу ввести логин и пароль, а затем создается бин. Т.е есть страница логина, я ввожу логин и пароль, если они верные, то происходит редирект на другую страницу, к которой привязан бин.

@Component
@Scope("session")
public class MainPageBean {

@Autowired
private UserService userService;


private User user;


@PostConstruct
public void onCreate() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    user = userService.findByUsername(auth.getName());
    period=0;
}
....
}

и метод onCreate() спокойно срабатывает. Однако, если же этот же бин я аннотирую @SessionScoped или @ViewScoped

@Component
@SessionScoped
public class MainPageBean {

@Autowired
private UserService userService;


private User user;


@PostConstruct
public void onCreate() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    user = userService.findByUsername(auth.getName());
    period=0;
}
....
}

То бин создается сразу при старте приложения, соответсвенно метод onCreate() выбрасывает Exception т.к. я еще не авторизован и auth = null. Почему так происходит, если @SessionScoped и @Scope("session") должны срабатывать одинаково. И как, используя @SessionScoped, мне добиться того же эффекта, как при использовании @Scope("session"). Также мне необходимо использовать @ViewScoped, который должен создаваться каждый раз при открытии новой страницы в браузере, однако он создается также при старте приложения, что для меня слишком рано, ведь я должен успеть авторизоваться.

appconfig-security.xml

         <?xml version="1.0" encoding="UTF-8"?>
          <beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">

<http use-expressions="true" auto-config="true">
    <intercept-url pattern="/views/*" access="permitAll()" requires-channel="http"/>
    <intercept-url pattern="/views/info/*" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" requires-channel="http"/>
    <intercept-url pattern="/views/admin/*" access="hasRole('ROLE_ADMIN')" requires-channel="http"/>

    <form-login login-page="/views/login.xhtml" authentication-failure-url="/login?error"
                username-parameter="username" password-parameter="password"/>

    <logout delete-cookies="true" logout-success-url="/login?logout"/>
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userDetailsServiceImpl">
        <password-encoder ref="encoder"></password-encoder>
    </authentication-provider>
</authentication-manager>

<beans:bean id="userDetailsServiceImpl"
            class="com.special.nco.monitor.service.UserDetailsServiceImpl"/>

<beans:bean id="encoder"
            class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <beans:constructor-arg name="strength" value="11"/>
</beans:bean>
</beans:beans>

Ответы (0 шт):