Spring entry to see this one enough!!! Not enough to find me!

Today, let's have some good guys. There are some java basics in front of us,

How to say also need to have some frame right, don't say much, Huang sir takes you to open dry!

It's over

 

Spring framework

1, Spring Brief

  • Why learn spring?
  • Advantages of traditional projects: easy to build
  • Disadvantages of traditional projects:
  1. The service and dao objects are manually generated by the programmer. (not considering when to destroy or multithreading)
  2. Adding the same code to multiple methods can be cumbersome. (500 methods, adding a console presentation statement requires 500 modifications)

 

Purpose: for

1. Manage objects in projects more professionally

2. For the convenience and rapidity of function expansion in the future

 

  • What is Spring?

Spring framework is the most widely used framework for enterprises, and none of them. Spring is a one-stop framework, which is called a one-stop framework because spring can integrate other frameworks.

 

  • Spring:
  • Spring IoC: inversion of control technology. (one of the two characteristics)

Object factory and dependency injection;

Used to manage the creation and destruction of objects and inject instance objects into variables

  • Spring AOP: aspect oriented programming technology, which lays the foundation for spring transaction management.

Without modifying the original code, the new code can be cut into the execution of the original code

(reflection technology) (two features 2)

  • Spring Transaction management: Spring Transaction management.

Programmers themselves:

try{

//Commit transaction

}catch(Exception e){

//Rollback transaction

}


You only need to judge whether you need to throw an exception:

Throw an exception to spring, which will help you roll back automatically

No exception was thrown to spring. Spring automatically submits the transaction for you

Throw  new RuntimeException()

 

 

2, Differences between traditional projects and spring projects

  • Traditional projects:

Advantages: easy to build environment

Disadvantages:

1. service, dao and JavaBean are created by programmers themselves (when to destroy and multithread)

2. Function extension is particularly difficult (500 methods, add the same 2 lines of code, modify 500 times)

3. Integration between frameworks is particularly troublesome

 

Spring project:

advantage:

1. Almost all objects are spring managed (regardless of creation, destruction, multithreading) IOC

2. The management of database transactions is very easy (just judge whether to throw exceptions to spring)

3. Function extension is very easy (500 methods, add the same 2 lines of code, modify once) AOP

4. Integration between frameworks requires only one configuration file

Disadvantages: 1. Environment construction is very troublesome

 

 

3, Build environment spring foundation (no MyBatis)

 

 

4, Entry case

1. Brief introduction of IOC

IOC: reverse control. The object management authority (creation, destruction) is reversed by the programmer to the spring container.

 

2. Spring project writing

1. Write profile class (configuration information)

SpringConfiguration:

/**
 * spring Core profile
 * Configuration Note: mark the current Java class as a spring core configuration file class
 * ComponentScan Annotation: scan packages that spring needs to manage
 *                    basePackages:Tell spring about the packages that spring needs to manage and scan
 */
@Configuration
@ComponentScan(basePackages = {"com.czxy.demo1"})
public class SpringConfiguration {
}

 

2. Register components for spring

Demo1Dao:

/**
 * Component:Components. Mark current class, managed by spring
 */
@Component("demo1Dao")
public class Demo1Dao {
    public void insert(){
        System.out.println("demo1Dao-insert");
    }
}

 

Demo1Service

@Component("demo1Service")
public class Demo1Service {
    private Demo1Dao demo1Dao;
    public void insert(){
        System.out.println("demo1Service-insert");
        demo1Dao.insert();
    }
}

 

3. Component injection (tell spring to create the object and assign it to whom)

Demo1Service

@Component("demo1Service")
public class Demo1Service {
    @Resource(name="demo1Dao")
    private Demo1Dao demo1Dao;
    public void insert(){
        System.out.println("demo1Service-insert");
        demo1Dao.insert();
    }
}

 

Demo1Test

public class Demo1Test {
    @Resource(name="demo1Service")
    private Demo1Service demo1Service;

    @Test
    public void run1(){
        demo1Service.insert();
    }
}

 

4. Load the spring configuration file for spring running test

Demo1Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class Demo1Test {
    @Resource(name="demo1Service")
    private Demo1Service demo1Service;

    @Test
    public void run1(){
        demo1Service.insert();
    }
}

 

 

 

3. Introduction process

 

 

To be continued!!!

Of course, there are more nice ones in the back. The next one is more for you!!!

Remember to pay attention. Don't miss it!

 

Please give yourself a compliment!

Make a little progress every day`~~~~~

Keywords: Programming Spring Java Database

Added by Rob2005 on Sat, 25 Apr 2020 19:20:44 +0300