2017-02-27 181 views
0

我有一個屬性的類配置屬性爲空春季啓動

@ConfigurationProperties(prefix = ShiroProperties.SHIRO_PREFIX) 
public class ShiroProperties { 
    public static final String SHIRO_PREFIX = "shiro"; 

    private String urlLogin; 
    private String urlSuccessed; 

和配置類

@Configuration 
@EnableConfigurationProperties({ ShiroProperties.class }) 
public class ShiroConfig implements ApplicationContextAware { 

    ApplicationContext applicationContext; 
    @Autowired 
    private ShiroProperties shiroProperties ; 

shiroProperties是空的,但我可以找到它在ShiroConfig值使用

applicationContext.getBean(ShiroProperties.class) 

我的應用等級:

@SpringBootApplication 
public class Bootstrap { 
    public static void main(String[] args) { 
    SpringApplication.run(Bootstrap.class, args); 
    } 
} 

奇怪的是,我可以在其他項目中使用類似的代碼運行成功,但是這個。

+0

我不知道,但我覺得你應該加上'@ Component'爲'ShiroProperties'(按[春季啓動外部配置文件](HTTPS ://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)) –

+0

我看到之前,我試過了,但它沒有工作。無論如何,謝謝 – Dean

+0

我發現這個奇怪問題的原因,如果添加由'@ Bean'註釋的'LifecycleBeanPostProcessor',那麼'@ Autowired'不起作用。 'LifecycleBeanPostProcessor'類來自shiro。 – Dean

回答

0

您可以嘗試在ShiroProperties.class添加@Component

@Component 
@ConfigurationProperties(prefix = ShiroProperties.SHIRO_PREFIX) 
public class ShiroProperties { 
    public static final String SHIRO_PREFIX = "shiro"; 

    private String urlLogin; 
    private String urlSuccessed; 
} 
+0

謝謝,它不工作,添加''@ Component''會拋出NoUniqueBeanDefinitionException,換句話說''ConfigurationProperties''已經在Spring Context中創建bean – Dean

+0

嘗試移除applicationContext.getBean(ShiroProperties.class); – mhshimul

0

這是爲你的應用的另一個配置類應@Configuration註釋加以裝飾,使一個bean創建和注入的背景下從另一個類佈線。

+0

對不起,我不明白你的意思 – Dean

+0

對不起,不清楚。我的意思是把你的班級更新爲'@Configuration @ConfigurationProperties(prefix = ShiroProperties.SHIRO_PREFIX) public class ShiroProperties {0} {0} public static final String SHIRO_PREFIX =「shiro」; private String urlLogin; 私人字符串urlSuccessed;' –

0

我遇到了同樣的問題作爲@Dean說,我做的就是把LifecycleBeanPostProcessor豆是另一種配置類,而在另一個配置類配置其他四郎,見下面的例子:

@Configuration 
public class ShiroLifecycleBeanPostProcessorConfig { 

    /** 
    * 
    * 
    * @return 
    */ 
    @Bean(name = "lifecycleBeanPostProcessor") 
    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() { 
     return new LifecycleBeanPostProcessor(); 
    } 

} 

主要四郎配置分類:

@Configuration 
@AutoConfigureAfter(value = ShiroLifecycleBeanPostProcessorConfig.class) 
public class ShiroConfiguration { 

    public static final String cacheFile = "encache.xml"; 
    private static final String active_cache_name = "activeSessionCache"; 

    @Autowired 
    private RedisTemplate<String, Object> redisTemplate; 

    /** 
    * 
    * 
    * @throws UnknownHostException 
    */ 
    @Bean(name = "shiroFilter") 
    @ConditionalOnMissingBean 
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) 
      throws UnknownHostException { 
     ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); 

     shiroFilterFactoryBean.setSecurityManager(securityManager); 

     shiroFilterFactoryBean.setLoginUrl(ShiroSecurityUrls.LOGIN_PAGE); 
     // shiroFilterFactoryBean.setSuccessUrl(ShiroSecurityUrls.LOGIN_SUCCESS_URL); 
     shiroFilterFactoryBean.setUnauthorizedUrl("/error"); 

     Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); 
     LogoutFilter logoutFilter = new LogoutFilter(); 
     logoutFilter.setRedirectUrl(ShiroSecurityUrls.LOGIN_PAGE); 
     filters.put(DefaultFilter.logout.name(), logoutFilter); 
     shiroFilterFactoryBean.setFilters(filters); 

     Map<String, String> filterChainDefinitionManager = new LinkedHashMap<String, String>(); 

     filterChainDefinitionManager.put("/static/**", DefaultFilter.anon.name()); 
     filterChainDefinitionManager.put("/node_modules/**", DefaultFilter.anon.name()); 
     filterChainDefinitionManager.put("/pages/**", DefaultFilter.anon.name()); 

     filterChainDefinitionManager.put(ShiroSecurityUrls.LOGIN_PAGE, DefaultFilter.anon.name()); 
     filterChainDefinitionManager.put(ShiroSecurityUrls.LOGOUT_URL, DefaultFilter.logout.name()); 
     filterChainDefinitionManager.put(ShiroSecurityUrls.REGISTER_PROCESS_URL, DefaultFilter.anon.name()); 
     filterChainDefinitionManager.put("/**", DefaultFilter.user.name()); 

     shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager); 

