Spring framework study notes/review outline

Day03-01 knowledge points


1. Push our project to the Gitee platform through Git in IDEA
  • Two ways (no warehouse-share, existing warehouse-push)
2. Frequently Asked Questions (FAQ) Analysis
  • The push process is rejected (the account is incorrect or the mail is a hidden account, the warehouse name does not follow the rules, and the remote warehouse has a readme when pushing)
  • What Git operations do we need before push (add, commit)
  • Can we only push the specified project module when pushing (do not commit the module that is not pushed)
  • Different projects can be placed in the same remote warehouse (not possible), and different projects with different local libraries will also correspond to different remote warehouses.
3. Clone the remote project to the local through Git
  1. Clone method (through the clone option in idea, through the Git Bash tool based on the git clone command)
  2. FAQ analysis
    2.1. How to run the cloned project? (jdk, maven, rebuild, out, mark as source root)
    2.2. Can the cloned project be assigned a new name? (Yes)
    2.3. Use git directly How to open the project cloned by the clone command? (Based on the file/open option in the idea menu)
4. Creation and structural analysis of SpringBoot project in Idea
  1. Project creation (Spring Initializr-Ultimate Edition, Spring Assistant-Community Edition,...)
  2. Project structure analysis (pom.xml,src/main/java,src/main/resources,src/test/java,...)
  3. Project startup process analysis? (thread/io->(Disk->load memory)->Class object->Read and Store Config-->BeanFactory-->Create Instance-->Store Instance)

Day03-02 Preliminary analysis of Spring Boot engineering business practice

1. Background analysis

In the Spring Boot project, how does the resource integration and application of our own Classes, and in what ways did he simplify them?

2. Preliminary business design
  1. Create a business class (e.g. DefaultCache)
  2. Hand over business classes to Spring for management (how Spring discovers these classes)
  3. Obtain objects from spring and perform application testing (based on unit test implementation)
FAQ?
  1. Does the created business class have requirements for packages? (It needs to be placed in the src/java/main directory, in the package or sub-package where the startup class is located)
  2. How do our business classes need to be described when they are handed over to spring for management? (Describe with the annotations that describe the beans defined in the spring framework)
  3. Why do we need to hand over the instance of the class to spring management? (Spring can give this bean object more scientific characteristics)
  4. What are the requirements for writing the unit test class in the Springboot project? (directory, package, annotation)
    4.1) Directory: src/test/java
    4.2) Package: The package or subpackage where the startup class is located (not the src/main/java directory)
    4.3 ) Comment: (@SpringBootTest,@Test-org.junit.jupiter.api.Test)
  5. How to understand the relationship of Has a in the project? (Dependency -> Contains -> There is one -> There is another type of attribute in the class)
  6. The relationship between the class and the class from which several methods can we start to understand? (is a,has a ,use a)
    6.1) is a (extends,implements)
    6.2) has a (has one)
    6.3) use a (use)
Bug?
  1. NullPointerException (NPE-Null Pointer Exception)
  2. UnsatisfiedDependencyException (dependency injection exception)
  3. NoSuchBeanDefinition (the corresponding Bean object was not found)
  4. ParameterResolutionException (parameter resolution exception-unit test method adds parameters)
