directory

What is the SSM framework?

1, Spring

2, for SpringMVC

3, MyBatis

Two, SpringIOC container details

1. What is SpringIOC?

IOC(IOC of Control)

(2) DI: Dependency Injection.

Instantiation of the SpringIOC container

Add beans to the IOC container

Get the Bean properties from the IOC container

1. Get by id of bean property

2. Fetch by bean type

3. Get parameters by class and attribute names

Methods for assigning attributes to beans in IOC

1. Construct with no arguments and specify attribute assignment

2. Assign by taking arguments and specifying attributes

3. Assign without specifying an attribute through a parameter construct

4. Specify the position of the parameter through the index value

5. Distinguish overloaded constructors by type

6. Assign using the P namespace

Write at the end

This is the 9th day of my participation in Gwen Challenge.

Hello, I’m Grey Ape! A super bug writing program ape!

SSM framework believe that many Java learning partners have been very familiar with, as the mainstream framework of modern enterprise application development, but now the SSM framework has been considered by bigs as the entry level teaching of Java development, not point SSM technology, are afraid to go out to find a job.

So today, I would like to share with you what SSM framework is, and the basic introduction of the first S-Spring framework of SSM framework, and will continue to share with you in the following articles. Lay the foundation for the future of Java bull! Article content is more, it is recommended to collect slowly learning, behind more wonderful!

What is the SSM framework?

**SSM framework is an abbreviation of Spring+SpringMVC+MyBatis. It is a combination of Spring and MyBatis (SpringMVC is part of Spring). Often used as a framework for Web projects with simpler data sources. In great white words is convenient for the integration of front-end and back-end development, more suitable for enterprise project development, can reduce the amount of code lightweight framework.

Let’s make a brief introduction to these three components:

1, Spring

Spring is like a large factory for assembly beans throughout the project, specifying in the configuration file that objects are instantiated by calling the constructor of the entity class with specific parameters. You can also call it the glue in a project.

With Spring’s IOC feature, dependencies between objects can be handed over to Spring, facilitating decoupling and simplifying development. With Spring’s AOP features, it is easy to control transactions, logging, and permissions. Spring also provides integration support for other excellent open source frameworks. Specific low – intrusive characteristics.

2, for SpringMVC

SpringMVC intercepts user requests in a project and separates the roles of controller, model object, dispatcher, and handler object, which makes them easier to customize. At the same time, springMVC is a lightweight Web framework that uses MVC design ideas to decouple the Web layer, making our development more concise. It also works seamlessly with Spring. Flexible data validation, formatting, data binding mechanism.

3, MyBatis

MyBatis acts on the database persistence layer, is the encapsulation of JDBC, it makes the underlying database operation transparent. Mybatis operates around an instance of sqlSessionFactory. Mybatis is associated with the Mapper file of each entity class through the configuration file, and the Mapper file configures the SQL statement mapping required by each class to the database. Every time you interact with the database, get a sqlSession through sqlSessionFactory, and then execute SQL commands.

MyBatis has the following advantages:

  1. SQL is written in XML files to facilitate unified management and optimization, and decouple SQL from program code.
  2. Provide mapping label, support object and database ORM field relationship mapping, support object relationship mapping label, support the formation of object relationship
  3. Provides XML tags to support writing dynamic SQL.

Meanwhile, the disadvantages are:

  1. The workload is large, especially in the case of many fields in the table and many associated tables
  2. The preparation of SQL statements depends on the database, and the portability is poor
  3. Cascading deletion is not supported. You need to delete the table in cascading update mode.

In fact, the above mentioned so much, it may be a little difficult for friends to understand what is the SSM framework at the beginning, I will tell you in plain English:

The page sends the request to the controller, the controller calls the business layer to process the logic, the logic layer sends the request to the persistence layer, the persistence layer interacts with the database, and then returns the result to the business layer, the business layer sends the processing logic to the controller, and the controller calls the view to present the data.

Understanding the basic application principle and structure of SSM framework, then the next is how to get started and use the process, next we will start from Spring, introduce the use of spring IOC container.

Two, SpringIOC container details

1. What is SpringIOC?

Spring consists of two major components, IOC and AOP. Let’s talk about IOC.

IOC(IOC of Control)

When the components in the application need to obtain resources, the traditional way is that the components actively obtain the required resources from the container. In this mode, we often need to know the specific resource acquisition method in the specific container, which increases the learning cost and reduces the development efficiency.

* * the idea of inversion of control completely subvert the traditional way of the application components for resources: reverses the resources acquisition direction – instead container active will be pushed to resources needed components, developers do not need to know how to create a container is resource objects, you just need to provide a way to receive resources, * * greatly reduce the learning cost, improve the efficiency of development. This behavior is also known as the passive form of lookup.

