【问题标题】:Spring: Why does Bean resolve as null during injecting?Spring:为什么 Bean 在注入期间解析为 null?
【发布时间】:2017-08-21 14:25:01
【问题描述】:

同学们,我有一颗豆子:

    @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 [主要] 调试 o.s.b.f.annotation.InjectionMetadata - 处理注入的元素 豆'basicAuthAuthorizationInterceptor':AutowiredFieldElement 私有 java.util.Map com.comp.spum.service.interceptors.BasicAuthAuthorizationInterceptor.authMap 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥 [environmentProperties] 中的“accepted.username.pass1” 2017-08-22 10:25:00,574 [主] TRACE o.s.c.e.PropertySourcesPropertyResolver - 在 [systemProperties] 中搜索键 'accepted.username.pass1' 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥 [systemEnvironment] 中的“accepted.username.pass1” 2017-08-22 10:25:00,574 [主] TRACE o.s.c.e.PropertySourcesPropertyResolver - 在 [class path 资源中搜索 key 'accepted.username.pass1' [spum-dev.properties]] 2017-08-22 10:25:00,574 [主要] 调试 o.s.c.e.PropertySourcesPropertyResolver - 找到密钥 [类路径资源中的“accepted.username.pass1” [spum-dev.properties]] 类型为 [String] 2017-08-22 10:25:00,574 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - 找到密钥 [environmentProperties] 中的“accepted.username.pass1”,类型为 [字符串] 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.util.PropertyPlaceholderHelper - 已解决的占位符 'accepted.username.pass1' 2017-08-22 10:25:00,595 [主要] 调试 o.s.b.f.s.DefaultListableBeanFactory - 完成创建实例 bean 'basicAuthAuthorizationInterceptor'

【问题讨论】:

  • 您需要发布您的SpumConfig.class。我们需要查看您的 Spring 配置。
  • @William,我添加了它,但目前它是空的。
  • 您确定您正在构建的存档包含属性文件,对吧?
  • 也许试试这个页面的答案之一:stackoverflow.com/questions/15937592/… 将此添加到您的配置中:@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }
  • 构建存档时,属性放在哪里?它们在类文件夹中吗?

标签: java spring cxf


【解决方案1】:

很可能 jar 没有选择正确的属性文件路径。请先验证属性文件的路径。

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

【讨论】:

  • Nilsaw,我确信属性正在恢复,因为日志中有关于它的信息2017-08-24 09:45:04,848 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [class path resource [spectrum-dev.properties]] with type [String] 2017-08-24 09:45:04,848 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [environmentProperties] with type [String] 2017-08-24 09:45:04,849 [main] TRACE o.s.util.PropertyPlaceholderHelper - Resolved placeholder 'accepted.username.pass1'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
  • 2018-03-08
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
相关资源
最近更新 更多