diff --git a/README.md b/README.md index fdb6ccd..733ed84 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Jave Basic Tech 1. update to java8 2. some basic tec in java8 -3. add lambda expression \ No newline at end of file +3. add lambda expression +4. test uploading. \ No newline at end of file diff --git a/basic-java8-core/src/main/java/edu/gqq/algorithm/Fibonacci.java b/basic-java8-core/src/main/java/edu/gqq/algorithm/Fibonacci.java new file mode 100644 index 0000000..5c569e1 --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/algorithm/Fibonacci.java @@ -0,0 +1,21 @@ +package edu.gqq.algorithm; + +public class Fibonacci { + + public static void main(String[] args) { + // output the result of Fibonacci. 1 -> 1, 2 -> 1, 3 -> 2, 4->3, 5->5, 6->8, 7-> 13 ... + int n = 10; + int fibo = getFiboByN(n); + System.out.println(String.format("fibo(%d) is %d", n, fibo)); + } + + private static int getFiboByN(int n) { + int x = 1, y = 1; + for (int i = 3; i <= n; i++) { + int temp = x + y; + x = y; + y = temp; + } + return y; + } +} diff --git a/basic-java8-core/src/main/java/edu/gqq/algorithm/ReverseArray.java b/basic-java8-core/src/main/java/edu/gqq/algorithm/ReverseArray.java new file mode 100644 index 0000000..5af127f --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/algorithm/ReverseArray.java @@ -0,0 +1,38 @@ +package edu.gqq.algorithm; + +public class ReverseArray { + + public static void main(String[] args) { + reverse(); + reverse2(); + } + + private static void reverse2() { + int[] arr = {3, 5, 23, 343, 2342, 2, 1}; + int len = arr.length; + for (int i = 0; i < len / 2; i++) { + int temp = arr[i]; + arr[i] = arr[len - 1 - i]; + arr[len - 1 - i] = temp; + } + System.out.print("{"); + for (int ele : arr) { + System.out.print(ele + ","); + } + System.out.println("}"); + } + + private static void reverse() { + int[] arr = {3, 5, 23, 343, 2342, 2, 1}; + for (int i = 0, j = arr.length - 1; i < j; i++, j--) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + System.out.print("{"); + for (int ele : arr) { + System.out.print(ele + ","); + } + System.out.println("}"); + } +} diff --git a/basic-java8-core/src/main/java/edu/gqq/funcinterface/ConsumerTest.java b/basic-java8-core/src/main/java/edu/gqq/funcinterface/ConsumerTest.java new file mode 100644 index 0000000..0d66c5d --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/funcinterface/ConsumerTest.java @@ -0,0 +1,108 @@ +package edu.gqq.funcinterface; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Stream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConsumerTest { + + private static final Logger logger = LoggerFactory.getLogger(ConsumerTest.class); + + public static void main(String[] args) { + // how to use consumer string + Consumer printConsumer = str -> System.out.println(str); + printConsumer.accept("test str");//test str + + Stream cities = Stream.of("Sydney", "Dhaka", "New York", "London"); + // why param in foreach is Consumer action? + // because in the foreach method, it must call action.accept(T type). + // so, the Consumer action must be the superclass of T. + cities.forEach(printConsumer); + + // Consumer is lambda expression. You can imagine that's a method. + // this method can be called with params + Consumer integerConsumer = integer -> { + System.out.println(integer + 3); + }; + integerConsumer.accept(5);//8 + + // 3. test getConsumer + // 3.1 Integer + Consumer consumer = getConsumer("string1", 3); + consumer.accept(5); // 5string13 + // 3.2 String + getConsumer("second", "third").accept("first"); + // 3.3 object + getConsumer(new Object(), new Object()).accept(new Object()); + + // 4. test super + // why param in exec is Consumer action? + // because in the exec method, it must call action.accept(T type). + // so, the Consumer action must be the superclass of T. + // If this Consumer is , that means it can accept T type as its parameter. + // 4.1 FileExec String + Consumer printBookInfo = _identifier -> { + if (_identifier instanceof String) { + System.out.println("ISBN is " + _identifier); + } else if (_identifier instanceof BigInteger) { + System.out.println("book id is " + ((BigInteger) _identifier).intValue()); + } else { + System.out.println("UNKNOWN type"); + } + }; + + System.out.println("---------------------- 4.1 FileExec String ----------------------"); + FileExec bookIsbn = new FileExec<>(); + FileExec bookId = new FileExec<>(); + bookIsbn._identifier = "BSK2342034234"; + bookIsbn.exec("Super hero", printBookInfo); + + System.out.println("---------------------- 4.2 FileExec BigInteger ----------------------"); + bookId._identifier = BigInteger.valueOf(134397934L); + bookId.exec("Find the world", printBookInfo); + + // 5. test andThen + testAndThen(); + } + + private static void testAndThen() { + System.out.println("----------------------test andThen----------------------"); + + List cities = Arrays.asList("Sydney", "Dhaka", "New York", "London"); + Consumer> upperCaseConsumer = list -> { + // please find a stream way to change a list into Upper Case. +// List collect = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList()); +// System.out.println(list); +// System.out.println(collect); + list.replaceAll(x -> x.toUpperCase()); + +// for (int i = 0; i < list.size(); i++) { +// list.set(i, list.get(i).toUpperCase()); +// } + }; + Consumer> printConsumer = list -> list.stream().forEach(System.out::println); + upperCaseConsumer.andThen(printConsumer).accept(cities); + } + + public static Consumer getConsumer(Object obj, T val) { +// T tObj = (T) obj; + return t -> { + System.out.printf("%s_%s_%s%n", t.toString(), obj.toString(), val.toString()); + }; + } +} + +class FileExec { + + private static final Logger logger = LoggerFactory.getLogger(FileExec.class); + T _identifier; + + public void exec(String fileName, Consumer action) { + logger.info("fileName is " + fileName); + action.accept(_identifier); + } +} diff --git a/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/ActionBase.java b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/ActionBase.java new file mode 100644 index 0000000..f0ff45e --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/ActionBase.java @@ -0,0 +1,73 @@ +package edu.gqq.funcinterface.actiontest; + +import java.math.BigInteger; +import org.apache.commons.lang3.StringUtils; + +public class ActionBase { + + private String actionName; + private int actionID; + + public ActionBase(int actId, String name) { + this.actionID = actId; + this.actionName = name; + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public int getActionID() { + return actionID; + } + + public void setActionID(int actionID) { + this.actionID = actionID; + } + + @Override + public String toString() { + return "ActionBase{" + + "actionName='" + actionName + '\'' + + ", actionID=" + actionID + + '}'; + } +} + +class BlockAction extends ActionBase { + + private final String blockReason; + + public BlockAction(int actId, String name, String reason) { + super(actId, name); + this.blockReason = StringUtils.isBlank(reason) ? "UNKNOWN" : reason; + } + + public String getBlockReason() { + return blockReason; + } +} + +class SendSMSAction extends ActionBase { + + private final String smsMsg; + private final BigInteger accountNumber; + + public String getSmsMsg() { + return smsMsg; + } + + public BigInteger getAccountNumber() { + return accountNumber; + } + + public SendSMSAction(int actId, String name, String accNo, String smsMsg) { + super(actId, name); + this.smsMsg = smsMsg; + this.accountNumber = new BigInteger(accNo); + } +} \ No newline at end of file diff --git a/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/BasicActionProcessor.java b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/BasicActionProcessor.java new file mode 100644 index 0000000..3c5780d --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/BasicActionProcessor.java @@ -0,0 +1,55 @@ +package edu.gqq.funcinterface.actiontest; + + +import edu.gqq.funcinterface.actiontest.Decision.DecisionStatus; +import java.security.cert.CertPathValidatorException.BasicReason; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import org.apache.commons.collections4.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BasicActionProcessor implements IActionProcessor { + + private static Logger logger = LoggerFactory.getLogger(BasicActionProcessor.class); + + @Override + public Consumer getProcessActionConsumer(Map map, T action) { + return (Decision x) -> { + if (x == null || action == null || MapUtils.isEmpty(map)) { + logger.debug("decision, action or map is null"); + return; + } + + if (DecisionStatus.Disabled.equals(x.getStatus())) { + logger.debug("decision status is disabled"); + return; + } + + logger.debug(map.toString()); + logger.debug(action.toString()); + logger.debug(x.toString()); + }; + } + + public static void main(String[] args) { + BasicActionProcessor basicAction = new BasicActionProcessor<>(); + ActionBase action1 = new ActionBase(101, "basicAction1"); + ActionBase action2 = new ActionBase(102, "basicAction2"); + Map map = new HashMap<>(); + map.put("101", action1); + map.put("102", action2); + + basicAction.getProcessActionConsumer(null, action1). + accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); + basicAction.getProcessActionConsumer(map, null). + accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); + basicAction.getProcessActionConsumer(map, null). + accept(null); + basicAction.getProcessActionConsumer(map, action1). + accept(new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED)); + basicAction.getProcessActionConsumer(map, action1). + accept(new Decision(DecisionStatus.Enabled, BasicReason.EXPIRED)); + } +} diff --git a/basic-java8-core/src/main/java/edu/gqq/stream/Decision.java b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/Decision.java similarity index 50% rename from basic-java8-core/src/main/java/edu/gqq/stream/Decision.java rename to basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/Decision.java index 7a66ab9..61a2f9e 100644 --- a/basic-java8-core/src/main/java/edu/gqq/stream/Decision.java +++ b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/Decision.java @@ -1,33 +1,42 @@ -package edu.gqq.stream; +package edu.gqq.funcinterface.actiontest; import java.security.cert.CertPathValidatorException; public class Decision { - public String getStatus() { - return status; + public Decision(DecisionStatus status, CertPathValidatorException.Reason reason) { + this.status = status; + this.reason = reason; } - public void setStatus(String status) { - this.status = status; + public DecisionStatus getStatus() { + return status; } public CertPathValidatorException.Reason getReason() { return reason; } - public void setReason(CertPathValidatorException.Reason reason) { - this.reason = reason; - } - /** * 3DS decision status code */ - private String status = null; + private DecisionStatus status = null; /** * Reason of the risk evaluation by issuer for inroamtional purposes */ private CertPathValidatorException.Reason reason = null; + + @Override + public String toString() { + return "Decision{" + + "status=" + status.toString() + + ", reason=" + reason.toString() + + '}'; + } + + public enum DecisionStatus { + Enabled, Disabled + } } diff --git a/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/IActionProcessor.java b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/IActionProcessor.java new file mode 100644 index 0000000..b38f0de --- /dev/null +++ b/basic-java8-core/src/main/java/edu/gqq/funcinterface/actiontest/IActionProcessor.java @@ -0,0 +1,112 @@ +package edu.gqq.funcinterface.actiontest; + +import edu.gqq.funcinterface.actiontest.Decision.DecisionStatus; +import java.security.cert.CertPathValidatorException.BasicReason; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * interface testing action processor. It returns a Decision Consumer. (map, action) -> Consumer + */ +public interface IActionProcessor { + + Consumer getProcessActionConsumer(Map map, T action); +} + +/** + * please look at the return value. + * + * 1.if it's a primitive or Object, you can return specific one. + * + * 2. if it's a interface which only has one method, please use lambda expression. + * + * (parm1, param2 ...) -> {return type. } + * + * 3. int a = 1. Object o = new Object(). Action a = x -> {} + * + * 4. If return type is interface, that means return a method to be used in the future. This interface will + * use some features of the parameters which will be passed in the future. + */ +class ActionTest { + + private static Logger logger = LoggerFactory.getLogger(ActionTest.class); + + // This is a connection between decision, map and action. + // like map contains action id. decision reason == action reason. Then we can output matched. + public IActionProcessor getBlockActionProcessor() { + return (map, act) -> + decision -> { + if (DecisionStatus.Disabled.equals(decision.getStatus())) { + logger.debug("decision Status is disabled"); + return; + } + if (map.containsKey(act.getActionID() + "") + && decision.getReason() != null + && decision.getReason().toString().equals(act.getBlockReason())) { + logger.debug(String.format("block action is taking effect. ID:%s, reason: %s", + act.getActionID(), act.getBlockReason())); + } else { + logger.debug(String.format("block action %s is NOT taking effect", act.getActionID())); + } + }; + } + + public IActionProcessor getSendSMSActionProcessor() { + return (map, act) -> + decision -> { + if (DecisionStatus.Disabled.equals(decision.getStatus())) { + logger.debug("decision Status is disabled"); + return; + } + if (map.containsKey(act.getActionID() + "") + && decision.getReason() != null + && decision.getReason().toString().equals(act.getSmsMsg())) { + logger.debug(String.format("block action is taking effect. ID:%s, reason: %s", + act.getActionID(), act.getSmsMsg())); + } else { + logger.debug(String.format("block action %s is NOT taking effect", act.getActionID())); + } + }; + } + + public static void main(String[] args) { + ActionTest actionTest = new ActionTest(); + Map map = new HashMap<>(); + BlockAction validAction = new BlockAction(12135, "validAction", "EXPIRED"); + BlockAction validAction2 = new BlockAction(12136, "validAction2", "NOT_YET_VALID"); + BlockAction validAction3 = new BlockAction(12137, "validAction3", "INVALID_SIGNATURE"); + map.put(validAction.getActionID() + "", validAction); + map.put(validAction2.getActionID() + "", validAction2); + map.put(validAction3.getActionID() + "", validAction3); + + Decision enabledDecision = new Decision(DecisionStatus.Enabled, BasicReason.EXPIRED); + Decision enabledDecision2 = new Decision(DecisionStatus.Enabled, BasicReason.INVALID_SIGNATURE); + Decision disableDecision = new Decision(DecisionStatus.Disabled, BasicReason.EXPIRED); + + IActionProcessor blockActionProcessor = actionTest.getBlockActionProcessor(); + Consumer decisionConsumer = blockActionProcessor.getProcessActionConsumer(map, validAction); + decisionConsumer.accept(enabledDecision); + decisionConsumer.accept(enabledDecision2); + decisionConsumer.accept(disableDecision); + + System.out.println(); + actionTest.getBlockActionProcessor().getProcessActionConsumer(map, validAction3).accept(enabledDecision2); + actionTest.getBlockActionProcessor().getProcessActionConsumer(map, validAction3).accept(disableDecision); + + logger.info("---------------------- test SMS info ----------------------"); + IActionProcessor sendSMSActionProcessor = actionTest.getSendSMSActionProcessor(); + Map smsMap = new HashMap<>(); + SendSMSAction sendSMSAction1 = new SendSMSAction(20000, "20000", "100001", "No"); + SendSMSAction sendSMSAction2 = new SendSMSAction(20001, "20001", "100001", "ALGORITHM_CONSTRAINED"); + + smsMap.put(sendSMSAction1.getActionID() + "", sendSMSAction1); + smsMap.put(sendSMSAction2.getActionID() + "", sendSMSAction2); + Decision finalDecision = new Decision(DecisionStatus.Enabled, BasicReason.ALGORITHM_CONSTRAINED); + sendSMSActionProcessor.getProcessActionConsumer(smsMap, sendSMSAction1).accept(finalDecision); + sendSMSActionProcessor.getProcessActionConsumer(smsMap, sendSMSAction2).accept(finalDecision); + } +} + diff --git a/basic-java8-core/src/main/java/edu/gqq/stream/ActionBase.java b/basic-java8-core/src/main/java/edu/gqq/stream/ActionBase.java deleted file mode 100644 index f8fc19b..0000000 --- a/basic-java8-core/src/main/java/edu/gqq/stream/ActionBase.java +++ /dev/null @@ -1,34 +0,0 @@ -package edu.gqq.stream; - -public class ActionBase { - public String getActionName() { - return actionName; - } - - public void setActionName(String actionName) { - this.actionName = actionName; - } - - public int getActionID() { - return actionID; - } - - public void setActionID(int actionID) { - this.actionID = actionID; - } - - private String actionName; - private int actionID; -} - -class Action1 extends ActionBase{ - private String reason = "Action1"; - - public String getReason() { - return reason; - } -} - -class Action2 extends ActionBase{ - -} \ No newline at end of file diff --git a/basic-java8-core/src/main/java/edu/gqq/stream/ConsumerTest.java b/basic-java8-core/src/main/java/edu/gqq/stream/ConsumerTest.java deleted file mode 100644 index 37da08b..0000000 --- a/basic-java8-core/src/main/java/edu/gqq/stream/ConsumerTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package edu.gqq.stream; - -import java.util.function.Consumer; - -public class ConsumerTest { - - public static void main(String[] args) { - - // how to use consumer string - Consumer stringConsumer = str -> System.out.println(str); - - // Consumer is lambda expression. You can imagine that's a method. - // this method can be called with params - Consumer integerConsumer = integer -> { - System.out.println(integer + 3); - }; - - stringConsumer.accept("test str");//test str - integerConsumer.accept(5);//8 - - // 3. test getConsumer - Consumer consumer = getConsumer("string1", 3); - consumer.accept(5); // 5string13 - - } - - public static Consumer getConsumer(Object obj, T val) { -// T tObj = (T) obj; - return t -> { - System.out.println(t.toString() + obj.toString() + val.toString()); - }; - } -} diff --git a/basic-java8-core/src/main/java/edu/gqq/stream/IAction.java b/basic-java8-core/src/main/java/edu/gqq/stream/IAction.java deleted file mode 100644 index ecac89d..0000000 --- a/basic-java8-core/src/main/java/edu/gqq/stream/IAction.java +++ /dev/null @@ -1,52 +0,0 @@ -package edu.gqq.stream; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; - -/** - * interface testing - * - * @param - */ -public interface IAction { - Consumer processAction(Map map, T action); -} - -/** - * please look at the return value. - * 1.if it's a primitive or Object, you can return specific one. - * 2. if it's a interface which only has one method, please use lambda expression. - * (parm1, param2 ...) -> { - * return type. - * } - * - * 3. int a = 1. Object o = new Object(). Action a = x -> {} - * 4. If return type is interface, that means return a method to be used in the future. This interface will use some features of the - * parameters which will be passed in the future. - */ -class ActionTest { - public IAction getAction1() { - return (map, act) -> { - return decision -> { - if (map.containsKey(act.getReason())) { - System.out.println("That's correct!" + decision.getStatus()); - } else { - System.out.println("That's wrong!"); - } - }; - }; - } - - public static void main(String[] args) { - ActionTest actionTest = new ActionTest(); - Map map = new HashMap<>(); - map.put("Action1", "Action1"); - // should be "That's correct! null" - // Here is the place of future using. - // 1. we get returned interfaces. - // 2. we are using the methods of these interfaces. - // 3. we pass parameters into these methods of the interfaces. - actionTest.getAction1().processAction(map, new Action1()).accept(new Decision()); - } -} - diff --git a/basic-java8-core/src/main/java/edu/gqq/stream/StreamBasicDemo.java b/basic-java8-core/src/main/java/edu/gqq/stream/StreamBasicDemo.java index da95152..ed293a6 100644 --- a/basic-java8-core/src/main/java/edu/gqq/stream/StreamBasicDemo.java +++ b/basic-java8-core/src/main/java/edu/gqq/stream/StreamBasicDemo.java @@ -25,7 +25,7 @@ public static void main(String[] args) { // For that purpose object streams support the special mapping operations mapToInt(), mapToLong() and mapToDouble: Stream.of("a1", "a100", "a3") .map(str -> str.substring(1)) - .mapToInt(Integer::parseInt) + .mapToInt(str -> Integer.parseInt(str)) .max() .ifPresent(x -> logger.debug(x + "")); diff --git a/basic-java8-core/src/main/resources/logback.xml b/basic-java8-core/src/main/resources/logback.xml index 6c7d685..be679c1 100644 --- a/basic-java8-core/src/main/resources/logback.xml +++ b/basic-java8-core/src/main/resources/logback.xml @@ -1,17 +1,19 @@ - - - - %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n - - - + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}.%M\(%line\) - %msg%n + + + - - - - - - - - \ No newline at end of file + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 876d194..7e89edd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,58 +1,63 @@ - 4.0.0 + 4.0.0 - - edu.gqq - basic-java8 - 0.1-SNAPSHOT - pom + + edu.gqq + basic-java8 + 0.1-SNAPSHOT + pom - - - basic-java8-core - old-code - springboot - + + + basic-java8-core + old-code + springboot + - - 4.13.1 - 3.11 - 1.2.3 - + + 4.13.1 + 3.11 + 4.4 + 1.2.3 + - - - ch.qos.logback - logback-classic - ${logbackVer} - + + + ch.qos.logback + logback-classic + ${logbackVer} + + + junit + junit + ${junitVersion} + + + org.apache.commons + commons-lang3 + ${commonLang3Ver} + + + org.apache.commons + commons-collections4 + ${commonCollectionVer} + + - - junit - junit - ${junitVersion} - - - org.apache.commons - commons-lang3 - ${commonLang3Ver} - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - 1.8 - 1.8 - - - - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + \ No newline at end of file diff --git a/springboot/pom.xml b/springboot/pom.xml index 298924b..20b5d59 100644 --- a/springboot/pom.xml +++ b/springboot/pom.xml @@ -1,86 +1,99 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.4.0 - - - edu.gqq - gqq-springboot - 0.0.1-SNAPSHOT - gqq-springboot - Demo project for Spring Boot + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + edu.gqq + gqq-springboot + 0.0.1-SNAPSHOT + gqq-springboot + Demo project for Spring Boot + + + 1.8 + 1.2.3 + 2.5.8.RELEASE + 1.15.0 + edu.gqq.gqqspringboot.GqqSpringbootApplication + + - - 1.8 - 1.2.3 - 2.5.8.RELEASE - 1.15.0 - - edu.sources.kafka.KafkaApplication - + + + spring-releases + https://repo.spring.io/libs-release + + - - - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - test - - - ch.qos.logback - logback-classic - ${logbackVer} - + + + spring-releases + https://repo.spring.io/libs-release + + - - - org.springframework.kafka - spring-kafka - ${spring-kafka.version} - - - com.fasterxml.jackson.core - jackson-databind - - - org.springframework.kafka - spring-kafka-test - ${spring-kafka.version} - test - - - org.testcontainers - kafka - ${testcontainers-kafka.version} - test - - + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + ch.qos.logback + logback-classic + ${logbackVer} + - - - - org.springframework.boot - spring-boot-maven-plugin - - ${start-class} - - - - + + + org.springframework.kafka + spring-kafka + ${spring-kafka.version} + + + com.fasterxml.jackson.core + jackson-databind + + + org.springframework.kafka + spring-kafka-test + ${spring-kafka.version} + test + + + org.testcontainers + kafka + ${testcontainers-kafka.version} + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${start-class} + + + + \ No newline at end of file diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationContextAwareTest.java b/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationContextAwareTest.java new file mode 100644 index 0000000..df3601f --- /dev/null +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationContextAwareTest.java @@ -0,0 +1,19 @@ +package edu.gqq.gqqspringboot; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +public class ApplicationContextAwareTest implements ApplicationContextAware { + + ApplicationContext context; + + public ApplicationContext getContext() { + return this.context; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context = applicationContext; + } +} diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationTestApplication.java b/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationTestApplication.java new file mode 100644 index 0000000..900e2cb --- /dev/null +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/ApplicationTestApplication.java @@ -0,0 +1,28 @@ +package edu.gqq.gqqspringboot; + +import edu.gqq.gqqspringboot.beans.AnyBean; +import edu.gqq.util.SpringUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class ApplicationTestApplication { + static Logger logger = LoggerFactory.getLogger(ApplicationTestApplication.class); + + public static void main(String[] args) { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml"); + ApplicationContextAwareTest appcontext = (ApplicationContextAwareTest) context.getBean("appcontext"); + ApplicationContext appCon = appcontext.getContext(); + AnyBean a = (AnyBean) appCon.getBean("anybean"); + + a.doTask(); + context.registerShutdownHook(); + + logger.debug("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = appCon.getBeanDefinitionNames(); + SpringUtil.printBean(beanNames, logger); + } +} diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/GqqRestAPI.java b/springboot/src/main/java/edu/gqq/gqqspringboot/GqqRestAPI.java index 59535f2..604fa9b 100644 --- a/springboot/src/main/java/edu/gqq/gqqspringboot/GqqRestAPI.java +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/GqqRestAPI.java @@ -1,5 +1,6 @@ package edu.gqq.gqqspringboot; +import edu.gqq.gqqspringboot.beans.About; import edu.gqq.gqqspringboot.beans.UserBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,12 +12,21 @@ public class GqqRestAPI { Logger logger = LoggerFactory.getLogger(GqqRestAPI.class); + @Autowired UserBean userBean; + @Autowired + About about; + @RequestMapping("/") public String index() { logger.debug("GqqRestAPI is requested!"); return "Greetings from Spring Boot! " + userBean.getName(); } + + @RequestMapping("/about") + public String about() { + return about.getMsg(); + } } diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/GqqSpringbootApplication.java b/springboot/src/main/java/edu/gqq/gqqspringboot/GqqSpringbootApplication.java index f21a643..9344476 100644 --- a/springboot/src/main/java/edu/gqq/gqqspringboot/GqqSpringbootApplication.java +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/GqqSpringbootApplication.java @@ -1,5 +1,6 @@ package edu.gqq.gqqspringboot; +import edu.gqq.util.SpringUtil; import java.util.Arrays; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,6 +11,8 @@ import org.springframework.context.annotation.Bean; //Configuration,EnableAutoConfiguration,ComponentScan +// use mvn org.springframework.boot:spring-boot-maven-plugin:run to start + @SpringBootApplication public class GqqSpringbootApplication { @@ -25,16 +28,9 @@ public static void main(String[] args) { @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { - -// System.out.println("Let's inspect the beans provided by Spring Boot:"); logger.debug("Let's inspect the beans provided by Spring Boot:"); - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { -// System.out.println(beanName); - logger.debug(beanName); - } + SpringUtil.printBean(beanNames, logger); }; } } \ No newline at end of file diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/beans/About.java b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/About.java new file mode 100644 index 0000000..27d2444 --- /dev/null +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/About.java @@ -0,0 +1,17 @@ +package edu.gqq.gqqspringboot.beans; + +import org.springframework.stereotype.Component; + +@Component +public class About { + + private String msg; + + public About() { + msg = "Welcome MSG!"; + } + + public String getMsg() { + return msg; + } +} diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/beans/AnyBean.java b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/AnyBean.java new file mode 100644 index 0000000..92a16cd --- /dev/null +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/AnyBean.java @@ -0,0 +1,8 @@ +package edu.gqq.gqqspringboot.beans; + +public class AnyBean { + + public void doTask() { + System.out.println("Do some task."); + } +} diff --git a/springboot/src/main/java/edu/gqq/gqqspringboot/beans/UserBean.java b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/UserBean.java index b9630b3..fe16d69 100644 --- a/springboot/src/main/java/edu/gqq/gqqspringboot/beans/UserBean.java +++ b/springboot/src/main/java/edu/gqq/gqqspringboot/beans/UserBean.java @@ -1,16 +1,20 @@ package edu.gqq.gqqspringboot.beans; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class UserBean { + Logger logger = LoggerFactory.getLogger(UserBean.class); private String name; private int id; public UserBean() { id = 133; name = "Peter"; + logger.debug("construct user bean."); } public String getName() { diff --git a/springboot/src/main/java/edu/gqq/util/SpringUtil.java b/springboot/src/main/java/edu/gqq/util/SpringUtil.java new file mode 100644 index 0000000..c30ec18 --- /dev/null +++ b/springboot/src/main/java/edu/gqq/util/SpringUtil.java @@ -0,0 +1,14 @@ +package edu.gqq.util; + +import java.util.Arrays; +import org.slf4j.Logger; + +public final class SpringUtil { + + public static void printBean(String[] beanNames, Logger logger) { + Arrays.sort(beanNames); + for (String beanName : beanNames) { + logger.debug(beanName); + } + } +} diff --git a/springboot/src/main/resources/app-conf.xml b/springboot/src/main/resources/app-conf.xml new file mode 100644 index 0000000..cd176ac --- /dev/null +++ b/springboot/src/main/resources/app-conf.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/springboot/src/main/resources/logback.xml b/springboot/src/main/resources/logback.xml index 6c7d685..fd8a40c 100644 --- a/springboot/src/main/resources/logback.xml +++ b/springboot/src/main/resources/logback.xml @@ -14,4 +14,5 @@ + \ No newline at end of file