watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Spring As an enterprise Java The most popular application development framework , Has been used by millions of developers around the world ,Spring Framework is used to create high performance , Easy to test , Reusable code . It's an open source Java platform , It was originally created by Rod Johnson Written and published in 2003 year 6 Month in Apache2.0 For the first time under license . Because it's lightweight , When it comes to size and transparency .spring The basic version of the framework is about 2MB. The core function can be in any Java Used in applications , But there are also extensions Java EE Build on platform Web Applications .Spring The goal of the framework , In order to make J2EE Development is easier to use , And by making it based on POJO Programming model , Promote good programming habits . This article will show you about Spring Basic use of framework , It is convenient to better understand its principle in work .

1:Spring What is the role in development ? 

Spring The main role of is to integrate the framework of the third party 、 by Service Layer provides transactions

2: Conventional MVC Development model

     M:Model  (pojo+Service+DAO)

 

     V:View    (jsp、HTML)

 

     C:Controll  (Servlet、SpringMVC、struts)

 

     In traditional development : We are Servlet You need to call Service、 stay Service You need to call DAO

     We can think indirectly that  Servlet---->Service----->DAO

 

     In our development Service The creation of 、DAO The creation of 、 It is the programmer who directly or indirectly creates objects

 

     Our object creation is done by programmers ? Dependencies are managed by programmers ?

 

     Can we leave the creation of our objects to a third party ? Can our dependence be maintained by a third party ?

 

    Spring The emergence of the right solution to this problem .......

3:Spring What is it? ?

     Spring It's a technology related to the creation of management objects and the dependency relationship between management objects

      That is to say, it uses Spring after Our objects can be created by Spring To complete 、 Dependencies between objects can also be created by Spring For maintenance  

  

4:Spring Can do ?

      4.1:Spring Managing dependencies between objects

 

      4.2: by Service Layer provides transactions

 

      4.3: Integrate the third party framework

 

      4.4: Provides DAO Layer solution

 

      4.5: Provides the controller solution

 

      4.6: Provide for the right to Web Support for

 

      4.7: Provides context support

 

5:Spring What are our family products

     Spring     SpringMVC    Spring Security   Spring data   Spring Boot    Spring Cloud

 

