sábado, 13 de febrero de 2021

Spring inicial, APUNTES, MASTER CLASS, CRACKING SPRING LAST VERSION Part2

 Spring inicial, APUNTES, MASTER CLASS, CRACKING SPRING LAST VERSION Part2



funcion main(), que hace inyección mediante interfaz

ANTES


package com.in28minutes.spring.basics.springin5steps;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringIn5StepsApplication {

public static void main(String[] args) {

BinarySearchImpl binarySearch =

new BinarySearchImpl(new BubbleSortAlgorithm());

int result = binarySearch.binarySearch(new int[] { 12, 4, 6 }, 3);

System.out.println(result);

}

}

CON MODIFICACION


import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringIn5StepsApplication {

// What are the beans?
// What are the dependencies of a bean?
// Where to search for beans? => No need

public static void main(String[] args) {

// BinarySearchImpl binarySearch =
// new BinarySearchImpl(new QuickSortAlgorithm());
// Application Context
ApplicationContext applicationContext =
SpringApplication.run(SpringIn5StepsApplication.class, args);
BinarySearchImpl binarySearch =
applicationContext.getBean(BinarySearchImpl.class);
int result =
binarySearch.binarySearch(new int[] { 12, 4, 6 }, 3);
System.out.println(result);
}




CON MODIFICACION DE BEANS


ANTES
package com.in28minutes.spring.basics.springin5steps;

public class BinarySearchImpl {
private SortAlgorithm sortAlgorithm;
public BinarySearchImpl(SortAlgorithm sortAlgorithm) {
super();
this.sortAlgorithm = sortAlgorithm;
}
public int binarySearch(int[] numbers, int numberToSearchFor) {
int[] sortedNumbers = sortAlgorithm.sort(numbers);
System.out.println(sortAlgorithm);
// Search the array
return 3;
}

}


DESPUES:

VEMOS CLARAMENTE ANOTACION COMPONENT Y AUTOWIRED DONDE SE VA A INJECTAR EL BEAN O LA DEPENDENCIA
package com.in28minutes.spring.basics.springin5steps;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BinarySearchImpl {

@Autowired
private SortAlgorithm sortAlgorithm;
public int binarySearch(int[] numbers, int numberToSearchFor) {

int[] sortedNumbers = sortAlgorithm.sort(numbers);
System.out.println(sortAlgorithm);
// Search the array
return 3;
}

}




ANTES


DESPUES

LA INTERFAZ NO CAMBIA COMO SE PUEDE APRECIAR
package com.in28minutes.spring.basics.springin5steps;

public interface SortAlgorithm {
public int[] sort(int[] numbers);
}


ANTES



VEMOS CLARAMENTE ANOTACION COMPONENT
package com.in28minutes.spring.basics.springin5steps;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class BubbleSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
// Logic for Bubble Sort
return numbers;
}
}
ANTES


package com.in28minutes.spring.basics.springin5steps;

import org.springframework.stereotype.Component;

@Component
public class QuickSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
// Logic for Quick Sort
return numbers;
}
}






IMPORTANTE LOGING EN APPLOCATION PROPERTIES

logging.level.org.springframework = debug



LOG ANOTACIONES
Searching directory [/in28Minutes/git/getting-started-in-5-steps/spring-in-5-steps/target/classes/com/in28minutes/spring/basics/springin5steps] for files matching pattern [/in28Minutes/git/getting-started-in-5-steps/spring-in-5-steps/target/classes/com/in28minutes/spring/basics/springin5steps/**/*.class]
Identified candidate component class: file [/in28Minutes/git/getting-started-in-5-steps/spring-in-5-steps/target/classes/com/in28minutes/spring/basics/springin5steps/BinarySearchImpl.class]
Identified candidate component class: file [/in28Minutes/git/getting-started-in-5-steps/spring-in-5-steps/target/classes/com/in28minutes/spring/basics/springin5steps/BubbleSortAlgorithm.class]

Creating instance of bean 'binarySearchImpl'
Creating instance of bean 'bubbleSortAlgorithm'
Finished creating instance of bean 'bubbleSortAlgorithm'

Constuctor - Autowiring by type from bean name 'binarySearchImpl' via constructor
to bean named 'bubbleSortAlgorithm'
Setter - Autowiring by type from bean name 'binarySearchImpl' to bean named 'bubbleSortAlgorithm'
No Setter or Constructor - Autowiring by type from bean name 'binarySearchImpl' to bean named 'bubbleSortAlgorithm'


Finished creating instance of bean 'binarySearchImpl'



GIT IGNORE
target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

No hay comentarios:

Publicar un comentario

zen consultora

Blogger Widgets

Entrada destacada

Platzy y el payaso Freddy Vega, PLATZI APESTA, PLATZI NO SIRVE, PLATZI ES UNA ESTAFA

  Platzy y los payasos fredy vega y cvander parte 1,  PLATZI ES UNA ESTAFA Hola amigos, este post va a ir creciendo conforme vaya escribiend...