java

Why does Spring Boot discover but not instantiate a @Component?

本秂侑毒 提交于 2021-02-18 22:55:55
问题 I have a Spring Boot application with the following structure com.package Application - annotated with @SpringBootApplication Configuration - annotated with @Configuration Component1 - annotated with @Component, constructor annotated with @Autowired com.package.subpackage Component2 - annotated with @Component, constructor annotated with @Autowired My application class is package com.package; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure

Why does Spring Boot discover but not instantiate a @Component?

蓝咒 提交于 2021-02-18 22:55:36
问题 I have a Spring Boot application with the following structure com.package Application - annotated with @SpringBootApplication Configuration - annotated with @Configuration Component1 - annotated with @Component, constructor annotated with @Autowired com.package.subpackage Component2 - annotated with @Component, constructor annotated with @Autowired My application class is package com.package; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure

JUnit tests: Suppress enum constructor by mocking?

℡╲_俬逩灬. 提交于 2021-02-18 22:50:33
问题 I know that it is possible to mock a single enum(using How to mock an enum singleton class using Mockito/Powermock?), but I have like 1000 of enum values and they can call 5 different constructors. The enum values are often changing in development. I want to really mock only one or two for my JUnit test. I don't care about the rest, but they are still instantiated, which calls some nasty stuff, which loads the values for the enum from the file system. Yes I know It's very bad design. But for

JUnit tests: Suppress enum constructor by mocking?

一曲冷凌霜 提交于 2021-02-18 22:48:37
问题 I know that it is possible to mock a single enum(using How to mock an enum singleton class using Mockito/Powermock?), but I have like 1000 of enum values and they can call 5 different constructors. The enum values are often changing in development. I want to really mock only one or two for my JUnit test. I don't care about the rest, but they are still instantiated, which calls some nasty stuff, which loads the values for the enum from the file system. Yes I know It's very bad design. But for

How can I start spring boot application in docker with profile?

旧时模样 提交于 2021-02-18 22:43:31
问题 I have a simple spring-boot project: -resources -application.yaml -application-test.yaml And I have this Dockerfile : FROM openjdk:8-jdk-alpine EXPOSE 8080 ADD micro-boot.jar micro-boot.jar ENTRYPOINT ["java","-Dspring.profiles.active=test" "-jar","/micro-boot.jar"] 1) I build image - C:\micro-boot>docker build -f Dockerfile -t micro-boot . 2) show all images - C:\micro-boot>docker image ls -a micro-boot latest ccc9a75ebc24 4 seconds ago 112MB 3) try to start C:\micro-boot>docker image ls -a

Is there a equivalent to ChronoUnit.between that returns fraction instead of integer?

余生颓废 提交于 2021-02-18 22:35:27
问题 Methods such is ChronoUnit.HOURS.between(start, end) returns long so I can't get the fraction from there. Is there an alternative method/approach that would return a fraction? 回答1: The whole point of ChronoUnit.HOURS.between(start, end) is to get the number of HOURS between the two timepoints. For example: it's either 1 or 2 hours between, there is no such thing as 1.5658 hours*. If you need more precicion, use another ChronoUnit, i.e. Minutes or seconds. The problem with decimal fractions is

What is the point of Apache Lang3 StopWatch.split()?

我只是一个虾纸丫 提交于 2021-02-18 22:33:46
问题 I am currently evaluating implementations between Apache StopWatch and Guava's Stopwatch and the split functionality in the former intrigued me, but I am struggling to understand what exactly it does, and what value it has. According to the documentation for StopWatch: https://commons.apache.org/proper/commons-lang/javadocs/api-3.9/org/apache/commons/lang3/time/StopWatch.html split() the watch to get the time whilst the watch continues in the background. unsplit() will remove the effect of

减少那该死的 if else 嵌套

冷暖自知 提交于 2021-02-18 22:31:34
点击上方“ Java社区 ”,选择“设为星标” 学习的最好时刻是三年前,其次是现在。 Java社区 点击右侧关注, 领取500G编程资源! 来源: http://t.cn/Ez2s6ba 写在前面 不知大家有没遇到过像“横放着的金字塔”一样的 if else 嵌套: if ( true ) { if ( true ) { if ( true ) { if ( true ) { if ( true ) { if ( true ) { } } } } } } 我并没夸大其词,我是真的遇到过了!嵌套6、7层,一个函数几百行,简!直!看!死!人! if else 作为每种编程语言都不可或缺的条件语句,我们在编程时会大量的用到。但 if else 一般不建议嵌套超过三层,如果一段代码存在过多的 if else 嵌套,代码的可读性就会急速下降,后期维护难度也大大提高。 所以,我们程序员都应该尽量避免过多的 if else 嵌套。下面将会谈谈我在工作中如何减少 if else 嵌套的。 正文 在谈我的方法之前,不妨先用个例子来说明 if else 嵌套过多的弊端。 想象下一个简单分享的业务需求:支持分享链接、图片、文本和图文,分享结果回调给用户(为了不跑题,这里简略了业务,实际复杂得多)。 当接手到这么一个业务时,是不是觉得很简单,稍动下脑就可以动手了: 先定义分享的类型

What is the shortest, best, cleanest way to set a private field via Reflection in Java?

怎甘沉沦 提交于 2021-02-18 22:30:32
问题 Hi I've already worked with Reflection in Java. But if you are using the java standards (e.g. injecting a private field) you have to write a lot of code to get the job done. What is the shortest way to inject a private field in a Java Object? Are there implementations in widely used and production ready libraries? 回答1: Without using external libraries you need to: get the Field instance set the field instance as accessible set the new value As follow: Field f1 = obj.getClass()

When classes are loaded?

人走茶凉 提交于 2021-02-18 22:25:48
问题 Well, I've got such a code: public class Main { public static void main(String[] args) { Test t; //1 Integer i = new Integer(1); //2 t = new Test(); //3 System.out.println(Test4.a); //4 } } class Test { private int a = 10; private Test2 t2; //5 List<Test2> list = new ArrayList<Test2>() { { for (int i = 0; i < a; i++) { add(new Test2()); //6 } } }; } class Test2 extends Test3{ } class Test3 { } class Test4 { public static final int a = 4; } I don't know how (fully or partially) and when