http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client
In the above mentioned example we have hard coded TestUser(username) and TestPassword(password) in the message handler class.I want to externalize these values.
After some research I was not able to implement #Autowired and #value .Please help me out with this
If your .properties file is saved somewhere on the local hard-drive and it is one of the KPL (Key Project Locations), you can simply do this:
Properties propObj = new Properties ();
FileInputStream fis = new FileInputStream("relative path to your file");
propObj.load(fis);
Once you do this, simply use getProperty() or setProperty() to access/mutate desired properties. There is also another way to use the loadFromXML() method if your choice of file is an xml file. I hope this solves your problem because it sounds like you simply having trouble to load a file regardless of being in a controller class. If I am right, then you will find endless examples online that tells you to do merely what I wrote above :)
When using things like #Autowire remember that you have to annotate before the beginning of the target class with #ContextConfiguration("location_of_file.xml"). Getting the correct relative path may be a bit tricky when specifying an applicationContext file. See here and follow other links to find out more about this.
BTW one thing you have to remember that application properties are somewhat internal to an application and for this reason, they are set in a .properties file outside the source files so that it can be run without changing source code (correct me somebody if I am talking bollocks!).
If you are using build automation tools such as Maven or Ivy, there is a way to make sure that your application properties are set using the a) POM and b) applicationContext.xml files. In this way, the application is even more decoupled from breaking down in case a relative path doesn't work out to be the some on a target computer. If you want this, you have to be careful about setting the filters in POM. I haven't personally done this by hand, but we were working on a project a few months ago when a colleague of mine set the filer in front of me. I remember this because everything was failing due to incorrect filter setting. Sorry, but you have to find it out yourself using uncle Google!
Related
I want to write to an application.yml file as well as read from it respectively, after value has been written in a spring boot application during runtime. It can be application.properties file as well. The property's key can be written dynamically or it can be entered manually by the developer(Doesn't matter).
The key-value pair must me accessible after it has been written to the yaml/properties file.
I don't have much information about the above mentioned concept. So, haven't implemented it yet. Hence, there is no source code for it. I tried to search on the web but didn't get expected information, so posting it here.
Since I don't know the way it is done, if someone knows whether it is the same way or different ways to write to both(properties/yaml) files, preferred way is using the yaml file.
Any helpful links or tutorials are welcome.
Thank You
I used #ConfigurationProperties in my project to solve this problem.
My goal is to have a simple way of running Camel with a few routes in a dynamic fashion. Ideally, I'd like to run something like this
java -jar camel.jar routes.xml
which allows adjusting the routes in routes.xml without changing the Jar. But if the routes.xml file is read from a relative filesystem location, that would work too.
I've generated an application using camel-archetype-spring-boot using mvn archetype:generate. I put this line in resources/application.properties:
camel.springboot.xmlRoutes = classpath:routes.xml
which loads the routes defined in resources/routes.xml. So I can configure the routes but must rebuild the Jar everytime I want to adjust something. Now how can I load routes.xml from outside the Jar?
Maybe the path I've chosen is not ideal to get a Camel instance I can quickly reconfigure. If there is a better way, I'd like to hear that. I'm a bit lost with all the options.
I found Externalized Configuration in the Spring manual, but that only explains how to change properties. I also found a question that talks about how to exclude the configuration XML. Unfortunately it doesn't say how the external XML is loaded instead.
Thanks to #ClausIbsen: The file is read from outside the Jar with:
camel.springboot.xmlRoutes = file:routes.xml
Simple, once you know :-)
So I have a few .drlt files (normal txt files that serves me as templates for rule engine) and
I need to read them and put in to some static string variable in bootstrap of application.
I just cannot figure out where to put it in grails application and how to read them.
I've tried put them to src/java and read them from src/grails, domain, service etc. None of that worked. The only thing that worked is to put it into grails-app/conf and read them like this:
def classLoader = Thread.currentThread().contextClassLoader
def fileReader = classLoader.getResourceAsStream('my-file.drlt').newReader()
But I don't want to put my template files in config directory.
I've read many posts and 'solutions' for this problem but non of them are good solutions. Is it really possible that this is unable to do nice in such a stable framework as Grails?
Thanks for any help,
Ivan
Since Grails is a well structured Spring application you can take advantage of Resource and ResourceLoader to load your text file. The grailsResourceLocator makes using these quite simple (be sure to inject it into your bean). Using these classes you can locate your file within the src/java or src/groovy directories and treat it as any other class resource.
So for example, if your file is in src/java/com/example/textfile.txt then your can access it as a File using grailsResourceLocator.findResourceForURI('classpath:/com/example/textfile.txt').getFile()
A detailed example can be found within this blog post and the API documentation for Resource may help as well.
This is the correct way to handle resources within your Grails application.
I need system similar to ResourceBundles except that I will not use it for localization. Basically, I need to store several versions of settings. One version of that settings is String-to-String map, that can be represented by Properties file. These settings versions must be easily persisted to file system inside application .jar (alike PropertyResourceBundle).
The idea is to have different versions of application settings (settings profiles), represented by key-value pairs of type string, that can be chosen from at application start up based of user decision. Again these are not language versions so ResourceBundle (according to its javadoc) is not the right way to implement it.
Any easy way how to do that without implementing the whole think myself? Please do not suggest third-party it should only use Java SE classes.
Edit: I forgot to mention one important detail. It would be hard for me to get stream like this: Thread.currentThread().getContextClassLoader().getResourceAsStream("/your/resource/here");. That is because the project that would contain properties file is compiled by Ant and used as a dynamically loaded library in different GUI projects that actually run. I might have all properties files in fixed project folder but since Thread.currentThread().getContextClassLoader() returns context of GUI project and I do not know where in that project was the .jar with property file placed by Ant I do not know what to use as "/your/resource/here".
I might have misunderstood the question however seems to me you can easily do something like this:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/your/resource/here");
Properties prop = new Properties();
prop.load(inputStream);
This will be safe in a Java EE environment as well and you can call you anytime you need if you want settings to change.
Update: as long as the resource is on the classpath you should be able to find it without knowing the full path of the resource as well.
I'm trying to bind in a third party app to our project but have discovered that the unix paths of their property files have been hard-coded into several classes. It is probably possible to make minimal changes to their setup, so my question is: what are the possible (and quickest) ways to achieve this? The third party app uses neither ant nor spring, but just a build script that compiles the code into a .jar that is called from the command line.
You could read the properties files as resources. Then you only have to put the directories containing these files into your classpath.
You haven't actually said what you want to achieve. Would your application determine some paths and pass them to third party app via an API? Would something in your environment (for example a command line argument) specify the location of the files?
I would first refactor their code so that I know for certain that any hard coded strings are held in one defined place. So if for example they have
readProperties("/usr/thing/propertyFileXxx.txt");
or
static final String PROPERTY_XXX = "/usr/thing/propertyFileXxx.txt";
readProperties(PROPERTY_XXX);
I would first consolidate to a single accessor for the properties
readProperties(PROPERTY_XXX_ENUM);
So now we have a well-defined single piece of code that determines where to obtain the properties of each type and a specific list of the types of properties.
Then we need some controllable way to define the set of property files to be used at run-time. I have used the idea suggested by #tangens of loading the properties as resourcees from a specific directory added to the classpath.