3. Summary
  1. Through this business introductory application, we have mastered how to write our own classes in SpringBoot and handed over to Spring management and understood some problems and solutions that may occur during operation
  2. At the same time, awareness of the convenience brought by the springboot project to the realization of our business (I don't need to care about the dependencies myself, and do not do basic configuration) let us focus more on the realization of the business.

Day03-03 The core features of the Bean object in the Spring Boot project

1. Background information
  1. For computers, "computing" is one of the core problems it wants to solve? How to make calculations more "efficient" and "low-consumption"
  2. This is also a direct goal that our programmers should consider in the programming process. In the spring project, the objects are handed over to spring for management. The purpose is to let Spring give these objects more scientific characteristics, and then make the objects more efficient and effective in memory. Low-cost operation
2. What is the characteristic analysis of Bean in Spring?

Many scientific features are given to our Bean objects in the Spring framework, such as:

  1. Lazy loading (delayed object creation)-this feature reduces resource occupation problems (some large objects that are not used temporarily, consider using this feature)
  2. Scope (let the object be stored in the specified scope and then be reused-improve access performance)
  3. Life cycle method (the object can be initialized and resource destroyed before it is created and destroyed)

Based on these characteristics, our objects in the sping project can solve some business problems more efficiently.

3. What are the application practices of Bean characteristics in Spring?
  1. Create project module
  2. Construct an object pool object whose type is ObjectPool? (similar to string pool, integer pool, thread pool, connection pool,...)
  3. Test and analyze the object pool through the unit test class
FAQ?
  1. How to design the pool object to better enable the pool object to serve our business? (low consumption, high efficiency)
  2. What design pattern will be applied to the design of general pool objects? (Flyweight pattern-the design focuses on the reuse of objects)
  3. Does lazy loading in Spring mean that the class is temporarily not loaded into memory? (No, no instance of the class is created temporarily after the class is loaded into memory)
  4. When are instances of lazy loading objects created in Spring? (when used)
  5. What is the focus of the lazy loading feature in Spring to solve? (resource consumption problem)
  6. How to understand the scope of objects in Spring? (an application category problem of objects)
  7. Singleton scoped class, the instance of the same name has only one copy in memory (will be stored in spring's object pool) and can be reused.
  8. How does Singleton scoped classes support lazy loading? (Described with @Lazy annotation)
  9. Does Prototype scoped classes support lazy loading by default? (Support, the default is to create when used, no need to use @Lazy annotation description)
  10. Is the instance of the prototype scoped class created every time it is requested from the spring framework?
  11. Every object in the program has a life cycle, but it is not necessary to define a life cycle method? Correct

Day03-04 Dependency injection analysis of Bean in Spring Boot project

1. Background analysis
  1. In addition to solving the resource consumption of the object and the application performance of the object, the Spring framework should also consider the maintainability and scalability of the object.
  2. If you want to consider this feature, then how to implement it? In general programs, such a feature will be implemented by inheritance or composition, but inheritance or composition cannot be separated from coupling. How to reduce this coupling?
2. Dependency injection analysis in Spring project?
  • The Spring framework is a resource integration framework. In order to reduce the coupling between classes and improve the scalability of its programs, it is recommended that when coupling exists, it should be coupled to the interface or factory as much as possible (the bottom layer of the object creation in our project is now coupled. Spring's BeanFactory factory), and the specific value injection (DI) is completed by the spring framework.
3. Business design and implementation of dependency injection in Spring project
  1. Create project module
  2. Create business interface Cache. (com.cy.pj.common.cache.Cache)
  3. Create business interface implementation classes SoftCache, WeakCache.
  4. The unit test class CacheTest is defined and coupled to the Cache interface, and then spring completes the injection of the interface value.
FAQ?
  1. When the @Autowire annotation describes properties, what are the dependency injection rules?
    Answer:
    1.1. When the spring framework detects that there are properties in the beans managed by it that are described with @Autowired annotations, it will look up the corresponding property types from the spring container Bean object (this process is called dependency search-DL), if there is only one, it will be injected directly.
    1.2. If there are multiple, it will be matched based on the attribute name and the name of the bean object. If there is a match, it will be injected directly, and there is no match. An exception is thrown (dependency injection exception)
    1.3. Of course, we can also use the @Qualifier annotation with the @Autowired annotation to specify the name of the bean to be injected.

  2. What technology is Dependency Injection (DI) in Spring implemented based on?
    Answer: Reflection technology

  3. There is a design idea in the learning process of Spring, which is called IOC (Inversion of Control). How to realize the inversion of control?
    Answer: You can use DI

Bug analysis:
1.NoUniqueBeanDefinitionException (the definition of non-unique bean)


homework

Self-summarize
  1. Bean definition in SpringBoot project?
  2. What are the characteristics of the Bean object in the SpringBoot project? What benefits do these characteristics bring?
  3. How does the SpringBoot project implement dependency injection? What are the injection rules when the @Autowired annotation describes attributes?
  4. How does springboot project realize the integration and application of hikaricp connection pool.

Day04-01 Application of HikariCP connection pool in SpringBoot project


Reference notes

  1. Background analysis When
    we access the database, we need to establish a connection with the database through the TCP protocol. After the connection is used, the connection must be released. The
    TCP protocol is a connection-oriented protocol. The establishment of the connection requires three handshake, and the release of the connection requires four waves. This
    process is It is more time-consuming. If you frequently access the database, you will directly establish a connection with the data every time, which will bring great
    performance problems. How to solve such problems? The connection pool was born.

  2. What kind of connection pool?
    Connection pool is an application of pooling thought. It is implemented based on the Flyweight model , which is to open up an area in memory and store the created connections, so that these connections can be reused, thereby improving the access performance of the database. .

  3. The realization of the connection pool
    in Java ? The realization of all connection pools in java (c3p0, dbcp, druid, hikariCP,...) must comply with a design specification in java. This specification is javax.sql.DataSource. The connection pool is obtained through this specification The specific realization of, and then get the connection to realize the communication with the database.

FAQ?
  1. What do you need to establish a connection with the database in Java?
    Answer: database driver
  2. Does this database-driven design need to comply with any specifications?
    Answer: JDBC
  3. After we get a connection through the JDBC API, will the connection be returned to the pool after the application ends?
    Answer: Yes
  4. Are there any disadvantages of connection pool in application?
    Answer: It will bring a certain amount of memory overhead, exchange space for time
  5. If you were to design a connection pool now, what issues would you consider?
    5.1) Storage structure (array -> random access is more efficient, linked list-suitable for random insertion and deletion)
    5.2) Algorithm (FIFO, FILO,...)
    5.3) Thread safety (lock-lock granularity will affect performance and concurrency)
    5.4) The design of parameters (how many connections are there at most, when will the excess connections be released,...)
