工作中遇到的问题--实现程序运行时就加载CustomerSetting的第二种方法

写一个自定义注解

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface CurrentCustomerSettings {

}

在web初始化类中添加:

@Configuration
@Order(3)
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MFGWebSecurityConfigurerAdapter extends
        AWebSecurityConfigurerAdapter {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CustomerSettingsRepository customerSettingsRepository;

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .formLogin()
                .successHandler(
                        new SavedRequestAwareAuthenticationSuccessHandler())
                .loginPage("/login").permitAll().failureUrl("/login-error")
                .defaultSuccessUrl("/").and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/logged-out").permitAll().and().rememberMe()
                .key(SECURITY_TOKEN)
                .tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(mfgSettings.getRememberMeTokenValidity())
                .and().sessionManagement().maximumSessions(1)
                .sessionRegistry(sessionRegistry).and().sessionFixation()
                .migrateSession().and().authorizeRequests().anyRequest()
                .authenticated();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        return tokenRepository;
    }

    @Bean
    @LoggedInUser
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    @Transactional(readOnly = true)
    public User getLoggedInUser() {
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (authentication != null
                && !(authentication instanceof AnonymousAuthenticationToken)
                && authentication.isAuthenticated())
            return userRepository.findByLogin(authentication.getName());
        return null;
    }

    @Bean
    @SystemUser
    @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.NO)
    @Transactional(readOnly = true)
    public User getSystemUser() {
        return userRepository.findByLogin(Constants.SYSTEM_USER);
    }

    @Bean

    @CurrentCustomerSettings
    @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.NO)
    public CustomerSettings customerSettings() {
        return customerSettingsRepository.findAll().get(0);
    }

以后在注入的时候,只需要写:

@CurrentCustomerSettings

@Autowired

CustomerSettings customerSettings;

原文地址:https://www.cnblogs.com/ly-radiata/p/4789678.html