【问题标题】:@Bean returning null@Bean 返回 null
【发布时间】:2020-02-10 03:05:32
【问题描述】:

在配置类中我定义了几个@Beans。问题是某些 bean 在调用时会在其他类中重新调整 null。我想了解为什么会这样。

SoapConfig.class

@Configuration
@ComponentScan(basePackages = {"mk.test.wsdl","mk.test.Porting"})
public Jaxb2Marshaller marshaller(){
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("mk.softnet.wsdl");
    System.out.println("out:" + marshaller);
    return marshaller;
}

@Bean
public SaajSoapMessageFactory messageFactory() {
    SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
    messageFactory.setSoapVersion(SoapVersion.SOAP_12);
    return messageFactory;
}
  @Bean
   public KeyStoreFactoryBean keyStoreFactoryBean(){
       KeyStoreFactoryBean keyStoreFactoryBean = new KeyStoreFactoryBean();
       keyStoreFactoryBean.setPassword("test");
       keyStoreFactoryBean.setLocation(new 
       ClassPathResource("test.jks"));

       return  keyStoreFactoryBean;
   }

SoapClinet.class

private Jaxb2Marshaller marshaller;

在方法中:

System.out.println(marshaller) //我得到一些值,例如:marshalar: org.springframework.oxm.jaxb.Jaxb2Marshaller@376c7d7d(我不知道这是什么意思)

但是如果我 System.out..."keyStoreFactoryBean""messageFactory" 我总是得到 null,我需要在 SoapClient.class 中定义它

只有表明某事的信息是这样的: [org.springframework.ws.soap.security.support.KeyStoreFactoryBean] 类型的 Bean 'keyStoreFactoryBean' 不符合被所有 BeanPostProcessor 处理的条件(例如:不符合自动代理条件)

但从我的阅读来看,这不是错误。

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    你在类上有@Configuration 注释而不是上面的方法吗?我使用下面的代码运行它并创建了 bean。

    @Configuration
    public class SoapConfig {
    
        @Bean
        public Jaxb2Marshaller marshaller(){
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setContextPath("mk.softnet.wsdl");
            System.out.println("out:" + marshaller);
            return marshaller;
        }
    
        @Bean
        public SaajSoapMessageFactory messageFactory() {
            SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
            messageFactory.setSoapVersion(SoapVersion.SOAP_12);
            return messageFactory;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2016-09-17
      • 2012-04-12
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 2011-07-02
      • 2021-10-28
      相关资源
      最近更新 更多