Summary of important knowledge points of Spring framework (1)

One, overview of spring framework

Spring solves the problem of loose coupling between the business logic layer and other layers, so it applies interface-oriented programming ideas throughout the entire system application. Spring is a lightweight Java development framework that emerged in 2003 and was created by Rod Johnson. Simply put, Spring is a hierarchical JavaSE/EE full-stack (one-stop) lightweight open source framework.

Two, the advantages of spring framework

1. Facilitate decoupling and simplify development; 2. Support aspect-oriented programming (AOP);
3. Support declarative transaction processing, only need to configure the transaction management without manual; 4. Convenience test. Spring supports Junit and can test spring programs through annotations; 5. It is convenient to integrate excellent frameworks; 6. The difficulty of using API is reduced;

Three, spring core technology

(1) What is IOC

IOC: Inversion of control, inverting the right of object creation to the spring framework is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes.

1. Getting started with IOC program

The first step: create the maven warehouse java project. The
second step: introduce the coordinates ①spring-context②commons-logging③log4j④junit
's four main jar packages:
①Spring Beans: The basic implementation of Spring IOC, including access to configuration files, creation and management of beans, etc., all applications are Used.
②Spring Context: Provides extended services on basic IOC functions. In addition, it also provides support for many enterprise-level services, such as mail service, task scheduling, JNDI positioning, EJB integration, remote access, caching, and support for multiple view layer frameworks.
③Spring Core: Spring's core toolkit, other packages depend on this package
④spring-expression-3.1.0.RELEASE.jar
Step 3: Import log4j files
Step 4: Use interface methods to create business layer and persistence layer
Step 5: And call in the test class (using the method of creating an object) and
then use the new method IOC:
1.1 First create applicatinContext.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">

    <!--IOC管理bean-->
    <bean id="userService" class="demo.serviceImpl" />

</beans>

1.2 Writing test classes
Steps: ①Create a spring factory; ②Get from the container; ③Call methods

@Test
    public void run(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Service service = (Service) ac.getBean("userService");
        service.save();
    

2. Summary of IOC technology

ApplicationContext interface, the interface of the factory, use this interface to obtain specific Bean objects. There are two specific implementation classes under this interface.
ClassPathXmlApplicationContext (load the spring configuration file under the class path)
FileSystemXmlAppLicationContext (load the spring configuration file under the local disk)

3. Bean management configuration file mode of Spring framework

id attribute, the name of Bean, the constraint of ID is used in the constraint, unique, value requirements: must start with a letter, can use letters, numbers, hyphens, underscores, sentences, colons id: no special characters can appear.
The class attribute, the full path of the Bean object.
The scope attribute, the scope attribute represents the scope of the Bean.
singleton singleton (default), the most commonly used method.
The prototype multiple
requests are used in Web projects, and each HTTP request will create a new Bean
session. In the Web project, the same HTTP Session shares a Bean
. The two properties of the Bean object are created and destroyed. Configuration
instructions: Spring initialized bean When the bean is destroyed or destroyed, some processing work is sometimes required, so spring can call the two life cycle methods
init-method of the bean when the bean is created and dismantled , and the init-method attribute is called when the bean is loaded into the container. Method
destroy-method, when the bean is deleted from the container, call the method specified by the destroy-method attribute

4. Three ways to instantiate Bean objects

The default is a parameterless construction method (default method, basically used)

<bean id="userService" class="demo.serviceImpl" />

Static factory instantiation method
Instance factory instantiation method

(2) What is DI dependency injection

DI: Dependency injection, when the Spring framework is responsible for creating Bean objects, it dynamically injects dependent objects into Bean components

1. The set method of the attribute injects the value

Write the attribute, provide the set method corresponding to the attribute, and write the configuration file to complete the injection of the attribute value

// 编写成员属性,一定需要提供该属性的set方法
private OrderDao orderDao;
// 一定需要提供该属性的set方法,IOC容器底层就通过属性的set方法方式注入值
public void setOrderDao(OrderDao orderDao) {
    this.orderDao = orderDao;
}
// 消息
private String msg;
// 年龄
private int age;
public void setMsg(String msg) {
    this.msg = msg;
}
public void setAge(int age) {
    this.age = age;
}

Configuration file

<bean id="os" class="cn.tx.service.OrderServiceImpl">
    <property name="orderDao" ref="od" />
    <property name="msg" value="你好" />
    <property name="age" value="30" />
</bean>

2. Attribute construction method injection value

For class member variables, constructor injection.

public class Car {

// 名称
private String cname;
// 金额
private Double money;

public Car(String cname, Double money) {
    this.cname = cname;
    this.money = money;
}

@Override
public String toString() {
    return "Car{" +
            "cname='" + cname + '\'' +
            ", money=" + money +
            '}';
}}

Configuration file

<bean id="car" class="cn.tx.demo2.Car">
    <constructor-arg name="cname" value="大奔" />
    <constructor-arg name="money" value="400000" />
 </bean>

3. Injection of array, set (list, set, map) Properties, etc.

// 数组
private String [] strs;
public void setStrs(String[] strs) {
    this.strs = strs;
}

private List<String> list;
public void setList(List<String> list) {
    this.list = list;
}

private Map<String,String> map;
public void setMap(Map<String, String> map) {
    this.map = map;
}

private Properties properties;
public void setProperties(Properties properties) {
    this.properties = properties;
}

Configuration file

<!--给集合属性注入值-->
<bean id="collectionBean" class="cn.tx.demo3.CollectionBean">
    <property name="strs">
        <array>
            <value>美美</value>
            <value>小凤</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>亮亮</value>
            <value>建云</value>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key="aaa" value="聪聪"/>
            <entry key="bbb" value="淞铭"/>
        </map>
    </property>
    <property name="properties">
        <props>
            <prop key="username">root</prop>
            <prop key="password">123456</prop>
        </props>
    </property>
</bean>

4. How to load multiple configuration files

Another configuration file is created in the src directory. Now it is the two core configuration files. There are two ways to load these two configuration files!
<import resource="applicationContext2.xml"/>
② Load multiple configuration files directly when the factory is created

Next section:
Link: Summary of important knowledge points of Spring framework (2) .

Guess you like

Origin blog.csdn.net/javaScript1997/article/details/107989954