Lil Villa
Lil Villa
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 5/30/2025 in #java-help
Proper OOP patterns practices with factory
Hey everyone! 👋 I'm working on a feature to decorate API responses with currency formats based on user config (from S3). I initially handled validation and decoration with simple singletons, since I'm actually writing code in Scala, I'm trying to follow fp practices and use singleton to create something where to place a function for logic. My idea to not overcomplicate things and just use plain functions, since this is a very simple feat imo. A new team lead has recently joined my team. He asked me to refactor my pr using Factory class for creating a "currency plugin" instance. The proposed logic for this Factory is: 1. The Factory would be invoked/relevant only if the search query requests currency formats. (The lead's note said: "If search query is requesting currency formats -> None" which I interpret as the factory's specific logic below only applies if currency is requested, otherwise this specific factory/plugin isn't engaged for its primary purpose). 2. If currency format is requested, but the necessary user currency configuration is missing from S3 -> The Factory itself should throw a hard error. This error would occur during the initial request processing and validation phase. 3. If currency formats are requested and the configuration exists -> The Factory creates and returns the plugin instance. My main question is about point 2: Is it a good practice for a Factory class to be responsible for input validation logic? I'm confused, because I think factory should only be responsible for creating object instances which then in turn are supposed to do validation, transformations and so on... And I also think that factories should not be used when there's only two classes in the domain. What are your thoughts on a Factory performing this type of validation leading to a hard stop? Is this a common or advisable pattern, or should such critical validation remain separate from the Factory's creation logic? Thanks for any insights! 🙏
15 replies
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 12/27/2023 in #java-help
Change order of log4j2 configuration files properties precedence
I have gradle project which has log4j2.properties file in its resources directory. I've created integration-tests directory with another resources directory and log4j2.xml file. When I run my integrations tests, at first log4j2.xml configuration is used, but once test make a call to main project files xml configuration is being overwritten by log4j2.properties file. How could I make xml file properties take precedence over main project properties file when running tests?
4 replies
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 5/29/2023 in #java-help
Can't resolve transitive dependencies by myself
I have a flink project. Which has transitive guava dependencies. Simplified it looks like this:
+--- foo
| +--- bar
| | +--- guava:1.5
| +--- biz
| | +--- guava:1.3
+--- foo
| +--- bar
| | +--- guava:1.5
| +--- biz
| | +--- guava:1.3
If I set 1.5, it ruins biz and if I set 1.3 it ruins bar (these versions do not represent my real project and i put them here just as an example) How to force gradle to set exact dependencies for dependencies dependencies?
9 replies
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 5/4/2023 in #java-help
Flink ShutdownHookManager throws Trying to access closed classloader when destroying itself
No description
8 replies
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 11/29/2022 in #java-help
Force Spring Yaml configuration environment parameter to be treated as yaml instead of String
Intro: I have a Spring Boot application with its configuration set up with application.yml file like so: - Scala class:
@ConfigurationProperties(prefix = "foo.bar")
@ConstructorBinding
case class MyConfig(
biz: String,
testYamlMaps: java.util.Map[String, String]
)
@ConfigurationProperties(prefix = "foo.bar")
@ConstructorBinding
case class MyConfig(
biz: String,
testYamlMaps: java.util.Map[String, String]
)
- application.yml:
foo:
bar:
biz: "hello"
test-yaml-maps: ${YAML_TEST_MAP} # option 1
# test-yaml-maps: {redelivery: true, secure: false} option 2
foo:
bar:
biz: "hello"
test-yaml-maps: ${YAML_TEST_MAP} # option 1
# test-yaml-maps: {redelivery: true, secure: false} option 2
- YAML_TEST_MAP environment variable:
$ echo $YAML_TEST_MAP
{redelivery: true, secure: false}
$ echo $YAML_TEST_MAP
{redelivery: true, secure: false}
Problem: Option 2 works but with option 1 it looks like Spring is parsing application.yml first and only then substitutes environment values this way treating YAML_TEST_MAP as a String making it throw this error:
No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
Question: Is there a way to force Spring replace environment variables first and only then parse? Is there a way to tell Spring to treat value as a rest of yaml document?
7 replies
JCHJava Community | Help. Code. Learn.
Created by Lil Villa on 10/11/2022 in #java-help
how to add Log4J json template layout
https://logging.apache.org/log4j/2.x/manual/json-template-layout.html says:
Adding log4j-layout-template-json artifact to your list of dependencies is enough to enable access to JsonTemplateLayout in your Log4j configuration:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<version>2.19.0</version>
</dependency>
Where should I be looking for my list of dependencies? Should it be my gradle file? Should I be looking at log4j.xml?
6 replies