logo
Tags down

shadow

How to inject appllication,properties value based on field availability?


By : gtomoe
Date : September 15 2020, 01:00 PM
will be helpful for those in need I think the real question is what do you do with that ApplicationPropertyHandler?
One possible solution is to not use values at all, it wasn't presented among answers so I add it here.
code :

@SpringBootAppplication
@EnableConfigurationProperties(MyConfigurationProperties.class)
public class Main {
   public static void main(String [] args){...}
}

@ConfigurationProperties 
public class MyConfigurationProperties {


   public static class Http {
       private int port = 8080; // getters/setters, 0 is a default
   }

   public static class Https {
       private int port = 0; // getters/setters, 0 is a default
   }

   ....
}


Share : facebook icon twitter icon

Using @Qualifier based on @Inject field CDI


By : Georff
Date : March 29 2020, 07:55 AM
this one helps. I'm having trouble with CDI conditional injection to use a kind of Strategy in EJB's injections. , Try this...
code :
public class someManagedBean {

    @Inject 
    @MyOwnQualifier
    private BeanInterface myEJB;
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyOwnQualifier {
    SomeCondition condition();
}

public class BeanInterfaceFactory {

    @Inject
    SomeBean someBean

    @Produces
    @MyOwnQualifier
    public BeanInterface getBeanInterfaceEJBImpl() {
        if(someBean.getCondition()) {         
            return new BeanInterfaceEJBImpl();
        } else {
           ....
        }
    }
}

public enum RoleCondition {
    ADMIN("ADMIN User");
}

Is it possible to inject different repository classes based on application.properties?


By : Wai Yan Linn Htet
Date : March 29 2020, 07:55 AM
help you fix your problem I assume both repositories have a common interface where you want to load the actual bean. I'd try something like this: (I didn't test it)
code :
@Autowired
MongoRepository mongoRepository;
@Autowired
CrudRepository crudRepository;

@Value("isMongo")
boolean isMongo;

private Repository actualRepository;

 @PostConstruct
  public void init() {
    actualRepository = isMongo ? mongoRepository : crudRepository;
  }
@Bean
public Repository actualRepository(@Value("isMongo") boolean isMongo, MongoRepository mongoRepository, CrudRepository crudRepository) {
  isMongo ? mongoRepository : crudRepository;
}

Spring - Inject Dependency based on properties


By : Mayank goyal
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If you really only need a single bean, then create a conditional configuration
code :
@Configuration
@ConditionalOnProperty(name = "fileStore.class", havingValue="FileStoreA")
public class FileStoreAConfiguration {
    @Bean
    public IFileStore fileStore() {
        return new FileStoreA(...);
    }
}

@Configuration
@ConditionalOnProperty(name = "fileStore.class", havingValue="FileStoreB")
public class FileStoreBConfiguration {
    @Bean
    public IFileStore fileStore() {
        return new FileStoreB(...);
    }
}

Spring, inject properties in a Bean instance based one of that Bean's field value, is it possible?


By : Antonio
Date : March 29 2020, 07:55 AM
should help you out Are you using just spring or also spring-boot?
What about injecting org.springframework.core.env.Environment to your pojo and configuring it with it.
code :
public class ServiceConfig {

    private String url;
    private String endpoint;
    private String serviceName;