6:Spring Seven modules in

     Spring Core: This module is Spring Core module   The main function is IOC/DI   What is provided is Spring The core container of IOC

 

     Spring AOP: The main function of this module is to provide aspect oriented programming

 

     Spring ORM: The main function of this package is Provides Integrating third parties ORM The package of the frame

 

     Spring Web: The main function of this package is Provide for the right to Web Support for

 

     Spring WebMVC:SpringMVC  (SpringMVC It's not a framework, it's Spring It's just a module in )

 

     SpringDAO: The main function of this module is Provides access to the database

 

     Spring Context: authentication 、 mail 、EJB、UI

7:Spring One of the first HelloWorld Program

7.1: Guide pack

    The main import is Spring The core function package of Spring Core

7.2: Write entity class

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

7.3: Writing configuration files

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

7.4: test

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

8:Spring Medium IOC Use ( The configuration file )

8.1: No parameter object creation, configuration file writing

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 8.2: Creation of objects with parameters

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

  8.3: How to play the configuration file

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

8.4: The creation of factory class

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

8.5: Non static method creation in factory JAVA object

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

8.6: Static method creation in factory JAVA object

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Sum up :IOC

   IOC: Inversion of control

    Who controls who ? 

    Spring Of IOC Containers control the creation of objects

     Something's reversed ?

     Before, we needed to create our own JAVA object   Now? The right to create objects is reversed 、 The right to create objects is given to Spring To complete

9:Spring Medium DI

   DI: Dependency injection

    Who depends on whom ?

    Controll Depend on Service   Service Depend on DAO

    Something has been injected ?

    1: Inject specific objects     2: Injected the actual value

 

9.1:set Inject ( It's probably the most used way in the past )

9.1.1: Add... To the class that needs to inject objects set Method

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.1.2: Writing configuration files

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.2:P Label Injection

Premise : There must be... In the constraint :xmlns:p="http://www.springframework.org/schema/p"

          9.2.1: It has to be written where the object needs to be injected set Method

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.2.2: Injection in the configuration file

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.3: Inside bean For injection

     9.3.1: Declare... In a class set Method

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.3.2: Writing configuration files

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

9.4: Automatic assembly

   9.4.1: Global automatic assembly

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

    9.4.2: Local automatic assembly

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Be careful : If automatic assembly is realized by type, then the injected object is in IOC In the container , There has to be only one instance 、 If there are multiple instances, it will result in I don't know about that object ( Report errors )、 When assembling by name So as long as the names of the objects are consistent, no error will be reported 、 No matter what kind of injection is, then set All the methods have to be written

10: Get three forms of objects

 10.1: Get by type

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 10.2: Get by name

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 10.3: adopt id To get

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

11: Play with notes IOC and DI

  Remember the following notes

 

@Component: This means to put the instantiated object of the current class into IOC Inside the container

@Service : Semantically Component Commonly used in Service above The meaning is the same as above

@Repository: Semantically Component Commonly used in DAO above   The meaning is the same as above

@Scope: This is used to set whether the current class is singleton or multi instance

@PostConstruct: This means : The current class object is placed in IOC The initialization method executed when the container is opened

@PreDestroy: This means that the object is in IOC The method to be executed when destroying in the container ( Recycling )

@Autowired: This means by type (type) Automatic injection

@Resource: This means injecting by name

 

Be careful : Use... On the class @Component、@Service、@Repository When The default object name is the lowercase initial of the current class name  

 

@Component、@Service、@Repository The three annotations have the same meaning 、 In actual development Service Layers generally use @Service、DAO Layers generally use @Repository、 If there are other objects, we need to make IOC Container to achieve automatic management, then In general use @Component

 

If you can't remember annotations in development You can use them all @Component To put objects in IOC Containers And let Spring To manage

 

12:AOP Use

  AOP: Tangent oriented programming

   What is aspect oriented programming ? It's the process of software development The duplicate code encountered is extracted to form a special class This class is called aspect class , In the normal operation of the software When we need to use our extracted duplicate code Through proxy design patterns The idea of dynamically implanting our repetitive code It's called aspect oriented programming

 

   AOP The greatest use of programming : Do risk control 、 User behavior records 、 User behavior logs It's usually made into a watch

     System operation log : What we inhale into this log file is

12.1: The use of traditional models

12.1.1: Write our DAO class

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.1.2: Write business logic classes

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.1.3: Write cut class

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.1.4: test

12.2: It uses cglib Agent to achieve aop Use

12.2.1: To write UserDAO Class ( ditto )

12.2.2: To write UserService Class

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.2.3: Write a proxy class (UserServiceProxy)

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 

12.2.4: test

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.3:AOP The use of annotation mode in

Common notes :

@Component          // First step : take AOP Class into IOC Containers

@Aspect             // Indicates that the current class is a facet class

@Scope(value="singleton")   // Section classes are generally singletons in classes

@Pointcut(value="execution (* com.qf.aop1.UserService.*(..))")          // The expression written in this is called Pointcut expression

@Around(value="pt()")       // Surround means   Execute before and after method execution

@AfterReturning(value="pt()")    // Execute after method execution is complete

@AfterThrowing(value="pt()")    // Method is executed when there is an exception

@Before(value="pt()")        // Code executed before method execution at the location of the pointcut expression   hinder value It means to execute this method somewhere

@After(value = "pt()")        // Method is executed after execution

 

 12.3.1:AOP Use of profile

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

12.4:AOP The use of configuration mode

   12.4.1: Get rid of all the comments

   12.4.2: Writing configuration files

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

   12.4.3: test ( ditto )

13:Spring in DAO Use of modules (JdbcTemplate Use )

Spring In this DAO The main function of the module is : Is to provide the function of database access 、 Be similar to dbutils

nothing but Spring This framework provides a function of accessing database ( Basically not )

 

13.1: Guide pack

     C3P0 My bag

     mysql Driver package

     Spring The core function package of

     Spring Of DAO My bag

13.2: Writing configuration files

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

13.3: stay DAO Use in jdbcTemplate Complete the operation of the database

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

13.4: test ( ditto )


14:  Business

14.1: Transactions are implemented in a configured manner

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=


test : See the code for details ( Be careful : You can see it by setting exceptions Whether the transaction works )
 

14.2: Transactions are implemented through annotation patterns

14.2.1: Preparation of configuration file

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

14.2.2: stay Service Configure the use of transactions in

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

14.2.3: Use of tests

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 

15: matters needing attention

     15.1: In a production environment, transactions cannot be added to DAO Layer of 、 All transactions are added to Service Layer of 、 Because whether a business is successful or not It indicates whether the current operation is successful

     15.2: If the transaction is not rolled back in development   Then you manually throw a runtime exception So the transaction rolls back

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

If you think that asamura's article will help you , Please search and follow on wechat 「 Shallow feathered IT hut 」 WeChat official account , I'll share my computer information knowledge here 、 Theoretical Technology 、 Tool resources 、 The software is introduced 、 The backend development 、 interview 、 A series of articles, such as thoughts on work and some thoughts on life . What you see and what you get , It's all about life . take your time , Work harder , You and I grew up together ...

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

I set up a technology group , If you want to know more about IT The technology of the industry and the problems in life , Welcome to join the group , Just add my wechat , Note that you can enter the group , We look forward to your participation .

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Previous recommendation

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Java What we need to pay attention to in generics


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

How to be in Java Create elegant objects to improve program performance


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

More pictures , Understand 8 A common data structure


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

dried food !MySQL Optimization principle analysis and optimization scheme summary


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

hardcore !15 Zhang diagram Redis Why so soon?


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

We media people's essential multi platform synchronization 、 More than one article


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Programmer essential technology website Collection


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Things about image processing


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Shallow feather

Message area

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

Point a praise , Prove that you still love me