navigation

[react] Hooks

[React from zero practice 01- background] code split [React from zero practice 02- background] permission control [React from zero practice 03- background] custom hooks [React from zero practice 04- background] docker-compose Deploy React + Egg +nginx+mysql [React From zero practice 05- background] Gitlab-CI using Docker automated deployment

[source code – Webpack01 – precompiler] AST abstract syntax tree [source code – Webpack02 – Precompiler] Tapable [source code – Webpack03] hand written webpack-compiler simple compilation process [source code] Redux React-redux01 [source] Axios [source] vuex [source -vue01] Data reactive and initialize render [source -vue02] Computed responsive – Initialize, access, Update Procedure [source -vue04] Watch Listening properties – Initialize and update [source -vue04] vue. set and vm.$set [source -vue05] vue.extend

[source -vue06] Vue. NextTick and VM.$nextTick [Deployment 01] Nginx [Deployment 02] Docker deployVUE project [Deployment 03] Gitlab-CI

[Data Structures and Algorithms 01] Binary search and sort

[Deep 01] Execution context [Deep 02] Prototype chain [Deep 03] Inheritance [Deep 04] Event loop [Deep 05] Curri Bias function [Deep 06] Function memory [Deep 07] Implicit conversions and operators [Deep 07] Browser caching mechanism (HTTP caching mechanism) [Deep 08] Front-end security [Deep 09] Deep copy [Deep 10] Debounce Throttle [Deep 10] Front-end routing [Deep 12] Front-end modularization [Deep 13] Observer mode Publish subscribe mode Bidirectional data binding [Deep 14] Canvas [Deep 15] webSocket Webpack HTTP and HTTPS CSS- Interview Handwriting Promise Data Structures and Algorithms – Binary Search and Sorting Js Design Patterns – Agents, policies, singletons

/ front-end learn java01 – SpringBoot combat environment configuration and the HelloWorld service [front-end learn java02 – SpringBoot combat] mybatis + mysql implementation song to add and delete [front-end learn java03 – SpringBoot combat] Lombok, log, Java04 -SpringBoot combat deployment [front-end science Java04 -SpringBoot combat] static resources + interceptor + front and back end file upload [front-end science Java05 -SpringBoot combat] common annotates + Redis implementation statistics function [front-end science Java06 -SpringBoot combat] inject + Swagger2 3.0 + unit test JUnit5

(1) Pre-knowledge

(1) Some words

Condition condition // conditional condition adj camel swagger contact contact implicit implicit assertion The modifierCopy the code

Swagger2 3.0

(1) Install Swagger2 Maven dependent scenario Starter

  • Springfox-boot-starter => Maven address
pom.xml ------- <! -- Swagger --> <! -- automatically generate (interface document) and (self-test tool) --> <! -- Swagger2 3.0 --> <! <dependency> <groupId> IO. GroupId </groupId> < artifactId > springfox - the boot - starter < / artifactId > < version > 3.0.0 < / version > < / dependency > <! -- (1) springfox-swagger2 --> <! --<dependency>--> <! --<groupId>io.springfox</groupId>--> <! --<artifactId>springfox-swagger2</artifactId>--> <! - < version > 3.0.0 < / version > -- > <! --</dependency>--> <! -- (2) springfox-swagger-ui --> <! --dependency>--> <! --<groupId>io.springfox</groupId>--> <! --<artifactId>springfox-swagger-ui</artifactId>--> <! - < version > 3.0.0 < / version > -- > <! --</dependency>-->Copy the code

(2) Write a configuration class for Swagger2

