[Spring] Introduction to Spring Summary

table of Contents

Preface

1. What is spring?

Two, spring architecture

Three spring configuration files

1. The basic configuration of the Bean file

2. Three basic ways spring creates objects

Three Bean dependency injection

Two ways to achieve dependency injection:

Bean dependency injection data type



Preface

        I have studied spring these few days, and the learning process feels particularly vague. It is time to start to summarize and organize. Start from the first day of learning. After learning, the advantages of spring, spring related APIs, spring architecture and Some basic configurations of spring, etc.


1. What is spring?

        Spring takes IOC (Control Overturn) and AOP (Aspect Oriented Programming ) as the core, and provides a full-stack (full stack ) of many enterprise-level application technologies such as the presentation layer (SpringMVC), the persistence layer (Spring JDBC), and business layer transaction management . ) Lightweight java open source framework .

       Advantages :

  • Convenient decoupling and simplified development : (Through spring's IOC container, the dependencies between objects are handed over to spring for control, avoiding the coupling between programs caused by the coding process,)
  • AOP programming support : (Spring provides AOP functions to facilitate aspect-oriented programming. Many functions that are not easy to implement with traditional OOP can be easily implemented through AOP)
  • Support for declarative transactions : ( flexibly manage transactions through declarative methods to provide development efficiency and quality)
  • Convenient for program testing : (In spring, you can use spring's support for Junit4 to facilitate program testing operations through annotations)
  • Facilitate the integration of various excellent frameworks : (spring not only does not exclude various excellent open source frameworks, but can reduce the difficulty of using various frameworks and provide support for the use of various frameworks)
  • Reduced the use of JavaEE API : (Spring provides a simple encapsulation layer for many difficult APIs, and simple encapsulation through spring greatly reduces the difficulty of use.)

Two, spring architecture

                   

Three spring configuration files

1. The basic configuration of the Bean file

basic configuration:

  • id: unique identifier
  • class: fully qualified class name

Range configuration:

  • singleton: default value (singleton) ---> the created instance is in singleton mode, IOC is created once, and then always exists
  • Prototype: Multi-case--->The created instance is a multi-case model. Every time we get a bean, IOC will recreate a new object for us

                   

2. Three basic ways spring creates objects

1. Instantiation of no-parameter construction:

  • Create UserDaoImpl
package com.james.dao.impl;
import com.james.dao.UserDao;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
/*实现Userdao接口(save()方法)*/
public class UserDaoImpl implements UserDao{
    public UserDaoImpl(){
        System.out.println("调用了无参的构造方法");
    }
    public void save() {
        System.out.println("执行了UserDaoImplements的save方法");
    }
}
  • Create UserServiceImpl
package com.james.service.impl;
import com.james.dao.UserDao;
public class UserServiceImpl implements com.itheima.service.UserService {
    public void save() {
    }
}
  • Create test class
package com.james;
import com.james.dao.UserDao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    @Test
    public void save(){
        //从spring的IOC中获取userDao对象
        ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao=(UserDao)app.getBean("userDao");
        userDao.save();
    }
}
  • Write applicationContext.XML configuration file
<?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.itheima.dao.impl.UserDaoImpl"></bean>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"></bean>
</beans>

Execution result: (The spring container has successfully instantiated the Bean object)

:

2. Static factory method instantiation: (understand)

    This method provides a static factory to instantiate objects, the method is as follows:

  • Customize a factory class
package com.james.factory;
import com.james.dao.UserDao;
import com.james.dao.impl.UserDaoImpl;
public class StaticFactory {
    public static UserDao createUserDao(){
        return new UserDaoImpl();
    }
}
  • Configuration file

         

In the above code : the corresponding UserDaoImplement implementation class specifies its corresponding factory class StaticFactory, and the attribute of factory-method informs the spring container to call the createUserDao method of the factory class to obtain an instance of UserDaoImpl .

3. Instantiation of factory common methods (understand)

  • Custom factory class
package com.james.factory;
import com.james.dao.UserDao;
import com.james.dao.impl.UserDaoImpl;
public class NormalFactory {
    public  UserDao createUserDao(){
        return new UserDaoImpl();
    }
}
  • Configuration file

                       

In the above code: first configure an instance factory bean, and then configure the bean that needs to be instantiated.In the bean with the id userDao, use the factory-bean attribute to specify an instance factory, and the attribute value corresponds to the id attribute value of the instance factory, using factory The -method attribute determines the createBean method using the factory.

Three Bean dependency injection

        Dependency injection is a decoupling strategy in the Spring framework, called DI or IOC (Inversion of Control). There are mainly set methods and constractor (construction methods) methods, so that classes are organized in the form of configuration files. .

        For example, if classB is used in classA, if the code is written, it is new and one classB, and the way of dependency injection is to write two in applicationContext.xml

<bean id="id1" class="A"><property name="B" ref="id2"></bean>
<bean id="id2" class="B"></bean>

That is, there is no need to write in the place where new is needed in class A.

Two ways to achieve dependency injection:

Construction method:

set method:

Bean dependency injection data type

1) Common data types

2) Reference data type

The first two have been reflected in the construction method and set method of dependency injection.

3) Collection data type (array type, list collection, set collection, double column collection)

package com.james.Service.impl;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Data
public class UserService {
    private String[] hobbies;
    private List<Object> games;
    private Set<Object> names;
    private Map<String, Object> map;
    private Properties prop;

    public void setProp(Properties prop) {
        this.prop = prop;
    }
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
    public void setGames(List<Object> games) {
        this.games = games;
    }
    public void setNames(Set<Object> names) {
        this.names = names;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}

Configuration file:

<?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">
    <beans>
        <bean id="userService" class="com.itheima.Service.impl.UserServiceImpl">
            <!--bean类型的依赖注入之数组类型-->
            <property name="games">
                <list>
                    <value>LOL</value>
                    <value>王者荣耀</value>
                </list>
            </property>
            <!--bean类型的依赖注入值list类型-->
            <property name="hobbies">
                <array>
                    <value>抽烟</value>
                    <value>喝酒</value>
                    <value>烫头</value>
                </array>
            </property>
            <!--bean类型的依赖注入之set类型-->
            <property name="names">
                <set>
                    <value>James</value>
                    <value>Danny</value>
                </set>
            </property>
            <!--bean类型的依赖注入之map类型-->
            <property name="map">
                <map>
                    <entry key="one" value="破鞋"/>
                    <entry key="two" value="一匹马"/>
                </map>
            </property>

            <property name="prop">
                <props>
                    <prop key="a">A</prop>
                    <prop key="b">B</prop>
                </props>
            </property>
        </bean>
    </beans>

</beans>

 

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/109158834