(2) DI: Dependency Injection.

Speaking of inversion of control, dependency injection is indispensable. What is dependency injection? **DI dependency injection is another expression of IOC: a component receives injection of resources from a container in some pre-defined way, such as setter methods. ** Is more straightforward than IOC.

Instantiation of the SpringIOC container

The next step is how to use the IOC container to implement bean configuration. First, we need to instantiate the IOC container.

Spring provides two implementations of the IOC container:

  1. BeanFactory: The basic implementation of the IOC container, which is Spring’s internal infrastructure and is intended for Spring itself, not for use by developers.
  2. ApplicationContext: a subinterface of the BeanFactory that provides more advanced features. For Spring consumers, the ApplicationContext is almost always used instead of the underlying BeanFactory.

The ApplicationContext interface also has two common implementation classes:

  1. ClassPathXmlApplicationContext: corresponding classpath configuration file of XML format
  2. FileSystemXmlApplicationContext: corresponding configuration of file system in XML format

We are the most commonly used in the common development should be the ClassPathXmlApplicationContext,

As we usually obtain IOC container method is:

ApplicationContext iocContext = new ClassPathXmlApplicationContext(“ioc.xml”);

Add beans to the IOC container

The method for configuring beans in the IOC container also has a fixed format. In Spring, the bean properties added to the container should be written in, and the bean id and classpath should be specified as follows:

<! Define an object created by the IOC container using the bean element --> <! The class attribute specifies the full class name used to create the bean --> <! - the id attribute specifies the bean instance reference logo - > < bean id = "student" class = "com. Atguigu. The helloworld. Beans. Student" > <! <property name="studentId" value="1001"/> < Property name="stuName" value="Tom2015"/> <property name="age" value="20"/> </bean>Copy the code

Get the Bean properties from the IOC container

Now that we have an idea of how to instantiate and fetch an IOC container, how do we typically get bean properties from an IOC container? The common way to get beans in Spring is getBean(), which means different things depending on the parameters in the method.

Here are two common spring methods for getting beans,

1. Get by id of bean property

This method needs to know the container id of the bean we are fetching, such as the following bean definition in the IOC container:

<bean id=”person01″class=”com.spring.beans.Person”>

The bean id is used to obtain the bean property:

ApplicationContext iocContext = new ClassPathXmlApplicationContext("ioc.xml"); @test public void test01() {Person person01 = (Person)iocContext.getBean("person01"); System.out.println(person01); }Copy the code

2. Fetch by bean type

This method is obtained from the class assigned in getBean() as follows:

ApplicationContext iocContext = new ClassPathXmlApplicationContext("ioc.xml"); @test public void test02() {Person person01 = (Person)iocContext.getBean(person.class); System.out.println(person01); }Copy the code

If you define multiple bean attributes of the same class in an IOC container, which one should you retrieve using this method?

Ah, that’s right!

So this method can only be used if there is only one bean of this type in ioc, otherwise the following error will be reported:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.spring.beans.Person] is defined: expected single matching bean but found 4: person01,person03,person04,person05

In other words, the result is not found because there are multiple beans that meet the requirements and I do not know which one to use!

So what if you still want to use this method?

Which brings us to the third method.

3. Get parameters by class and attribute names

This method is a combination of the above two methods. GetBean () takes two arguments, the id of the property and the type of the bean property, as follows:

ApplicationContext iocContext = new ClassPathXmlApplicationContext("ioc.xml"); @test public void test02() {Person person02 = (Person)iocContext.getBean("person01",Person.class); System.out.println(person02); }Copy the code

This is a common way to get bean properties from the IOC container,

How do you assign attributes to beans in an IOC container?

Methods for assigning attributes to beans in IOC

Now that we know how to add the beans we need to the container, it’s time to assign values to those beans. Spring has a variety of methods for assigning values to properties in beans, depending on the type of properties in beans. Let’s take a look at each of these methods.

1. Construct with no arguments and specify attribute assignment

First of all, the first method, which is also the most commonly used method, is to use the set method of the bean attribute to perform assignment. This method needs to write the

<! <bean id="person01" class=" com.spring.beans.person ">< property name="name" value=" zhang3 "></property> <property name="age" value="18"></property> <property name="sex" value=" male "></property> <property name="email" value="[email protected]"></property> </bean>Copy the code

The name parameter is the name of the property you defined in the bean, the value is the value of the property you are going to assign,

2. Assign by taking arguments and specifying attributes

The above method specifies the method to assign a property to a bean without a parameter constructor. This method is now used when the bean has a parameter constructor. Of course, the above method can be used when the bean has both a parameter constructor and a no-parameter constructor.

