logo
down
shadow

In spring-boot, is it possible to get properties without being a spring bean?


In spring-boot, is it possible to get properties without being a spring bean?

By : Gabriel
Date : November 23 2020, 03:01 PM
I wish this helpful for you No and yes.
No is because that's the whole idea of Spring and dependency injection. You have to use something which will have access to Spring ApplicationContext.
code :


Share : facebook icon twitter icon
Loading properties using @Value into a BeanFactory object using @Bean in Spring Boot

Loading properties using @Value into a BeanFactory object using @Bean in Spring Boot


By : Pleiades-Note
Date : March 29 2020, 07:55 AM
I hope this helps you . Spring often has to query bean definitions for the type of object they produce. Factory beans are always problematic because they can cause dependency cascades in a futile attempt to resolve all dynamic information available before asking for the type.
I think ListFactoryBean is insufficiently precise about its product type (getObjectType() can only return a non-generic List.class). You might be able to write your own factory that is parameterized with the correct generic type. Or you might get away with just declaring the @Bean to return a FactoryBean.
code :
@EnableAutoConfiguration
public class TestClass
{

  protected static class NestedConfiguration {

    @Value("${test.value}")
    String value;

    @Bean
    public FactoryBean<Properties> test1()
    {
        System.out.println("test.value=" + value );
        // ...
        return factory;
    }

  }
    ...

}
Accessing Spring Boot application properties within Groovy bean DSL

Accessing Spring Boot application properties within Groovy bean DSL


By : Sakthi Ram
Date : March 29 2020, 07:55 AM
This might help you You use it with the place holder syntax (watch out to use single quotes, or else groovy will handle it), e.g.:
code :
appName String, '${spring.application.name}'
appName String, environment.getProperty('spring.application.name', 'The Fallback')
Is it possible to set properties for a Spring Boot application from a bean?

Is it possible to set properties for a Spring Boot application from a bean?


By : Depari Galak Ndebiri
Date : March 29 2020, 07:55 AM
hop of those help? Take a look at this for how to configure a port: Spring Boot - how to configure port
Relevant code is this:
code :
@Controller
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
      return (container -> {
        container.setPort(8012);
    });
}
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()
  .sources(SampleController.class)                
  .properties(props)
  .run(args);
Spring Boot Dynamic Bean Creation From Properties File

Spring Boot Dynamic Bean Creation From Properties File


By : K.DG
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can inject all properties as described below (not sure how to do it with your current properties structure, spring allows really anvanced features regarding properties injection, additional examples here)
code :
@ConfigurationProperties(prefix = "yourPrefix")
public class CustomProperties {

  private final Map<String, String> properties = new HashMap<>();

  @Autowired 
  private ApplicationContext applicationContext;      

  @PostConstruct
  public void init() {
    AutowireCapableBeanFactory beanFactory = this.applicationContext.getAutowireCapableBeanFactory();
    // iterate over properties and register new beans
  }

}
beanFactory.registerSingleton("beanName", bean);
Using properties from application.properties while building bean in spring boot configuration annotated class?

Using properties from application.properties while building bean in spring boot configuration annotated class?


By : Sushant Gupta
Date : March 29 2020, 07:55 AM
wish helps you I'm using this approach for different things like cors configurations. There are probably also disadvantages, but so far it has only had advantages for me. I think it's most times better to have config values outside of the code. That allows you, for example, to use different profiles (local/dev/int/prod). But I would recommend to encrypt things like passwords. I'm using Jasypt.
shadow
Privacy Policy - Terms - Contact Us © voile276.org