4. The entry implementation of HikariCP in the SpringBoot project

HikariCP is currently known as the best-performing connection pool in the world, and the default configuration implementation is also provided in the SpringBoot project. Analysis of specific application steps:

  1. Create project module
  2. Add project dependencies (mysql driver, spring data jdbc)
  3. Configure connection parameters (url, username, password,...)
  4. Write a unit test class to unit test the connection pool
FAQ?
  1. Who is the specific object pointed to by the DataSource variable in the program at runtime?
  2. What is the basic process of obtaining a connection based on the DataSource object in the program?
  3. Who is the specific connection pool object in the HikariCP connection product?
  4. Why is the running performance of the HikariCP connection pool good, and in what ways has it been optimized?
Bug
  1. SqlException (Access denied for user —>Access denied)
  2. CommunicationException (communication failure, check whether the database service is started)
  3. 'Url' attribute is not specified (URL is not configured or configured incorrectly)
  4. Unknow Host'dbgoods' (check whether there is a dbgoods database in the database)
4. Implement a JDBC service based on HikariCP connection pool in SpringBoot project
  1. Business description? (Query the product information from the database and make specific output in the test class)
  2. API design? (GoodsDao, DefaultGoodsDao, GoodsDaoTests)
  3. Timing access design? (GoodsDaoTest–>GoodsDao–>DefaultGoodsDao–>DataSource–>HikariDataSource->HikariPool->Connection–>JDBC)
  4. The specific implementation of the code? (GoodsDao, DefaultGoodsDao, GoodsDaoTests)
  5. The specific JDBC code implementation is as follows:
    5.1) Establish a connection (obtain a Connection object from the pool)
    5.2) Create a Statement (send SQL based on this object)
    5.3) Send SQL to perform query operations
    5.4) Process query results, store the results List<Map<String ,Object>>
    5.5) Release resources
FAQ?
  1. Where is the data element object DataSource obtained from?
    Answer: Value injection is performed by Spring
  2. If the value of the DataSource object is null, what is the reason?
    Answer: Is there any feature annotation description
  3. When the result set is mapped, who is the key in the map?
    Answer: field name
  4. When the Map object stores data, if the key is the same, the value will be overwritten?
    Answer: Yes
  5. Are the keys in the HashMap object in order?
    Answer: Unordered, the order of addition is not guaranteed
  6. Are there any rules when resources are released?
    Answer: Create first and then close
  7. Do you know what metadata is?
    Answer: The data that describes the data, for a table, the metadata is the field name
  8. Are you using a specification or a specification implementation when building a JDBC application? (
    Answer: Specification, Connection, Statement, ResultSet,...
Bug?
  1. SQLException? (SQL syntax problem)
  2. NullPointerException (Null Pointer Exception)

Day04-02 Application of MyBatis framework in SpringBoot project.

Reference notes

  1. MyBatis overview
  • It is an excellent persistence layer framework.Persistent
    layer: the layer of objects (DAO) responsible for storing objects in memory to the database.
  • The biggest advantage? It
    can quickly implement data operations in the database at low cost
    2.1) Open source (free-low cost)
    2.2) Simple (operation steps, parameter mapping, result mapping)
    2.4) Flexible (dynamic SQL-can better adapt to different Demand-for, if,...)
    2.5) Stable (no mistakes in three days)
  1. The application architecture of the MyBatis framework?
    1) DAO (a data access object written by yourself-this object needs to call the mybatis specification-SqlSession)
    2) MyBatis API (standard, implementation of the specification-encapsulates JDBC operations)
    3) JDBC API (specification- Java official definition)
    4) Driver (implementation of JDBC specification-database vendors provide its driver)

  2. Quick start of MyBatis application in SpringBoot project?
    1) Create project module
    2) Add project dependencies (mysql, spring data jdbc, mybatis)
    3) Project initial configuration (url, username, password, mapper-locations)
    4) Build unit test class, Test and analyze the basic environment of mybatis? (Get the sqlSession object in the test class, and get the connection based on this object)

FAQ?
  1. How to understand SqlSession?
    Answer: An entry object that implements a session with the database in mybatis
  2. Who is the specific implementation of the SqlSession specification during the test?
    Answer: SqlSessionTemplate
  3. Did we create the SqlSessionFactory object ourselves during the test?
    Answer: No, the springboot project is created at the bottom -out of the box
  4. When the SqlSession object gets a connection, where does the connection come from?
    Answer: The connection pool object associated with HikariDataSource

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/112733198