SRC/main/Java/com. Example. The demo/config/Swagger2Config. Java -- -- -- -- -- -- -- @ Configuration / / with the current class is a startup class, Public class Swagger2Config {@bean // Add this component to the IOC container public Docket CreateRestApi () {return new Docket (DocumentationType. SWAGGER_2). ApiInfo (apiInfo ()). The select () / / Swagger2 scanned for package, The scanning controller, Here to fill out the controller folder path. All apis (RequestHandlerSelectors. BasePackage (" com. Example. Demo. Controller ")) .paths(PathSelectors.any()) .build(); } private ApiInfo ApiInfo () {return new ApiInfoBuilder() // Caption.title (" React-admin-java-API builds RESTful APIs with Swagger2 ") Contact(new contact("woow_wu7", "woow_wu7", "woow_wu7", "woow_wu7") "https://github.com/woow-wu7/7-react-admin-java", "[email protected]")) / / service website. TermsOfServiceUrl (" http://120.53.220.141:81/admin-home "). The version (" 1.0 "). The build (); }}Copy the code

(3) accesshttp://localhost:7777/swagger-ui/index.html

  • In the case of remote deployment, access address:Server address /swagger-ui/index.html

(4) Relevant annotations

  • controller
    • @ApiThis annotation is best left unset for the requested class
    • @ApiOperationOn methods of the Controller class, stating the purpose and effect of the method
    • @ApiParamParameters for the request method =>Personal feeling is too intrusive, seriously affected the reading, mixed with business code is messy
    • @ApiImplicitParamsFor request methods => Mainly used for POST requests
    • @ApiImplicitParam Used in the @ApiImplicitParams parameter
  • models => bean || model || dto
    • @ApiModel
    • @ApiModelProperty
// Look up @getMapping ("/getMusicListTest") @ApiOperation(value = "getMusicListTest") // Swagger2 public PaginationTestDTO getMusicList("/getMusicListTest") @requestparam (value = "current", defaultValue = "1") @requestparam (value = "current", defaultValue = "1") @requestparam (name = "current", value = "1", defaultValue = "1") required = false) Integer current, @RequestParam(value = "pageSize", defaultValue = "10") @ApiParam(name = "pageSize", Required = false) Integer pageSize, @requestParam (value = "searchKey", value = "searchKey", DefaultValue = "") @apiparam (name = "searchKey", value = "search box ", defaultValue = "", required = false) String searchKey ) { return musicTestService.getMusicList(current, pageSize, searchKey); }Copy the code
@data @allargsconstructor @noargsconstructor @ApiModel(" song ") public class MusicTestBean {@ApiModelProperty(name = "Name ", value =" song ") private String name; @apiModelProperty (name = "album", value = "album") private String album; @apiModelProperty (name = "singer", value = "singer") private String singer; // singer private Integer id; private String startTime; private String endTime; }Copy the code



(3) unit test JUnit5

  • JUint5 is already integrated with SpringBoot
  • Very simple to use
    • Write test methods and use@test (unit5 version)
    • JUnit’s test classes have Spring’s capabilities
      • For example, you can use @autowired to automatically introduce components in IOC
      • For example, you can use @Transactional to mark test methods and automatically roll back transactions when the test is complete
  • The official documentation

(1) Introduce maven scenario initiator

pom.xml ------- <! -- spring-boot-starter-test --> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>Copy the code

(2) Common annotations for unit tests

  • @test indicates that the method is a Test method
  • ParameterizedTest indicates that the method is a parameter test
  • @repeatedTest indicates that the method is repeatable
  • DisplayName sets the DisplayName for the test class or test method
  • @beforeeach indicates execution BeforeEach unit test
  • @AfterEach
  • @beforeall executes BeforeAll unit tests
  • @AfterAll
  • @tag Unit test category
  • @disabled Indicates that the test class or test method is not executed
  • @timeout The test method will return an error if it exceeds the scheduled time
  • @extendWith provides extended class references for test classes or test methods
  • Difference between @beforeAll and @beforeeach
    • @beforeach is executed BeforeEach test method is executed. There may be more than one test method in a test class, so @beforeeach may be executed multiple times
    • @beforeall is executed BeforeAll test methods are executed, which means that @beforeall is executed only once in a test class
SRC/test/Java/com. Example. The demo/Junit5Test Java -- -- -- -- -- - / * * * @ SpringBootTest composite annotations of these annotations following * @BootstrapWith(SpringBootTestContextBootstrapper.class) * @ExtendWith(SpringExtension.class) => @ExtendWith */ Public class Junit5Test {@test@displayName (" Test method 1 @displayName annotation ") public class Junit5Test {@test@displayName (" Test method 1 @displayName annotation ") public void testDisplayName1() { System.out.println("@DisplayName1"); } @test@displayName (" Test method 2 @displayName annotation ") public void testDisplayName2() {system.out.println (" @displayName2 "); } @test@disabled // Disable @displayName (" Test method @disabled annotation ") public void testDisabled() {system.out.println (" @disabled "); } @ Test @ RepeatedTest (5) / / repeat the Test 5 times @ DisplayName (" Test methods @ RepeatedTest notes ") public void testRepeatedTest () { System.out.println("@RepeatedTest -----5"); } @Test @Timeout(value = 1000, Public void testTimeout() throws InterruptedException {thread.sleep (1200); System.out.println("@Timeout"); } @BeforeEach public void testBeforeEach() { System.out.println("@BeforeEach"); } @AfterEach public void testAfterEach() { System.out.println("@AfterEach"); } @beforeAll static public void testBeforeAll() {// Note: @beforeAll and @Afteral annotated methods must be (static) system.out.println (" @beforeAll "); } @AfterAll static public void testAfterAll() { System.out.println("@AfterAll"); }}Copy the code

(3) the assertion assertions

  • After all test runs are completed, a detailed test report is provided

(3-1) Simple assertion

  • Assertions.assertEquals
  • Assertions.assertSame
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; @SpringBootTest public class Junit5Test2 { int cal(int i, int j) { return i+j; } // @test@displayname (" testing simple assertion ") void testSimpleAssertions() {int CAL = CAL (2,3); // (1) assertEquals => assertEquals(5, CAL, "two values are not equal "); // (2) assertSame => Assert that the Object is the same Object Object obj1 = new Object(); Object obj2 = obj1; AssertSame (obj1, obj2, "Is it the same object => Not the same object "); // The third argument indicates what information will be displayed if the assertion fails}}Copy the code

(3-2) Array assertion

  • Assertions.assertArrayEquals
@test@displayName (" Test array assertion 1 assertArrayEquals") public void testAssertArrayEquals1() {assertArrayEquals(new int[]{1, 2}, new int[]{1, 2}, "data content not equal "); } @test@displayName (" Test array assertion 2 assertArrayEquals") public void testAssertArrayEquals2() {assertArrayEquals(new int[]{1, 2, 3}, new int[]{1, 2}, "data is not equal "); }Copy the code

(3-3) Combination assertions

  • Assertion.assertAll
  • A. right B. right C. right D. right
@test@displayName (" Test combination assertion assertAll") public void all() {assertAll(" Test ", () -> assertTrue(true && true, "Not true"), () -> assertEquals(1, 2, "not equal ")); System.out.println("Assertion. AssertAll, this log will print only if assertions are successful "); }Copy the code

(4) Spring IOC injection —– note that Spring is not SpringBoot

  • Divided into (manual injection) and (automatic injection)

(1) Manual injection

  • Set method injection
    • 1. Provide the set method for the property field
    • 2. Specify attributes in the XML configuration file through the property tag, a child tag of the bean tag
    • 3. When a component is added to IOC, the set is automatically injected into IOC
  • Constructor injection – Similar to set

(2) Automatic injection

  • @Resource
  • @Autowired

(2-1) Automatic injection environment configuration

src/main/resources/bean.xml ------- <? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <! -- Manual injection --> <! -- id: specifies the name of the component in the container. -- class: represents the full path of the bean object --> <! -- property: represents a property in the bean object --> <! -- name: indicates the specific attribute name --> <! -- ref: indicates the name of the component in the container, that is, the id attribute in the bean label --> <! --<bean id="userService" class="org.example.UserService">--> <! --property name="userBean" ref="userBean"/>--> <! --</bean>--> <! --<bean id="userBean" class="org.example.UserBean"></bean>--> <! -- Automatic injection --> <! - 1. Automatic injection allocation need to add the beans = > XMLNS: context = "http://www.springframework.org/schema/context" > <! --2. Add beans => xSI :schemaLocation also adds something, as above --> <! --3. Auto-injection configuration requires adding <context:annotation-config/> <! --4. Note that there is another difference between automatic injection and manual injection of XML configuration in addition to 123: To use userBeans in UserService, Property --> <context:annotation-config/> <bean ID ="userService" class=" org.example.userService "></bean> <bean  id="userBean" class="org.example.UserBean"></bean> </beans>Copy the code

(2-2) Use of the @resource annotation

/** * manual injection * set method injection * 1. Specify properties in the XML configuration file via the property tag, a child of the bean tag. When a component is added to the IOC, it is automatically injected into the IOC by calling a set */ /** * automatic injection ** @resource can be implemented automatically by using the reflection class * 1. The default is to find the corresponding (bean object) by (property field name) => (property name) and (bean tag ID) are the same * 2. If (attribute name) and (bean tag ID) are inconsistent, then * 3 will be looked up through (bean tag class). You may or may not provide a set * 4. You may annotate (on an attribute) or (on a set method) * 5. @resource (name="userBean") * 6.6.1 If the interface is injected with only one implementation class, instantiate normally * 6.2 If the interface has more than one implementation class, Public class UserService {// UserBean UserBean = new UserBean(); // UserBean UserBean = new UserBean(); // JavaBean object @resource (name = "userBean") private userBean userBean; @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") Private UserInterface UserInterface; private UserInterface UserInterface; public void test() { userBean.getName2(); userInterface.getName3(); }}Copy the code

(2-3) @Autowired + @Qualifier

  • @AutoWired defaults to the class attribute of the bean tag in the XML
  • Property fields may or may not provide a set method
  • @autowire can be written on the set method as well as on the property
  • If you want to find a bean object by specifying a name, you need to combine it with the @Qualifier annotation
  • The value in @qualifier (value = “autowiredBean2”) corresponds to the ID attribute of the bean tag in the XML
src/main/resources/bean.xml ------- <? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <! -- Manual injection --> <! -- id: specifies the name of the component in the container. -- class: represents the full path of the bean object --> <! -- property: represents a property in the bean object --> <! -- name: indicates the specific attribute name --> <! -- ref: indicates the name of the component in the container, that is, the id attribute in the bean label --> <! --<bean id="userService" class="org.example.UserService">--> <! --property name="userBean" ref="userBean"/>--> <! --</bean>--> <! --<bean id="userBean" class="org.example.UserBean"></bean>--> <! -- Automatic injection --> <! - 1. Automatic injection allocation need to add the beans = > XMLNS: context = "http://www.springframework.org/schema/context" > <! --2. Add beans => xSI :schemaLocation also adds something, as above --> <! --3. Auto-injection configuration requires adding <context:annotation-config/> <! --4. Note that there is another difference between automatic injection and manual injection of XML configuration in addition to 123: To use userBeans in UserService, Property --> <context:annotation-config/> <bean ID ="userService" class=" org.example.userService "></bean> <bean  id="userBean" class="org.example.UserBean"></bean> <bean id="userImplements" class="org.example.UserImplements"></bean>  <bean id="userImplements2" class="org.example.UserImplements2"></bean> <bean id="autowiredBean2" class="org.example.AutowiredBean"></bean> </beans>Copy the code
SRC/main/Java/org. Example/UserService Java -- -- -- -- -- - / manual injection * set method into * * * * 1. Attribute fields provide set methods * 2. Specify attributes in the XML configuration file via the property tag, a child tag of the bean tag * 3. When a component is added to the IOC, it is automatically injected into the IOC by calling a set */ /** * automatic injection ** @resource can be implemented automatically by using the reflection class * 1. The default is to find the corresponding (bean object) by (property field name) => (property name) and (bean tag ID) are the same * 2. If (attribute name) and (bean tag ID) are inconsistent, then * 3 will be looked up through (bean tag class). You may or may not provide a set * 4. You may annotate (on an attribute) or (on a set method) * 5. @resource (name="userBean") * 6.6.1 If the interface is injected with only one implementation class, instantiate normally * 6.2 If the interface has more than one implementation class, You need to specify the implementation class with the name parameter. The parameter is the id of the bean tag in the XML configuration file. The @autowired annotation pattern looks for the bean object using the class attribute of the bean tag in the XML configuration file, and has nothing to do with the property field * 2. You may or may not provide a set * 3. You may annotate (on an attribute) or (on a set method) * 4. @qualifier */ public class UserService {// manually instantiate UserBean UserBean = new UserBean(); // UserBean UserBean = new UserBean(); // JavaBean object @resource (name = "userBean") private userBean userBean; @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") @resource (name = "userImplements") Private UserInterface UserInterface; private UserInterface UserInterface; @Qualifier(value = "autowiredBean2") @Autowired AutowiredBean autowiredBean; public void test() { userBean.getName2(); userInterface.getName3(); autowiredBean.testAutowired(); }}Copy the code

Program source code

  • Program source code
  • Online access to address – song additions and deletes to check

data

  • Swagger Configuration tutorial juejin.cn/post/684490…
  • Swagger note 1 blog.csdn.net/ThinkWon/ar…
  • 2 zhuanlan.zhihu.com/p/49996147 Swagger annotations