I am developing an application that runs on Spring Boot configured via XML. For modularity, I didn't want to use #ImportResource as it requires me to go into the source code when in XML configuration I can just configure the XML files to change dependencies.
The problem is that I'm using Spring Boot to run my Spring MVC Controllers (#Controller) and for me to make use of the dependencies I configured in my XML files, I need to declare #ImportResource, which I don't want to use.
Is there any workaround to not use #ImportResource while still using XML config files to inject the dependencies in my Spring MVC Controllers?
If you are using spring boot and you are ok with mentioning the config location in application properties, you can do this in application.properties
config:
location: file:///config.xml
And the you can use this property in your #ImportResource
#ImportResource("${config.location}")
This way you can avoid changes to the source code, while still using xml configuration.
Related
I am working in spring boot project and I stuck in an issue. Following is the problem.
I have two log4j2 configuration file. Log4j2-1.yml and Log4j2-2.yml. Log4j2-1.yml consist the common configuration to avoid repetition duplicate configuration while Log4j2-2.yml consists application specific log config. I need to use these two log4j2 configuration in my application at same time. Please help me to configure these files in my application.\
I tried log4j2.configurationFile also but could not succeed.
Spring Boot has a custom logging configuration process that occurs just after the Log4j2 automatic configuration (its exact logic is in Log4j2LoggingSystem and is documented in Core Features). You can analyze the process by setting -Dlog4j2.debug=true.
To create a composite configuration you need to use two Spring Boot properties (cf. core properties):
logging.config=classpath:Log4j2-1.yml
logging.log4j2.config.override=classpath:Log4j2-2.yml
I want to create a jar file that I can add to a classpath and will basically "plug-in" to an existing spring boot application. I need it to be able to have annotations of Component, ConfigurationProperties, and all the fun things that spring boot gives you, but I want it "thin" and it will be a jar file used as part of a full spring boot web application.
I need the jar file to be externally configurable. Property files will be different for different deployments. So having a working #Configuration annotation is critical.
I look at spring-boot-starter-parent, and that has jetty, tomcat, hibernate stuff and is a huge jar file. I don't want that.
Is there a "thin" parent?
Is spring boot simply not what I want here? And I should just use a regular spring project and set my "Main" spring boot web app to do component scans to configure the jar file?
It sounds like you are trying to define your own Spring Boot Starter. That's the real power that Spring Boot gives you, the ability to include a dependency and have it auto-configure itself.
By packaging your jar the right way, Spring Boot will detect that there are configurations, components, and property files automatically. I've used this in the past for the case where I want all of my applications to log a specific way, or enforce a certain format for my REST endpoints.
The documentation gives a very thorough overview of the steps you'll need to take. But essentially, you are going to package your jar like any other (with your #Bean, #Component, #Service, and #Configuration classes in it), and provide a property file pointing to the configurations:
// Example spring.factories file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
Also check out the set of #ConditionalOn... annotations, they can really help with controlling what beans become active based on properties being defined, profiles being active, or dependencies being loaded.
I'd like to have a directory structure as so:
/Directory
- Application.jar
- application.properties
So that I can change properties without having to repackage and redeploy (and instead just restarting the jar). How can I accomplish this with spring annotations or configuration classes?
I'm not asking about making external resources available with my web application, I'm also looking to change the location from where spring loads the application.properties file.
You're mentioning jar, so you're using Spring Boot?
If so, external application.properties in the same directory (structure just like you described) will override application.properties packaged inside the jar file.
Then, if you have something like key=value in your application.properties, you can inject it in your code with #Value("${key}") String key.
Try it, it will just work :)
You may want to explore PropertyPlaceholderExplorer class in Spring. This class provides the facility to access the properties file external to your jar/war bundle. There is a short nice tutorial on this as well here.
If you start using Spring Boot (at some stage you for sure will). you get powerful configuration externalization features.
With Spring Boot your applications.properties are automatically loaded into Spring Boot context and you can use ${...} placeholders.
You can use even more modern feature #ConfigurationProperties to map you configuration to POJO. This POJO can even be validated by Java EE validation annotations (e.g. #NotNull)
Is there a way to enable yaml configuration (instead of properties file) in normal spring web mvc application without using spring boot and have the #Value annotation supported.
I searched a lot, everywhere it uses spring boot. I am not intrested to use spring boot, as its very heavy and have many dependancies configured for auto setup.
Any help would be appreciated......
What's the best method to initiate a new Spring Project ?
Initiating Spring project is a lot of pain with various xml and db configuration.
Is there an "official" repository with complete sample project, as MVC with db access and so ?
Spring Boot may also be the solution, but some point still not clear to me :
How to add external components (such as Quartz) without creating some xml config ? (No such xml in Boot apparently)
Is a Spring Boot builded app is production-proof ?
As writen in the comments http://start.spring.io/ is the best way to start Spring boot project(STS has integration for it).
If you want to use something that is not supported by Spring Boot you can init spring beans the same way you do it in xml, just use java configuration. See this for example: http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm
Also useing xml is still available. You can add #ImportResource on your Configuration class
#EnableAutoConfiguration
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})