Java EE SSM framework integrated development (two) Spring IoC and DI

Java EE SSM framework integrated development (two) Spring IoC and DI

Introduction

As mentioned above, Spring mainly manages various beans.

loC: Inverse of Control

  • The control of the objects that were originally created manually in the program is handed over to the Spring framework to manage.
  • Positive control
    : If you want to use an object, you need to be responsible for the creation of the object yourself
  • Counter-control
    : If you want to use an object, you only need to get the object you need to use from the Spring container, and you don t care about the creation process of the object, that is, the control of creating the object is reversed to the Spring framework

DI: Dependency Injection

  • Another term for IoC refers to the way that the Spring container initializes the Bean as dependency injection

Inversion of control is a way to generate or obtain a specific object through description (in Spring, it can be XML or annotation ) and through a third party. It is the IoC container that implements the inversion of control in Spring, and its implementation method is dependency injection. (The XML here is applicationContext.xml)

Spring IoC container

Note: How to write the configuration file in the SpringBean article

2.1 BeanFactory interface

BeanFactory
Provides complete IoC service support. It is a Bean management factory and is mainly responsible for initializing various Beans. When creating a BeanFactory instance, you need to provide the absolute path of the XML file.

The BeanFactory interface has multiple implementation classes, the most common being

org.springframework.beans.factory.xml.XmlBeanFactory
, Which assembles the Bean according to the definition in the XML configuration file.

public static void main (String[] args) { //Initialize the Spring container and load the configuration file BeanFactory beanFac = new XmlBeanFactory( new FileSystem Resource ( "D:\\eclipse-workspace\\ch1\\src\\applicationContext.xml" ) ) ; //Get test instance through the container TestDaott=(TestDao)beanFac.getBean( "test" ); tt.sayHello(); } Copy code

2.2 ApplicationContext interface

ApplicationContext
It is a sub-interface of BeanFactory, also known as application context, by
org.springframework.context.ApplicationContext
Interface definition. In addition to all the functions of the BeanFactory, the ApplicationContext interface also adds support for internationalization, resource access, and event propagation.

There are usually three ways to create an ApplicationContext instance:

1. Created by ClassPathXmlApplicationContext

  • ClassPath classPath directory (src root directory) to find the specified XML configuration file (classPath:WEB-INF/classes/)
public static void main (String[] args) { //Initialize Spring container ApplicationContext, load configuration file ApplicationContext appCon = new ClassPathXmlApplicationContext( "applicationContext.xml" ); //Get test instance TestDao through the container tt=(TestDao)appCon.getBean ( "test" ); tt.sayHello(); } Copy code

2. Created by FileSystemXmlApplicationContext

  • Absolute path to find XML configuration file
public static void main (String[] args) { //Initialize the Spring container ApplicationContext, load the configuration file ApplicationContext appCon = new FileSystemXmlApplicationContext ( "D:\\eclipse-workspace\\ch1\\src\\applicationContext.xml" ); //Get the test instance TestDao through the container tt=(TestDao)appCon.getBean( "test" ); tt.sayHello(); } Copy code

3. Create an ApplicationContext container by instantiating the Web server

2.3 Difference

If a certain attribute of the Bean is not injected:

  • After BeanFacotry is loaded, an exception will be thrown when the getBean() method is called for the first time.
  • ApplicationContext self-checks during initialization, which is helpful to check whether the dependent properties are injected.

Therefore, in real xt, BeanFactory is used only when system resources are low.

Property value injection method

Create a new Maven project and import the necessary package pom.xml

< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 </modelVersion > < groupId > www.xaut. edu.cn.software </groupId > < artifactId > testmaven1 </artifactId > < version > 0.0.1-SNAPSHOT </version> < packaging > war </packaging > < dependencies > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-aop </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > <artifactId> groupId spring-aspects </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-beans </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < > org.springframework </groupId > < artifactId > spring-context </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-core </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-expression </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-jdbc </artifactId > <version >artifactId5.0.2.RELEASE </version > </dependency > < dependency > < groupId > org.springframework </groupId > < artifactId > spring-tx </artifactId > < version > 5.0.2.RELEASE </version > </dependency > < dependency > < groupId > commons-logging </groupId > < >commons-logging </artifactId > < version > 1.2 </version > </dependency > < dependency > < groupId > commons-logging </groupId > < artifactId > commons-logging </artifactId > < version > 1.2 </version > </dependency > </dependencies > </project > copy code

3.1 Construction method injection

use

constructor-arg
label

Tmp.java

//In order to show that the members are other classes, use ref to specify the member attribute package com.dao in the xml ; public class Tmp { //Construction method for dependency injection public Tmp () {} void work () { System.out.println( "Tmp.work()" ); } } Copy code

TestDao.java

package com.dao; public interface TestDao { public void work () ; } Copy code

TestDaoImpl.java

package com.dao; public class TestDaoImpl implements TestDao { int a; Tmp b; //Construction method, used to implement dependency injection public TestDaoImpl ( int a,Tmp b) { super (); this .a = a; this .b = b; } @Override public void work () { System.out.println( "a = " +a); b.work(); } } Copy code

application.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 https://www.springframework.org/schema/beans/spring-beans.xsd" > < bean id = "tmp" class = "com.dao.Tmp" > </bean > < bean id = "test" class = "com.dao.TestDaoImpl" > < constructor-arg index = "0" value = " . 1 "/> < constructor Arg- index = ". 1 " REF = " tmp "/> </the bean > </Beans > copy the code

Test.java

package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dao.TestDao; public class Test { public static void main (String args[]) { @SuppressWarnings("resource") ApplicationContext appCon = new ClassPathXmlApplicationContext( "applicationContext.xml" ); TestDao tmp = (TestDao)appCon.getBean( "test" ); tmp.work(); } } Copy code

3.2 Property setter method injection

After calling the parameterless constructor or the parameterless static factory method to instantiate the bean, call the setter method of the bean.

use

property
label

Other files are the same as above) TestDaoImpl.java

package com.dao; public class TestDaoImpl implements TestDao { private int a; private Tmp b; public void setA ( int a) { this .a = a; } public void setB (Tmp b) { this .b = b; } @Override public void work () { System.out.println( "a = " +a); b.work(); } } Copy code

application.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 https://www.springframework.org/schema/beans/spring-beans.xsd" > < bean id = "tmp" class = "com.dao.Tmp" > </bean > < bean id = "test" class = "com.dao.TestDaoImpl" > < property name = "a" value = "1"/> < Property name = "B" REF = "tmp"/> </the bean > </Beans > copy the code