0

I have a properties file shared on several apps. To access this properties into one app, I use this tag:

<context:property-placeholder location="classpath:br/com/empresa/configuracao/mule-apps.properties"/>

On several Mule components, like database attributes connections, I use the following expression to access the properties, p.e.: ${db.user}. It works!

But on Java Transformer, how I access the properties?

I tried the following instructions, but returned null:

System.getProperty("db.user");
message.getInboundProperty("db.user");
message.getOutboundProperty("db.user");
message.getInvocationProperty("db.user");

Is there a way to access properties into Mule Java Transformer?

Muka
  • 1,190
  • 1
  • 12
  • 27

2 Answers2

2

This question has been answered for components here: How to get property from context property placeholder tag inside custom java component The exact same logic applies to transformers.

Use property injection:

<custom-transformer class="org.myCompany.CustomTransformer">
    <property name="dbUser" value="${db.user}" />
</custom-transformer>

Don't forget to add setDbUser on your custom component!

Community
  • 1
  • 1
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • I want to use this property to make configurable host (wsdl url) in the Service class (Java Web Service). It 's initialized statically. I did what you suggested, but is returning null. Is it works in a static context? – Muka Feb 23 '16 at 19:39
  • In XML: ` ` – Muka Feb 23 '16 at 19:56
  • In Java (piece): `public class MyServices extends Service { static String wshost; static { System.out.println(wshost); // print NULL } public static void setWshost(String s) { wshost = s; } }` – Muka Feb 23 '16 at 20:00
  • There are different options for injecting static fields: http://stackoverflow.com/a/11324464/387927 – David Dossot Feb 23 '16 at 21:31
  • Understand. But I think that my problem is that static class is load on ESB runtime before injecting static fields, because when I start the app, first static class is load and after `PropertySourcesPlaceholderConfigurer`. I need that properties to be loaded first. – Muka Feb 24 '16 at 11:10
  • Or can you just not use static? Mule/Spring provides you with proper application lifecycle and dependency management. Statics are messing with both aspects. – David Dossot Feb 24 '16 at 16:41
  • I'm not sure if I can leave static! I am using the web services classes generated by CXF component. But I found an alternative, programmatically, though not exactly desirable!: http://stackoverflow.com/questions/34923956/mule-property-placeholder-access-in-java-properties. Thanks for your attention! – Muka Feb 25 '16 at 17:40
2

You could use the old way of retrieving a property:

@Value("${db.user")
private String dbUser;
Alex Fernandez
  • 1,892
  • 14
  • 17