SSM(Spring+SpringMVC+MyBatis)
Introduction to Spring
Spring is a layered Java SE/EE application full-stack lightweight open source framework, with loC (Inverse Of Control: Inversion of Control) and AOP (Aspect Oriented Programming: aspect-oriented programming) as the core.
Lightweight: refers to the framework is small and compact, the API is relatively simple to use, and the learning cost is low, otherwise it is heavyweight.
full-stack: full stack, which means that each layer has a parallel solution
It provides many enterprise-level application technologies such as the presentation layer SpringMVC, the persistence layer Spring JDBCTemplate, and business layer transaction management . It can also integrate many famous third-party frameworks and libraries in the open source world, and has gradually become the most used open source framework for Java EE enterprise applications.
Advantages of Spring
- Convenient decoupling and simplified development
Through the IoC container provided by Spring, the dependencies between objects can be controlled by Spring to avoid excessive coupling caused by hard coding. Users no longer need to write code for low-level requirements such as singleton pattern classes and property file parsing, and can focus more on upper-level applications.
- AOP programming support
Through the AOP function of Spring, aspect-oriented programming is convenient, and many functions that are not easy to achieve with traditional OOP can be easily realized through AOP.
- Support for declarative transactions
It can free us from the boring transaction management code, and perform transaction management flexibly in a declarative manner, improving development efficiency and quality.
- Convenient program testing
Almost all testing work can be done in a non-container-dependent programming way, and testing is no longer an expensive operation, but something that can be done easily.
- Easy to integrate various excellent frameworks
Spring supports various excellent frameworks (Struts, Hibemate, Quartz, etc.).
- Reduce the difficulty of using JavaEE API
Spring has thinly encapsulated JavaEE APIs (such as JDBC, JavaMail, remote calls, etc.), which greatly reduces the difficulty of using these APIs.
- Java source code is a classic learning paradigm
Spring's source code is exquisitely designed, clear in structure, and ingenious, reflecting the master's flexible use of Java design patterns and profound attainments in Java technology. Its source code is not intended to be an example of best practices in Java technology.
Spring Architecture
The order of learning goes from bottom to top.
Test: test
Core Container: The core container
includes four parts: by importing the Maven package.
Beans: The container is mainly
the Core that generates beans: Core
Context: Spring context
SpEL: Spring Express Language abbreviation, Spring's expression language
AOP、Aspects、Instrumentation、Messaging
Data Access: data access layer, including JDBC, ORM, OXM, JMS, Transaction (transaction)
Web
Spring quick start
Spring program development steps
UserServiceImpl calls the object of UserDaoImpl by using a new UserDao interface to specifically receive the implementation of UserDao
In order to complete the decoupling, generally through the configuration file, first create an xml configuration file
Assign the fully qualified name (full package name) UserDaoImpl in Dao to the configuration file
Use id to identify the fully qualified name, and you can use this identification to obtain the full package name. The role of the Spring framework is to read the configuration file, so as to obtain the full package name and help you create objects.
We don't need to use the new method to call the object. Instead, through the client provided by Spring, call its given getBean method to pass in an identifier, and use the identifier to obtain the object you want. It goes down to find the Spring framework.
The Spring framework does two things:
- Read the xml configuration file;
- Obtain the corresponding fully qualified name according to the ID passed in the past
The fully qualified name is returned to the inside of the framework, the framework gets the fully qualified name, helps you create objects through reflection, and returns the objects to our callers
steps :
- Import the basic package coordinates developed by Spring , that is, the Maven jar package corresponding to Spring
- Write the interface and implementation of Dao
- Create a Spring core configuration file , the xml configuration file
- Configure UserDaoImpl in the Spring configuration file
- Use Spring's API to get an instance of the Bean . That is, through the getBean method of the Spring client, pass in the identifier, and finally return a specified object, go to the Spring container to ask for the object, instead of going to the new object yourself
Concrete example:
1. Create a Maven project:
IDEA configures Maven method, address: http://t.csdn.cn/7WaxQ
Folder new "Module"
Select "Maven" - "next"
Name "Name" - browse to select the location "Location" - complete "finish"
2. Import Spring coordinates:
Find the pom.xml file and write a piece of code in it
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
3. Create UserDao interface and implementation:
Find "src" - "main" - "java", right click to create a new "Java Class"
Create the name "com.xy.dao.UserDao", select the "Interface" interface
Write a simple method in the interface
public void save();
Create a corresponding implementation for the interface, and create a new "Java Class" in the "com.xy.dao" package
Create a new package "impl" under the "com.xy.dao" package
with the class name "UserDaoImpl"
Select Class
Implement the interface in the "UserDaoImpl" class
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save running...");
}
}
4. Create a configuration file:
Under "resources", "new" - "XML Configuration File" - "Spring Config"
The name can be chosen at will, but the default is "applicationContext" under the convention
5. Configure the full package name of UserDaoImpl to the configuration file, and at the same time give an id corresponding to the full package name:
code show as below:
<bean id="UserDao" class="com.xy.dao.impl.UserDaoImpl" ></bean>
6. Test:
Create a new test package "com.xy.demo" under "java"
and create a new test class,
select "Class" for "UserDaoDemo"
Write the code in the test class, the code is as follows:
public class UserDaoDemo {
public static void main(String[] args) {
// 获得已经放到容器当中,让Spring帮忙创建的UserDao
// 1.app是Spring的客户端代码,传入的参数是配置文件applicationContext.xml
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.调用getBean方法,传入参数是id标识的名称userDao
// 给一个UserDao类型的返回值
UserDao userDao = (UserDao) app.getBean("userDao");
// 3.调用save方法,如果能拿到对象,可以在控制台看到那句话
userDao.save();
}
}
The running results are as follows, indicating that the object can be obtained