16.11.18

Ejemplo de uso de @Bean (Spring Framework)

(Ver primero este) ... primero declaramos el método:


import java.util.Arrays;
import java.util.stream.IntStream;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Operacion operacionSuma5en5 ()
    {
        return new Operacion() {
            private int a=0;
            private int b=0;
            @Override
            public void establecerValorA(int a) {
                this.a=a;
            }

            @Override
            public void establecerValorB(int b) {
                this.b=b;
            }

            @Override
            public double realizarCalculo() {
                return IntStream.rangeClosed(Math.min(a,b),Math.max(a,b))
                        .filter(i->(i-a)%5==0).sum();
            }
            
        };
    }
}

Y luego lo usamos en el contenedor (qualifier el nombre del método):


import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class BasicController  {
    
    AtomicInteger i;
  
    @Autowired
    @Qualifier("sumarRango")
    private Operacion op1;

    @Autowired
    @Qualifier("sumarPares")
    private Operacion op2;

    @Autowired
    @Qualifier("operacionSuma5en5")
    private Operacion op3;    
    
    @PostConstruct
    public void init()
    {
        i=new AtomicInteger(0);
        op1.establecerValorA(0);
        op2.establecerValorA(0);
        op3.establecerValorA(0);
    }

    @RequestMapping("/")
    public String[] index() {
        int i=this.i.getAndIncrement();
        op1.establecerValorB(i);
        op2.establecerValorB(i);
        op3.establecerValorB(i);
        String[] results=new String[3];
        results[0]="The sum from "+0+" to "+i+" is " + op1.realizarCalculo()+".";
        results[1]="The sum of even numbers from "+0+" to "+i+" is " + op2.realizarCalculo()+".";
        results[2]="The sum of numbers every 5 from "+0+" to "+i+" is " + op3.realizarCalculo()+".";
        return results;
    }    

}

Pero también se podría hacer así (implementando ApplicationContextAware y usando el método getBean) :


import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class BasicController implements ApplicationContextAware  {
    
    AtomicInteger i;
  
    ApplicationContext ac;
    
    @Autowired
    @Qualifier("sumarRango")
    private Operacion op1;

    @Autowired
    @Qualifier("sumarPares")
    private Operacion op2;
       
    private Operacion op3;    
    
    @PostConstruct
    public void init()
    {
        i=new AtomicInteger(0);
        op3=ac.getBean("operacionSuma5en5", Operacion.class);
        op1.establecerValorA(0);
        op2.establecerValorA(0);
        op3.establecerValorA(0);
    }

    @RequestMapping("/")
    public String[] index() {
        int i=this.i.getAndIncrement();
        op1.establecerValorB(i);
        op2.establecerValorB(i);
        op3.establecerValorB(i);
        String[] results=new String[3];
        results[0]="The sum from "+0+" to "+i+" is " + op1.realizarCalculo()+".";
        results[1]="The sum of even numbers from "+0+" to "+i+" is " + op2.realizarCalculo()+".";
        results[2]="The sum of numbers every 5 from "+0+" to "+i+" is " + op3.realizarCalculo()+".";
        return results;
    }    
    
    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        this.ac=ac;
    }

}

Pero ya se le pierde la gracia a la inyección de dependencias claro está.

No hay comentarios :