I’m new to spring-boot and attempting to populate a property in a spring-boot XML with a function return value from a Java class but the class doesn’t seem to be visible to the spring-boot loader. I’m probably just missing an annotation or something but any input would be great.
Here is my Java class and method:
public class AppMqttConfig {
@Bean
public SSLSocketFactory getSocketFactory()
throws Exception {
ResourceLoader resourceLoader = new DefaultResourceLoader();
String caCrtFile = resourceLoader.getResource("classpath:/non-prod/AmazonRootCA1.pem").getFile().getAbsolutePath();
String crtFile = resourceLoader.getResource("classpath:/non-prod/certificate.pem.crt").getFile().getAbsolutePath();
String keyFile = resourceLoader.getResource("classpath:/non-prod/private.pem.key").getFile().getAbsolutePath();
Security.addProvider(new BouncyCastleProvider());
}
}
And here is where I’m referencing the return value of getSocketFactory:
<bean id="pahoMqttClientFactory"
class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
<property name="connectionOptions">
<bean class="org.eclipse.paho.client.mqttv3.MqttConnectOptions">
<property name="serverURIs">
<list>
<value>${mqtt.broker.url}</value>
</list>
</property>
<property name="connectionTimeout" value="${mqtt.client.connectionTimeout}" />
<property name="keepAliveInterval" value="${mqtt.client.keepAliveInterval}" />
<property name="cleanSession" value="${mqtt.session.clean}" />
<property name="socketFactory" value="#{AppMqttConfig.getSocketFactory()}" />
</bean>
</property>
</bean>
This is the error I’m receiving:
Property or field AppMqttConfig cannot be found on object of type
org.springframework.beans.factory.config.BeanExpressionContext –
maybe not public or not valid?
You haven’t provided enough code, we don’t see where you reference the property mqtt.broker.url. Do you have annotations on AppMqttConfig ?
Thanks for your comment Chasca! The property mqtt.broker.url is exposed as configuration in another class and works fine. My problem is on the line in the bean, <property name=”socketFactory” value=”#{AppMqttConfig.getSocketFactory()}” />. There are no annotions on AppMqttConfig although I’ve tried several to no avail.
Ok it is not like that that you can reference a bean in a property like that in XML. I advice you to switch to annotation based configuration. Thus you could create your SocketFactory and reference it directly at compile time. And if you really want to stick to that kind of configuration, then AppMqttConfig must be defined as a @Configuration, and its name would probably be appMqttConfig, thus you could try #{appMqttConfig.getSocketFactory()}
But make sure Spring creates an instance of AppMqttConfig, the annotation might not be sufficient if you don’t scan its package
Thanks Chasca! How do I go about ensuring that Spring creates an instance of AppMqttConfig?
Show 3 more comments