     return shiroFilterFactoryBean; 
    } 

    /** 
    * 
    * 
    * @throws UnknownHostException 
    */ 
    @Bean(name = "securityManager") 
    @DependsOn(value = { "ehCacheManager", "rememberMeManager", "sessionManager", "credentialsMatcher" }) 
    public DefaultWebSecurityManager securityManager(EhCacheManager ehCacheManager, RememberMeManager rememberMeManager, 
      SessionManager sessionManager, CredentialsMatcher credentialsMatcher) throws UnknownHostException { 
     DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); 
     // 1. Cache Support 
     securityManager.setCacheManager(ehCacheManager); 
     // 2. Session Support,inject the cacheManager from securitymanager 
     securityManager.setSessionManager(sessionManager); 
     // 3. Rememberme Support 
     securityManager.setRememberMeManager(rememberMeManager); 

     // 4. JDBC,LDAP Realm implements 
     Collection<Realm> authorizingRealms = Lists.newArrayList(shiroDatabaseRealm(credentialsMatcher), 
       shiroActiveDirectoryRealm(credentialsMatcher)); 
     securityManager.setRealms(authorizingRealms); // inject the cacheManager 
                 // from securitymanager 
     if (securityManager.getAuthenticator() instanceof ModularRealmAuthenticator) { 
      ModularRealmAuthenticator modularRealmAuthenticator = (ModularRealmAuthenticator) securityManager 
        .getAuthenticator(); 
      modularRealmAuthenticator.setAuthenticationStrategy(new FirstSuccessfulStrategy()); 
     } 

     return securityManager; 
    } 
} 

希望這段代碼能幫助你,謝謝。

+0

嗨,感謝您的解決方案,我已經將框架更改爲Spring Security,並且我知道實際問題是什麼,具有@Autowired的屬性將成功注入,如果使用public而不是private – Dean

0

通常,AutowiredAnnotationBeanPostProcessor在Spring加載FactoryBean類的階段設置此類屬性由@Autowired註釋。如果下面的工廠bean:

  1. ApplicationContextAwareProcessor
  2. ApplicationListenerDetector
  3. ConfigurationClassPostProcessor $ ImportAwareBeanPostProcessor
  4. PostProcessorRegistrationDelegate $ BeanPostProcessorChecker
  5. CommonAnnotationBeanPostProcessor會

是指你的配置bean時,你的bean將不會被自動裝配p未加載由於AutowiredAnnotationBeanPostProcessor而創建之後的roperties。

例如,屬性爲null,並且拋出NullPointerException

@Component 
public class BeanFactoryTest { 

@Autowired 
private IdGenProperties properties; 

@Bean 
public SnowflakeServer snowflakeServer() { 
    System.out.println(properties.getBaseUrl()); 
    return null; 
} 

@Bean(name = "conversionService") 
public ConversionServiceFactoryBean getConversionService() { 
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean(); 
    Set<Converter> converters = new HashSet<>(); 
    converters.add(new StringToDateConverter()); 
    bean.setConverters(converters); 
    return bean; 
} 

public static class StringToDateConverter implements Converter<String, Date> 
{ 
    public Date convert(String source) { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     try { 
      return sdf.parse(source); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    } 
}