Git Product home page Git Product logo

readproperties's Introduction

Read Properties

Este proyecto es un ejemplo de como poder leer en un POJO Java normal, es decir no dentro del ciclo de Spring, una propiedad leída desde el application.properties mediante una calse de Configuración.

21/01/2017

Autor

Ubay Díaz Machín

[email protected]

@Davioth

www.davioth.com

Utiliza

  • lombok 1.16.18
  • Spring Starters:
    • spring-boot-starter
    • spring-boot-starter-test
    • Versión: 2.0.0.M7
  • Java 1.8
  • Maven

Ejecución

Este proyecto sólo tiene un test a ser ejecutado, que permite comprobar el concepto.

mvn test

Descipción

  1. Primero tenemos la clase AppConfigProperties que es la clase que carga la propiedades de la forma normal usada en Spring Boot, desde application.properties

  2. Tenemos la clase SpringContextConfigBridgeImpl , que implementa la interfaz SpringContextConfigBridge y ApplicationContextAware.

    • La clase ApplicationContextAware, es propia de Spring y permite obtener el contexto de aplicación en un Bean de forma sencilla. Con el código:
    @Component
    public class SpringContextConfigBridgeImpl implements SpringContextConfigBridge, ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        @Override
        public <T> T getConfigProperties(Class<T> beanPropertie) {
            return applicationContext.getBean(beanPropertie);
        }
    
    
        public static SpringContextConfigBridge config() {
            return applicationContext.getBean(SpringContextConfigBridge.class);
        }
    }
    • Lo princial de la clase es el método estático, config() que nos permite obtener una instancia de este Bean de froma estática.
    • El método getConfigProperties() nos devuelbe el Bean indicado por la Clase que le pasemos. Este método nos permite tener por ejemplo una instancia de AppConfigProperties, para su uso más tarde.
  3. En la clase ClaseNoSpring se utiliza lo indicado anterior para poder recuperar una propiedad.

    public class ClaseNoSpring {
    
        public String getPropertie() {
            return SpringContextConfigBridgeImpl.config()
                    .getConfigProperties(AppConfigProperties.class).getPruebaConcepto();
        }
    }

readproperties's People

Contributors

davioth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.