Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.dependencyinjection.constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BinarySearch {

private static final Logger LOGGER = LoggerFactory.getLogger(BinarySearch.class);

@Autowired
private SortAlgorithm sortAlgorithm;

public BinarySearch(SortAlgorithm sortAlgorithm) {
super();
this.sortAlgorithm = sortAlgorithm;
}

public int binarySearch(int[] numbers, int numberToBeSearched) {
int index = 0;
// Sort the input array
// Search the number in the array
// Get the index
LOGGER.info("Executing Bubble Sort algorithm.");
return index;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.dependencyinjection.constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class BubbuleSortAlgorithm implements SortAlgorithm {

private static final Logger LOGGER = LoggerFactory.getLogger(BubbuleSortAlgorithm.class);

public int[] sort(int[] inputArray) {
// Sort the input array\
int[] sortedArray = {};
return sortedArray;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.dependencyinjection.constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DependencyInjection {

private static final Logger LOGGER = LoggerFactory.getLogger(DependencyInjection.class);

public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DependencyInjection.class, args);
BinarySearch bean = applicationContext.getBean(BinarySearch.class);
bean.binarySearch(new int[] { 1, 2, 3 }, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.dependencyinjection.constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuickSortAlgorithm implements SortAlgorithm {

private static final Logger LOGGER = LoggerFactory.getLogger(QuickSortAlgorithm.class);

public int[] sort(int[] inputArray) {
// Sort the input array
int[] sortedArray = {};
LOGGER.info("Executing Quick Sort algorithm.");
return sortedArray;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.dependencyinjection.constructor;

public interface SortAlgorithm {
public int[] sort(int[] inputArray);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.dependencyinjection.setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BinarySearch {

private static final Logger LOGGER = LoggerFactory.getLogger(BinarySearch.class);
private SortAlgorithm sortAlgorithm;

@Autowired
public void setSortAlgorithm(SortAlgorithm sortAlgorithm) {
this.sortAlgorithm = sortAlgorithm;
}

public int binarySearch(int[] numbers, int numberToBeSearched) {
int index = 0;
// Sort the input array
// Search the number in the array
// Get the index
LOGGER.info("Executing Bubble Sort algorithm.");
return index;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.dependencyinjection.setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class BubbuleSortAlgorithm implements SortAlgorithm {

private static final Logger LOGGER = LoggerFactory.getLogger(BubbuleSortAlgorithm.class);

public int[] sort(int[] inputArray) {
// Sort the input array\
int[] sortedArray = {};
return sortedArray;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.dependencyinjection.setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DependencyInjectionBySetterApplication {

private static final Logger LOGGER = LoggerFactory.getLogger(DependencyInjectionBySetterApplication.class);


public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DependencyInjectionBySetterApplication.class, args);
BinarySearch bean = applicationContext.getBean(BinarySearch.class);
bean.binarySearch(new int[] { 1, 2, 3 }, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.dependencyinjection.setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuickSortAlgorithm implements SortAlgorithm {

private static final Logger LOGGER = LoggerFactory.getLogger(QuickSortAlgorithm.class);

public int[] sort(int[] inputArray) {
// Sort the input array
int[] sortedArray = {};
System.out.println("Executing Quick Sort algorithm.");
return sortedArray;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.dependencyinjection.setter;

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