0713 - application-test.yml, ===, join json, @Qualifier, Serializable, @Responsebody, @JsonInclude..

1. Spring加载application-test.yml

By default, Spring Boot will try to load application.yml and application-{profilename}.yml available on classpath, so you could try with application-test.yml

传送门:
Be aware, it will additionally load the application-test.yml, so all properties that are in application.yml are still going to be applied as well. If you don’t want that, either use a profile for those as well, or override them in your application-test.yml.

1.1 Spring can load application-anySuffix.yml except application-test.yml? / application-test.yml cannot override application.yml in IntelliJ, but can override when using gradle build? - not solved

简直了…
然后我就在Stack Overflow问了个问题可是还没人回答:https://stackoverflow.com/questions/62761901/acitiveprofiles-works-for-any-suffix-except-test-for-loading-application-test
觉得应该是IntelliJ哪里设置的不对,但invalid cache&restart也是没用的。这里有IntelliJ的帖子,里面讨论的问题和我的相似,但最终没有解决方案。

我尝试了以下几种方法:

  1. Though the “anySuffix” can load ApplicationContext and load @Value without NullPointerException, but the tests seems abnormal and returns 302 instead of 200.

  2. It works fine with application-test.yml when gradlew.bat build, only in IntelliJ it will fail to load ApplicationContext because it cannot load @Value.

  3. The resources folder already marked as “test resources”.

  4. Have tried invalidate cache and restart

  5. Tried edit configuration -> environment variables -> SPRING_PROFILES_ACTIVE=test

  6. Tried adding @Configuration(In addition, Application should be annotated with @Configuration as was mentioned by someone else), and @EnableAutoConfiguration in my ServiceMain.class.

  7. Tried to use @SpringBootTest(properties=“spring.profiles.active=test”)

  8. Tried solving idea application context not configured for this file

  9. Tried change name suffix from .yml -> .yaml

  10. Actually, in debug mode, I saw the @Value(${spring.profiles.active}) is indeed “test”, but @Value is still loaded from application.yml instead of application-test.yml

2. == vs === ?

…Second difference between equals and == operator is that, == is used to check reference or memory address of the objects whether they point to the same location or not, and equals() method is used to compare the contents of the object e.g. in case of comparing String its characters, in case of Integer it’s their numeric values

2.1 will singleton objects have same memory address?

I guess yes. and If you have a single instance of the class, the == and the equals comparison will always return true.

2.2 if a == b, then is it singleton?

You can compare both variables a == b to see if they reference the same object, but it wouldn’t necessary mean they are a singleton, two variables pointing to the same object would return the same value if compared like this too.

3. join two json

const result = {
  ...obj1,
  ...obj2,
};

0713 - application-test.yml, ===, join json, @Qualifier, Serializable, @Responsebody, @JsonInclude.._第1张图片

4. Spring @Qualifier

@Component("fooFormatter")
public class FooFormatter implements Formatter {
 
    public String format() {
        return "foo";
    }
}
 
@Component("barFormatter")
public class BarFormatter implements Formatter {
 
    public String format() {
        return "bar";
    }
}

@Component
public class FooService {
     
    @Autowired
    private Formatter formatter;  // causing ambiguity 
}
public class FooService {
    @Autowired
    @Qualifier("fooFormatter")
    private Formatter formatter;
}

还能这样用:

@Component
@Qualifier("fooFormatter")
public class FooFormatter implements Formatter {
    //...
}
 
@Component
@Qualifier("barFormatter")
public class BarFormatter implements Formatter {
    //...
}

5. When should we implement Serializable interface?

implement the Serializable interface when you need to store a copy of the object, send them to another process which runs on the same system or over the network.

  1. Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class.
  2. Serializable classes are useful when you want to persist instances of them or send them over a wire.
  3. Instances of Serializable classes can be easily transmitted. Serialization does have some security consequences, however. Read Joshua Bloch’s Effective Java.

6. @Responsebody

有了@ResponseBody是不是就可以不用写一个Response class然后用Jersey的@Produce(Application_Json)了…
好像没关系

Response response = ...
response.readEntity(MyRequest.class)

// MyRequest class need this:
@Builder.Default
String responseType="application/json"

明明responseType感觉没有用到,但没有却不行。。我API用的是@ResponseBody,没有单独写一个Response class然后返回这个POJO class, 而是直接返回一个List。
可能readEntity会读取responseType吧

7. @JsonInclude

8. Jackson Annotations: Difference Between JsonIgnoreProperties(ignoreUnknown=true) and JsonInclude(Include.NON_EMPTY)

@JsonIgnoreProperties(ignoreUnknown=true) is applicable at deserialization of JSON to Java object (POJO) only. If your POJO does not contain certain properties that JSON does contain, they are ignored and no error is thrown.
On the other hand @JsonInclude(Include.NON_EMPTY) is used at serialization of POJO to JSON and it says, skip POJO properties that are: null or what is considered empty are not to be included. Definition of emptiness is data type-specific.

9. Lombok @AllArgsConstructor, @NoArgsConstructor and @RequiredArgsConstructor

9.1 @AllArgsConstructor For All Arguments Constructor

@AllArgsConstructor generates a constructor requiring an argument for every field in the annotated class.

So we have the Employee class with two fields:

@AllArgsConstructor
public class Employee {

    private String name;
    private int salary;
}

When we de-lombok the class, it becomes:

public class Employee {

    private String name;
    private int salary;

    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }
}

9.2 Static Factory Method for @AllArgsConstructor

@AllArgsContstructor also allows the creation of static factory methods using the staticName attribute:

@AllArgsConstructor(staticName = "of")
class Department {

    private String location;
    private String employeeNumber;
}

Consequently, Lombok makes the constructor private and then create a static factory method with the given name:

class Department {

    private String location;
    private String employeeNumber;

    private Department(String location, String employeeNumber) {
        this.location = location;
        this.employeeNumber = employeeNumber;
    }

    public static Department of(String location, String employeeNumber) {
        return new Department(location, employeeNumber);
    }
}

9.3 @NoArgsConstructor For No Argument Constructor

@NoArgsConstructor generates a default constructor with no parameters.

We have the following Employee class:

@NoArgsConstructor
public class Employee {

    private String name;
    private int salary;
}

When we look at the generated code, we see that Lombok adds a no-args constructor:

public class Employee {

    private String name;
    private int salary;

    public Employee() {
    }
}

9.4 Static Factory Method for @NoArgsConstructor

Similar to @AllArgsConstructor, we can create a static factory method for construction purposes using the @NoArgsConstructor annotation:

@NoArgsConstructor(staticName = "of")
class Department {

    private String location;
    private String employeeNumber;
}

mongoTestConfig
Apache Lucene

你可能感兴趣的:(One,week,Learning,Notes,spring,boot)