Instead of using the parameter constructor, assignment is performed in, where the parameters name and value have the same meaning as above.

<! <bean id="person03" class=" com.spring.beans.person "> <constructor-arg name="name" </constructor-arg> <constructor-arg name="age" value="30"></constructor-arg> <constructor-arg name="email" value="[email protected]"></constructor-arg> </bean>Copy the code

3. Assign without specifying an attribute through a parameter construct

The second method introduces the use of parameter construction specified attribute assignment, but it is not the same as the first method, ah, let’s look at another method. With parameter constructs, assignment is done without specifying an attribute, that is, instead of using name, assignment is done with value. For example, the parameter constructor in our bean looks like this:

public Person(String name, int age, String sex, String email) {
    super();
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.email = email;
}
Copy the code

When an assignment is made through a parameterized constructor without specifying an attribute, the assignment elements are written in the same order as the element attributes in the parameterized constructor

<! Assignment via parameter construction does not specify attributes, but requires assignment in the order of constructor arguments --> <! -- Person(String name, int age, String sex, String email) --> <! <bean id="person04" class=" com.spring.beans.person "> <constructor-arg Value =" constructor "></constructor-arg> <constructor-arg value="20"></constructor-arg> <constructor-arg </constructor-arg> <constructor-arg value="[email protected]"></constructor-arg> </bean>Copy the code

One thing to note here:

If the two parameter constructors are overloaded, assignment in IOC uses type after value to specify the attribute of the assigned element, such as string or int.

For the use of type we look at the following method!

4. Specify the position of the parameter through the index value

When we use the parameter constructor without specifying attribute assignments, there are other ways to write assignments that do not have to be written in order. ** Assigning to an index requires the index argument, which specifies the ordinal number of the attribute to be assigned. ** In the case of the person constructor above, assigning to an element using index specifies the location of the element. The index of name is 0 and the age is 1.

<bean id="person04" class=" com.spring.beans.person "> <constructor-arg value="20" index= "1 "> </constructor-arg> </constructor-arg value="[email protected]" </constructor-arg value="[email protected]" Index =" 3 "></constructor-arg> </constructor-arg value=" 2" ></constructor-arg> </bean>Copy the code

The same assignment can be obtained in the same way

5. Distinguish overloaded constructors by type

If we now have two parameter constructors with the same name, but with different parameter types, we will obviously get an error message indicating that the constructor is not unique if we still use the above two methods to assign attributes in the bean.

In this case, we need to use type to specify the type of the parameter, and index and type can be used together, as follows:

<bean id="book" class="com.atguigu.spring.bean.Book" > <constructor-arg value= "10010" index ="0" type="java.lang.Integer" /> <constructor-arg value= "Book01" index ="1" type="java.lang.String" /> <constructor-arg Value =" Author01" index ="2" type="java.lang.String" /> <constructor-arg value= "20.2" index ="3" type="java.lang.Double" /> </bean >Copy the code

6. Assign using the P namespace

Let’s introduce a new way of assigning, using the P namespace.

To simplify the configuration of XML files, more and more XML files use attributes rather than child element configuration information. Spring introduced a new P namespace starting with version 2.5 that allows you to configure Bean properties as element properties.

Using the P namespace simplifies the XML-based configuration even further. Specific use is as follows,

The first step is to add a p-namespace plug-in to the XML file.

Add the P namespace to use the plug-in as follows: Take Eclipse as an example:

If you see the following code in the XML header file, you can add the following code to the header file:

xmlns:p=”www.springframework.org/schema/p”

The P namespace is used by assigning values in the P: format directly after the creation.

<! Assign the bean to the p namespace, import the P namespace --> <! <bean id="person05" class=" com.spring.beans.person "p :name=" ha "p :age="21" p:sex=" female" p:email="[email protected]"></bean>Copy the code

This can also achieve the effect of assigning values to beans.

For the above methods of assignment, after we get the IOC object directly, we call the getBean method to get the bean of the object and the value of one of its properties.

Let’s demonstrate:

@test public void text04() {Person person05 = (Person)iocContext.getBean("person05"); System.out.println(person05); }Copy the code

Console output:

Write at the end

This article covered a simple introduction to Spring in the SSM framework, an overview of the IOC container, and how to create an IOC container, instantiate and get an IOC container, add beans to it, get beans from it, and simply assign beans to the container.

I’ll go on to share with you how to assign to assigned properties, such as a class, Map, List, etc., inheritance between beans and more concise annotation development.

If you have any questions, please leave a comment in the comments section.

I am aAsh little apeAnd we’ll see you next time!