Spring inicial, APUNTES, MASTER CLASS, CRACKING SPRING LAST VERSION Part2
funcion main(), que hace inyección mediante interfaz
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 arrayreturn 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;@Componentpublic class BinarySearchImpl {@Autowiredprivate SortAlgorithm sortAlgorithm;public int binarySearch(int[] numbers, int numberToSearchFor) {int[] sortedNumbers = sortAlgorithm.sort(numbers);System.out.println(sortAlgorithm);// Search the arrayreturn 3;}}
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;
}
}
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;
}
}
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