This chapter begins with spring, the last framework in SSH. Spring is an open source framework at the design level, which solves the problem of loose coupling between the business logic layer and other layers. Therefore, it puts the thought of interface oriented programming throughout the whole system application.

Let’s start with IOC, which has the benefit of reducing coupling, primarily by swapping dependencies between beans for associations as much as possible.

So let’s configure a Spring based on maven project.

You can start by downloading a spring plug-in: search for Spring in Eclipse ->Help->Eclipse Marketplace, and find Spring Tools to download.

Step 1: Import the Spring dependencies in the Maven project’s POM.xml: Spring-context. I use version 4.3.10 as an example


<! Spring framework</groupId> <artifactId> Spring-context </artifactId> < version > 4.3.10. RELEASE < / version > < / dependency >Copy the code


Step 2: Create an XML file named ApplicationContext.xml in the SRC /main/resources folder. Add the Spring header file to it as follows


<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 



xmlns:aop="http://www.springframework.org/schema/aop"



xmlns:tx="http://www.springframework.org/schema/tx" 



xmlns:context="http://www.springframework.org/schema/context"



xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">




 


</beans>
Copy the code

Interpretation of the label:

xmlns=XML Name space

XMLNS: Format file address for initializing beans

XMLNS: XSI: Auxiliary initialization bean

Xsi: Context: About the Spring context, including loading resource files

Constraints on the XMLNS: TX transaction tag

XMLNS: AOP Constraints on the AOP (aspect oriented) tag

Xsi :schemaLocation: Used for a schema document that declares the target namespace


Step 3: Create a new class, add the name, age attributes, encapsulate the construction, toString.

Step 4: Start configuring a bean to implement IOC. In applicationContext.xml, the configuration is as follows:

<bean id="person" class="com.entity.Person"></bean> //id is the name of the bean, class is the fully qualified path to configure the class // You can also use the name attribute instead of the ID attribute, the name attribute can use a special symbol to define the nameCopy the code

Step 5: Create another class and use the main method or junit @test to Test it. The Test code is as follows


BeanFactory Factory = new XmlBeanFactory(new ClassPathResource())"applicationContext.xml")); Person p = (Person) factory.getBean(Person)"person"); System.out.println(p); / / load a bean in time to create multiple objects ApplicationContext aContext = new ClassPathXmlApplicationContext ("applicationContext.xml"); Person p = (Person) acontext.getbean ("person");



        System.out.println(p);
Copy the code

This simple bean is configured.

We can add an attribute scope and init-method to the bean.

Scope attribute value:

Singleton (default)

Prototype A bean definition corresponds to an object instance in each Spring Ioc container. Request A bean definition corresponds to an instance in an HTTP request. That is, each HTTP request will have its own bean instance, created from a bean definition. Session In an HTTP session, one bean definition corresponds to one instance. This scope is valid only in the case of web-based Spring ApplicationContext. In a global HTTP session, one bean definition corresponds to one instance. Typically, this is valid only when the portlet context is used. This scope is valid only in the case of web-based Spring ApplicationContext.

The init-method property means which method is called when the bean is initialized. The value of the property is the name of the method in the bean.

Let’s look at the injection method:

First, add the property tag inside the bean tag. Name corresponds to the property name and value corresponds to the property value

1. For example, set default values for the properties of this object

<bean id="person" class="com.entity.Person"> <! -- Property Settings default value --> <property name="name" value="Ha ha"></property>



        <property name="age" value="11"></property>



</bean>
Copy the code

2. Inject a collection (set,list)

Add an lSIT <string> attribute to the Persion class, encapsulated

Injection:


<bean id="person" class="com.entity.Person"> //ls is the variable name of list<string> <property name="ls"> < list > < value > Google < value > / < value > haha < value > / < value > xi xi < value > / < / list > < / property > < / bean >Copy the code

3. Structural injection

// Construct injection requires the presence of a corresponding constructor in the entity class

<bean id="person" class="com.entity.Person">



    <constructor-arg name="name" value="1"></constructor-arg>



        <constructor-arg name="age" value="18"></constructor-arg>



</bean>
Copy the code

4. Inject objects

Create a new entity class card, add cid, Cname two attributes, encapsulation construction.

Add a bean to applicationContext.xml

<bean id="card" class="com.entity.Card"
>    </bean>Copy the code

Add persion object property to card class, encapsulation.

It is then injected into card’s bean

1

2

3

4

5

6

7

<bean id=”card” class=”com.entity.Card” >

//name Specifies the object variable name

<property name=”person”>

//ref specifies the label ID of the attribute class in the bean

<ref bean=”person” />// Idref element functions like <value> except that idref has more validation function <! — <idref bean=”person” /> –>

</property>

</bean>

I didn’t do that here. In addition to the above types of injection, you can also search for static factory method injection and instance factory method injection.