Spring:为什么在注入期间Bean会解析为null?

时间:2017-08-21 14:25:02

标签: java spring cxf

同意,我有一个豆子:

    @Component
    public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {

        private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptor.class);

        @Value("#{${accepted.username.pass1}}")
        private Map<String,String> authMap;

        @Override
        public void handleMessage(Message message) throws Fault {
            AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);

            if (policy == null) {
                sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
                return;
            }

            String username = policy.getUserName();
            String password = policy.getPassword();

            // CHECK USERNAME AND PASSWORD
            if (!checkLogin(username,password)) {

                 sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
            }
        }

        public boolean checkLogin(String username, String password) {
            MapUtils.debugPrint(System.out, "Map: " , authMap);

            if (authMap.containsKey(username.trim()) && password.trim().equals(authMap.get(username).trim())) {
                return true;
            }
            return false;
        }
   //some other methods
    }

当我运行测试用例时:

@RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = SpumConfig.class)
public class BasicAuthAuthorizationInterceptorTest {

    private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptorTest.class);

    @Autowired
    BasicAuthAuthorizationInterceptor basicAuthAuthorizationInterceptor;
    @Test
    public void handleMessage() throws Exception {
       log.info(String.valueOf(basicAuthAuthorizationInterceptor.checkLogin("abc", "321")));
    }}

我收到trueMapUtils.debugPrint方法打印所有键和值:

  

地图:= {       abc = 325 java.lang.String       cda = 322 java.lang.String       sss = Bas3 java.lang.String} java.util.Collections $ UnmodifiableMap

但是当我编译一个jar文件时,运行应用程序而不是调用Web服务我收到

Map:  = null

为什么会发生这种情况以及如何正确地将地图注入BasicAuthAuthorizationInterceptor

更新

    @Configuration
    @ComponentScan(basePackages = {"com.comp.spum"})
    @PropertySource("classpath:spum-${env}.properties")
    public class spumConfig {
      private static final Logger log = LoggerFactory.getLogger(spumConfig.class);

        @Autowired
        Environment environment;
        @Autowired
        spumCommons spumCommons;


  @Bean
    public String appProps() {
        log.info("test.db.url = " + environment.getProperty("test.db.url"));
        return null;
    }

    @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
    { return new PropertySourcesPlaceholderConfigurer();}

    }

更新2

根据日志属性进行解析并创建basicAuthAuthorizationInterceptor

  

2017-08-22 10:25:00,573 [main] DEBUG   o.s.b.f.annotation.InjectionMetadata - 处理注入的元素   bean&#39; basicAuthAuthorizationInterceptor&#39;:AutowiredFieldElement for   private java.util.Map   com.comp.spum.service.interceptors.BasicAuthAuthorizationInterceptor.authMap   2017-08-22 10:25:00,574 [主要] TRACE   o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥   &#39; accepted.username.pass1&#39;在[environmentProperties] 2017-08-22   10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver -   正在搜索密钥&quot; accepted.username.pass1&#39;在[systemProperties]中   2017-08-22 10:25:00,574 [主要] TRACE   o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥   &#39; accepted.username.pass1&#39;在[systemEnvironment] 2017-08-22   10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver -   正在搜索密钥&quot; accepted.username.pass1&#39;在[类路径资源   [spum-dev.properties]] 2017-08-22 10:25:00,574 [主要] DEBUG   o.s.c.e.PropertySourcesPropertyResolver - 找到密钥   &#39; accepted.username.pass1&#39;在[类路径资源   [spum-dev.properties]]类型[String] 2017-08-22 10:25:00,574   [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - 找到密钥   &#39; accepted.username.pass1&#39;在[environmentProperties]中的类型   [String] 2017-08-22 10:25:00,574 [主要] TRACE   o.s.util.PropertyPlaceholderHelper - 已解决的占位符   &#39; accepted.username.pass1&#39; 2017-08-22 10:25:00,595 [主要] DEBUG   o.s.b.f.s.DefaultListableBeanFactory - 完成创建的实例   bean&#39; basicAuthAuthorizationInterceptor&#39;

1 个答案:

答案 0 :(得分:0)

很可能jar没有开始使用属性文件的正确路径。请先验证属性文件的路径。

然后使用弹簧'PropertySourcesPlaceholderConfigurer添加正确的路径。您可以在部署应用程序时验证应用程序是否在服务器日志中选取属性文件。