Spring quick start learning (1)

Table of contents

1.What is Spring (understanding)

 Spring’s organizational structure:

The advantages of Spring are summarized as follows:

Spring ideas are proposed as follows:

The relationship between BeanFactory and ApplicationContext:

Three commonly used ApplicationContexts:

2. Detailed explanation of SpringBean configuration

Common configurations of Bean:

Bean scope configuration:

Lazy loading of beans:

3.Code

3.1 Getting Started

3.2Bean dependency injection method


 

 

 

1.What is Spring (understanding)

Official website address: Spring | Home

 Spring’s organizational structure:

51c585c3cd42426eb46c127ad5acabe2.png

 

 

The advantages of Spring are summarized as follows:

 

1. Low-intrusive design, code pollution is extremely low.

2. Independent of various application servers, applications based on the Spring framework can truly realize the promise of Write Once and Run Anywhere.

3. Spring's IoC container reduces the complexity of business object replacement and improves decoupling between components.

4. Spring's AOP support allows centralized management of some common tasks such as security, transactions, logs, etc., thus providing better reuse.

5. Spring's ORM and DAO provide good integration with third-party persistence layer frameworks and simplify underlying database access.

6. Spring’s high degree of openness does not force applications to rely entirely on Spring. Developers are free to choose part or all of the Spring framework.

 

Spring ideas are proposed as follows:

 

 

257911f4eccd4ccf905a0c5878766454.png

 

 

The relationship between BeanFactory and ApplicationContext :

 

1) BeanFactory is Spring’s early interface, called Spring’s Bean factory, and ApplicationContext is a later, more advanced interface, called Spring container;

2) ApplicationContext expands its functions based on BeanFactory, such as: monitoring function, internationalization function, etc. BeanFactory's API is more low-level, and ApplicationContext's API is mostly an encapsulation of these low-level APIs;

3) The main logic and functions of Bean creation are encapsulated in BeanFactory. ApplicationContext not only inherits BeanFactory, but also maintains references to BeanFactory inside ApplicationContext. Therefore, ApplicationContext and BeanFactory have both an inheritance relationship and a fusion relationship.

4) The initialization timing of Bean is different. The original BeanFactory only creates the Bean (extended loading) when getBean is called for the first time, while the ApplicationContext loads the configuration file and instantiates and initializes the Bean as soon as the container is created.

 

Three commonly used ApplicationContexts :

Only in the Spring basic environment, the three commonly used ApplicationContext functions are as follows:

8358f131b16547b987d093ce0cf6cf23.png

 

2. Detailed explanation of SpringBean configuration

 

Common configurations of Bean:

 

46fe9a138ecf478f836ea72d7f4a93b7.png

 

 

Bean scope configuration:

 

Ranges

Description

singleton

Default value, singleton

prototype

Multiple instances

request

In the WEB project, Spring creates a Bean object and stores the object in the request field.

session

In the WEB project, Spring creates a Bean object and stores the object in the session domain.

global session

In WEB projects, it is applied in the Portlet environment. If there is no Portlet environment, the globalSession is equivalent to the session.

 

Lazy loading of beans:

When lazy-init is set to true, it is lazy loading. That is, when the Spring container is created, the Bean instance will not be created immediately. When it is used, the Bean instance will be created and stored in the singleton pool, and the Bean will be used later. Just get it directly from the singleton pool. Essentially, the Bean is still a singleton.

<bean id="userDao" class="com.gz.dao.UserDaoImpl" lazy-init="true"/>

3.Code

 

3.1 Getting Started

  1. Create a maven project and import related dependencies
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.8</version>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>
  1. Build a package and write the UserDao and UserDaoImpl related classes under the package.

 

UserDao:

public interface UserDao {
    public void save();
}

 

UserDaoImpl:

 

public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("UserDao save method running....");
    }
}

 

Test:

 

public class UserDaoTest {
    @Test
    //法1:普通法
    public void test1() {
        UserDao userDao = new UserDaoImpl();
        userDao.save();
    }

    @Test
    public void test2() {
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //法2:
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.save();
        //法3:
        UserDao userDao2 = applicationContext.getBean("userDao", UserDao.class);
        userDao2.save();

    }

}

 

applicationContext.xml:

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.gz.core.dao.UserDaoImpl" ></bean>

</beans>

The above is the basic introduction.

3.2Bean dependency injection method

  Dependency injection methods include: set method and construction method. How to use them is as shown in the following code.

Based on the above UserDao UserDaoImpl, create the following

UserService:

 

public interface UserService {
    void save();
}

 

UserServiceImpl: There are 2 ways to write it

Set

public class UserServiceImpl implements UserService {

    private UserDao userDao;

@Override
    public void save() {
        userDao.save();
    System.out.println("-------- save();--------");
    }
    //构造set
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
        System.out.println("------setUserDao--------");
    }
}

 

 

Constructor

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    //构造方法

    //没有调用无参
    public UserServiceImpl() {
        System.out.println("-------UserServiceImpl----");
    }

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
        System.out.println("-------UserServiceImpl(UserDao userDao)------------");
    }
@Override
    public void save() {
        userDao.save();
    System.out.println("-------- save();--------");
    }

}

The corresponding configuration files applicationContext.xml :

Set:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.gz.core.dao.UserDaoImpl" ></bean>

<bean id="userService" class="com.gz.core.service.UserServiceImpl">
         <!-- id配置的是beanname,没有配id,可用name的别名首个单词,没有name,可用class-->

          <property name="userDao" ref="userDao"/>

         <!-- ref为引用,引用dao中的id(id可以任意起)

          .一定注意这里的name是set方法的名称而不是属性名,
          spring是根据这个name做了首字母大写和拼接set字符串而找到的set方法
          ,并通过反射调用该方法对属性赋值, 再说一遍这不是属性名-->
      </bean>

</beans>

 

Constructor:


 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.gz.core.dao.UserDaoImpl" ></bean>

  <bean id="userService" class="com.gz.core.service.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"/>
    </bean>

</beans>



 

 

Guess you like

Origin blog.csdn.net/weixin_52733693/article/details/130318137