Java Classes Structure - java

I am working to a project made in Maven with Spring and i want to start the structure fine from the beginning.
I need help from you to know if is ok what i did until now. So, in have the structure:
src/main/java/com/fabbydesign/controller
src/main/java/com/fabbydesign/model
src/main/java/com/fabbydesign/util
and i have another file in:
src/main/webapp/WEB-INF/appConfigs.xml
in this file i store the configurations of the project, like database login, etc
In the directory utils, i will store utils class for the project. for example, i will make a class that will read the appConfigs.xml to get configurations stored there
Is ok what i did until now?
In the model directory, i have to store the beans for the database, or i can add other types there? Like, the bean for the xml with configs?
Thanks!

Related

How to set properties value in multi-module project?

I have a multi-module Spring Boot Gradle project. I have properties in each module yml file that point to database: user, pass, url.
It's working solution, but it's difficult to change project database. Every time I want switch database user or url, I must change 10+ yml files.
How to avoid this?
You could bind the properties in a class (see here: https://www.baeldung.com/configuration-properties-in-spring-boot) and inject the class where needed.

How to get Hibernate to scan for Entities in a jar file included in the jar

I have a vexing problem.
I'm upgrading stuff to j11 and the latest of everything but I can't change too much of the app itself. The created jar file contains within itself a jar file that contains mapped entities, like this:
jar
BOOT-INF
classes
application classes
lib
entity lib
META-INF
persistence.xml
In my persistence.xml I refer to the entity lib through the <jar-file> directive like so:
<jar-file>BOOT-INF/lib/EntityLib-1.0.jar</jar-file>
But the scanner can't find it. (I get java.nio.file.NoSuchFileException: BOOT-INF/lib/EntityLib-1.0.jar)
I've read parts of JSR-338, JPA 2.1 and looked at the examples but I haven't gotten any further.
I'm using Hibernate 5.3.3.Final
This is a spring boot application but the original developers used basic JPA instead of the spring func and I'm not allowed to rewrite the app that much (nor do I want to, to be honest).
EDIT: the app is created through the normal spring-boot-maven-plugin.

SpringBoot library project does not see main app properties

I have a setup like this:
Main SpringBoot project with application-default.properties which on our deployment server are partially overwritten by a deployment specific properties.
Shared SpringBoot library project which has its own properties.
And when I run my main project with the library project attached (via gradle sourceControl gitRepository) I can see that the properties in the library project are empty.
How can I make the library project use the properties passed down from the main application ?
If you want to merge properties, please consider this official page.
Option 1 - default properties in library
As I found previously (probably, it is fixed), if you have jar1 and jar2 (sorted alphabetically) and both of them have application.properties file, only first will be used. They aren't merged. So please be carefully there.
However you can use #PropertySource in your library, e.g. put default properties there into the custom file name (for example - defaults-for-jar2.properties or something like this, to avoid automatic loading by Spring).
In this case:
Property load logic outside of your library will be the same with current.
Your library will load file from #PropertySource and next they will be overridden (if you have this) by your application.
Option 2 - configuration properties
If you use Kotlin and Spring, you can use ConfigurationProperties. And you can define the default values there. Moreover, IntelliJ Idea will highlight the default and possible values (according to the type, because you can use not only String, but any custom enum class, Duration class, etc.).
Just from that link:
#ConstructorBinding
#ConfigurationProperties("blog")
data class BlogProperties(var title: String, val banner: Banner) {
data class Banner(val title: String? = null, val content: String)
}
#SpringBootApplication
#EnableConfigurationProperties(BlogProperties::class)
class BlogApplication {
// ...
}
Please note:
You should mention your settings data class in the library configuration.
You should configure kapt properly to have Intelli Sence in IDEs.

Java MVC project in Spring Tool Suite additional files

I'm learning to make Java MVC project using Spring Tool Suite tool.
The path to make new project is:
File->New->SpringLegacyProject->Spring MVC Project.
My question is: which directory I have to use to add additional not-Spring files and where and what do I have to type for Spring files to see them?
For example:
css files - where to put and how to make jsp views see them, will 'link rel="" 'tag be enough?
properties files used to specify database connection or to specify messages for ReloadableResourceBundleMessageSource. In this case, do I have to create bean for this class in root-context.xml?
Thanks.
You should probably use Spring Boot (i.e. use File->New->Spring Starter Project and select Web as a starter. Place your web resources under src/main/resources/static folder. They are picked up automatically from that folder.
You should try an example project: File -> New -> Import Spring Getting Started Content and then pick "Serving Web Content" from the list.
Try some DB getting started content example to get the answer for the second part of your question.

Play Framework - How to keep the configurations of my module inside it?

I am using Play Framework 2.2.x/Java.
I want to create a module to sperate some of the logics from my main application and also I want to use the application.conf inside the module for its configurations instead of using the main application's config file!
But using the following snippet in the module, only reads values from the main application config file:
Play.application().configuration().getString("myVar");
Is there any other way to get the values from the application.conf file inside my module?
Play uses the typesafe-config library for reading configuration. This is actually a Java library, even though Typesafe is a Scala company.
The documentation for typesafe-config says "The idea is that libraries and frameworks should ship with a reference.conf in their jar."
So your module's config should be stored in a file called reference.conf - the format is exactly the same, it's just the name that is different.
The problem occurs because there is a conflict between the two config files because they are named the same, so it probably goes by classpath order or something. Don't use two application.conf files - this problem has bitten me in the past!
Save your config into ie. /conf/my-module.conf of the main app and then include it at the end of application.conf like:
include "my-module.conf"

Categories