    public ServiceConfig(String serviceName, Environment env) {
        // TODO assert on serviceName not empty 
        this.serviceName = serviceName;
        this.url = env.getProperty(serviceName.concat(".url");
        this.endpoint = env.getProperty(serviceName.concat(".endpoint"); 
    }
}
public class ServiceConfig {

    private String url;
    private String endpoint;

    // getters setters
}
@Configuration
public class ServicesConfiguration {

    @Bean
    @ConfigurationProperties("service1")
    ServiceConfig service1(){
        return new ServiceConfig();
    }

    @Bean
    @ConfigurationProperties("service2")
    ServiceConfig service2(){
        return new ServiceConfig();
    }
}

Getters/properties availability based on object's state (based on parsing state, for example)


By : conduitOf
Date : March 29 2020, 07:55 AM
it should still fix some issue One option: have the parse() method return the parse result (instead of storing it in the parser's state). That result could be an abstract class with two concrete subclasses: one for success (with getter(s) for the relevant result info) and one for failure (with getter(s) for the failure reason).
Related Posts Related Posts :
  • How to change the caret color in a JTable
  • What's best practice to store a value for spring boot application?
  • Best way to handle this REST API endpoint error?
  • Reference behaviour
  • Trying to multiply within the public static void. Any help is appreciated
  • Java - Get substring from 2nd last occurrence of a character in string
  • Calling method from an array of type Object
  • Java NumberFormat rounding mode, why half up format 1.015 to 1.01 not to 1.02
  • Getting last 2 digits after multiplication
  • Best way to return data to MainActivity from AsyncTask
  • How do I build this words to one sentence after looping?
  • String class value variable used in equals() method: how and when is it defined?
  • javac error: Scanner: FileNotFoundException
  • Is there really no way to forcefully kill a Thread in Java?
  • ERROR: Gradle DSL method not found: 'imlementation()'
  • How to timeout a Future in Java without using Future.get() as it's a blocking operation?
  • Why does HashMap override the existing object when I am returing different hash codes for the same object?
  • Java Hashmap how to handle key collision for grid of words
  • How to add a `Period` to `java.util.Date`?
  • Unit test throwing default admin username not available error
  • Using application classes as utilities in unit tests
  • Retrieving filename from Jfilechooser into JList
  • JSON Response Value says Null Rest Assured
  • How to load changed environment variable in spring boot application running in PCF without restarting application?
  • Array of Instants from Strings
  • Cannot add jdk11 and jdk13 in SDK in IntelliJ - java
  • convert ISO8601 date time to another ISO8601 format based on State name in Java?
  • KafkaProducer not producing message > 1 MB to topic
  • Is this statement true that Java 8 provides functional style but is not functional programming?
  • In Java how to easily get the value of a nested key from a JSON String / Object
  • Lombok builder pattern issue
  • Can a final class extend other classes in java?
  • How to find replacement packages when migrating from Java 7 to 12?
  • Iterating between text and integer in android using java to show a professional splash screen
  • Loops with no body,i got the code from a book but i dont understand the math in the code
  • Hello, can I ask some questions about java(Abstract, getter,setter)?
  • Maven jUnit5 org.junit.platform.runner, org.junit.platform.suite.api does not exist
  • How to fix bottom navigation bar not showing correctly
  • Populating an array using a different array but some of the values being inserted are null
  • Trying to understand how you insert variables into jdbc sql queries?
  • How does JFR manage to record stack trace, given it's promises of low-overhead?
  • Throw an exception within / after completable future
  • Why the contains method returns false when trying to pass two identical arrays of Integers
  • Mock connection to RabbitMQ
  • How do I rewrite this double for loop with a an if else using Java 8 lambdas?
  • What's the best way to get the number of days in a month using the java.time API?
  • Basic question about for loop with char array (Java)
  • how Java annotation @Suppresswarnings works?
  • Why is the Exponent for Float (32 Bit) in Java -126 and not -128?
  • How to write a java variable value in message of validator
  • How to fix "java.lang.ArrayIndexOutOfBoundsException: 1" injava
  • Get erorrs when use @SpringBootApplication instend @EnableAutoConfiguration in Spring boot
  • Java if statment ending with a semi-colim
  • How to upload files using JDK 11 java.net.http.HttpClient?
  • How to send arbitrary numbers and types or arguements
  • how to call many methods in another in android studio
  • Why the passed array cannot use lambda function
  • how will you handle the exception for overridden method
  • how to use globalKtable and StateStore on the same topic?
  • Can I use many listIterators sequentially to mutate or remove list elements from an ArrayList in Java?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk