0

I am trying to add a conditional yaml config file in a properties file but it's not able to read the env variable

hibernate.cache.redisson.config=redisson-${REDIS_MODE}.yaml

it is giving error

    Suppressed: java.io.FileNotFoundException: redisson-.yaml (No such file or directory)

How can we resolve this?

2 Answers 2

0

It seems like the ${REDIS_MODE} env variable is not set correctly. Check the error message - the suffix from the env var is not present in the error message's file name. Ensure that env var is properly set and matching your configuration file and then retry.

Sign up to request clarification or add additional context in comments.

1 Comment

REDIS_MODE is set in application.properties file and I am trying to access it here in hibernate.properties file. When providing the same key in catalina.sh it's picking it up correctly. Also it is being fetched correctly from application.properties in RedissonConfiguration class with @Value annotation.
0

Will be great if you can give the files structure. But base on the existing information, you can or programmatically set the Hibernate property, like:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class RedissonConfiguration {

    @Value("${REDIS_MODE}")
    private String redisMode;

    @Autowired
    private Environment env;

    @Bean
    public RedissonClient redissonClient() throws IOException {
        String configFile = "redisson-" + redisMode + ".yaml";
        Config config = Config.fromYAML(new File(configFile));

        // Set hibernate properties programmatically
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.cache.redisson.config", configFile);

        applyHibernateProperties(hibernateProperties);

        return Redisson.create(config);
    }

    private void applyHibernateProperties(Properties hibernateProperties) {
        // Apply these properties to your Hibernate configuration
    }
}

Or add property source annotation in your conf class:

@Configuration
@PropertySource("classpath:hibernate.properties")
public class HibernateConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        // Now you can resolve ${REDIS_MODE}
        String redisConfigFile = env.getProperty("hibernate.cache.redisson.config");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        // This will now resolve the placeholder
        properties.setProperty("hibernate.cache.redisson.config", env.getProperty("hibernate.cache.redisson.config"));
        return properties;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.