@Bean返回空值

时间:2019-10-13 04:51:24

标签: spring spring-boot

在配置类中,我定义了几个@Bean。问题在于,某些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的处理要求(例如:不符合自动代理的条件)

但是根据我的阅读,这不是错误。

1 个答案:

答案 0 :(得分: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;
    }
}