In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to understand the Java Spring framework and Spring IOC, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Introduction and configuration of Spring
Learning goal
[application] can complete the quick start of springIOC independently
[application] be able to master the configuration of bean tags of spring
[application] can independently complete the object attribute injection of bean
[application] can independently complete the ordinary attribute injection of bean
[understanding] can independently complete the collection attribute injection of bean
1. Spring Overview 1.1What is spring
Spring is a layered Java SE/EE application full-stack lightweight open source framework, with IoC (Inverse Of Control: inversion control) and AOP (Aspect Oriented Programming: aspect-oriented programming) as the core.
It not only provides many enterprise application technologies such as presentation layer SpringMVC, persistence layer Spring JDBCTemplate and business layer transaction management, but also integrates many famous third-party frameworks and class libraries in the open source world, and gradually becomes the most widely used open source framework for Java EE enterprise applications.
1.2 the development of Spring
In 1997, IBM put forward the idea of EJB
In 1998, SUN developed and developed the standard specification EJB1.0
In 1999, EJB1.1 released
In 2001, EJB2.0 released
In 2003, EJB2.1 released
In 2006, EJB3.0 released
Rod Johnson Robin Johnson (father of Spring)
Expert One-to-One J2EE Design and Development (2002) describes the advantages and solutions of J2EE's development and design using EJB.
Expert One-to-One J2EE Development without EJB (2004) describes the solution for J2EE development without using EJB (Spring chicks)
Shape)
The latest version of Spring, Spring 5.3.9 Universal version (officially released), was released in July 2021.
1.3 advantages of Spring (understanding)\ 1. Facilitate decoupling and simplify development
Spring is a large factory (IoC container), which can hand over the creation of all objects and the maintenance of dependencies to the Spring container, which greatly reduces the coupling between components.
\ 2. Support for AOP programming
Through the AOP function of Spring, it is convenient for aspect-oriented programming, and many functions that are not easy to implement with traditional OOP can be easily realized through AOP.
Such as the realization of the program permission interception, operation monitoring and other functions
\ 3. Support for declarative transactions
It can free us from the monotonous transaction management code and manage transactions flexibly in a declarative way to improve the efficiency and quality of development.
\ 4. Convenient program testing
Spring supports Junit4, you can easily test Spring programs through annotations, testing is no longer an expensive operation, but something you can do at your fingertips.
\ 5. Easy to integrate all kinds of excellent frameworks
Spring does not exclude a variety of excellent open source frameworks, and it provides direct support for a variety of excellent frameworks (such as Struts, Hibernate, MyBatis, Quartz, etc.).
\ 6. Reduce the difficulty of using JavaEE API
Spring encapsulates some API (JDBC, JavaMail, remote calls, etc.) that are very difficult to use in JavaEE development, which greatly reduces the difficulty of these API applications.
\ 7. Java source code is a classic learning example.
The source code of Spring is exquisite in design, clear in structure and unique in ingenuity, reflecting the master's flexible use of Java design patterns and profound attainments in Java technology everywhere. Its source code is not intended to be an example of best practices for Java technology.
1.4 Architecture of Spring (understanding)
2. The concept and function of Spring IoC Quick start 2.1IoC
IoC: Inversion Of Control means that the application itself is not responsible for the creation and maintenance of dependent objects, but the external container is responsible for the creation and maintenance of dependent objects. In this way, the control is transferred from the application to the external container, and the transfer of control is called reversal.
Simple understanding: leave the creation, initialization, destruction and other work of the object to the spring container. The lifecycle of the object is controlled by the spring container.
Clarify the role of IoC: to reduce the coupling of computer programs (to remove dependencies in our code).
It turns out that when we get objects, we all use new. I took the initiative.
Now: when we get the object, we ask the spring container for spring to find or create the object for us. It's passive.
2.2 Spring IoC program development steps
① imports basic package coordinates for Spring development
② writes Dao interfaces and implementation classes
③ creates Spring core profile
④ configures UserDaoImpl in the Spring configuration file
⑤ uses Spring's API to get Bean instances
2.3 Import the basic package coordinates developed by Spring org.springframeworkspring-context5.2.8.RELEASEjunitjunit4.12test2.4 to write the Dao interface and implementation class package com.summer.dao;public interface UserDao {public void save ();} package com.summer.dao.impl;import com.summer.dao.UserDao;public class UserDaoImpl implements UserDao {public void save () {System.out.println ("user add ~");}} 2.5 create Spring core configuration file
Create an applicationContext.xml profile under the classpath classpath (resources)
2.6 configure UserDaoImpl2.7 in the Spring configuration file to use Spring's API to get Bean instance public class TestIoC {public static void main (String [] args) {/ / create spring container instance ApplicationContext context = newClassPathXmlApplicationContext ("applicationContext.xml"); / / get bean from container get UserDao userDao = (UserDao) context.getBean ("userDao") according to unique identity id; userDao.save ();}}
Look for a configuration file under the classpath to instantiate the container
Spring Quick start Code consolidation activity
Instantiate objects through the XML configuration of springIoC under the exercise
\ 1. Import coordinat
\ 2. Create Bean
\ 3. Create a profile applicationContext.xml
\ 4. Configure in the configuration file
\ 5. Create an ApplicationContext object getBean
3. Getting started with dependency injection in Bean
① writes Dao,Service interfaces and implementation classes
Public interface UserDao {public void save ();} public class UserDaoImpl implements UserDao {public void save () {System.out.println ("user added ~");}} public interface UserService {public void save ();} public class UserServiceImpl implements UserService {public void save () {/ / TODO}}
② handed over the right to create UserDaoImpl and UserServiceImpl to Spring
The save () method of UserDao is called inside UserService.
Public class UserServiceImpl implements UserService {public void save () {/ / create spring container instance ApplicationContext context = new ClassPathXmlApplicationContext (); / / get dao object UserDao userDao = (UserDao) context.getBean ("userDao") according to id; / / call method userDao.save ();}}
③ gets the UserService from the Spring container to operate
Public class TestDI {/ / Quick start to test DI @ Testpublic void test1 () {/ / create spring container instance ApplicationContext context = newClassPathXmlApplicationContext ("applicationContext.xml"); / / get dao object UserService userService = (UserService) context.getBean ("userService") according to id; / / call method userService.save ();} 3.1 dependency injection Analysis of Bean
Currently, both UserService instance and UserDao instance exist in the Spring container. The current practice is to obtain UserService instance and UserDao instance outside the container, and then combine them in the program.
Because both UserService and UserDao are in the Spring container, and the final program uses UserService directly, you can set the UserDao inside the UserService in the Spring container.
3.2 dependency injection concept of Bean
DI: Dependency Injection dependency injection requires an IoC environment. In the process of creating an object by Spring, Spring injects the attributes that the object depends on into the object.
When writing a program, the creation of the object is handed over to Spring through control inversion, but it is impossible to have no dependencies in the code.
IoC decoupling only reduces their dependencies, but does not eliminate them. For example, the business layer still calls the methods of the persistence layer.
After using Spring, the dependency relationship between the business layer and the persistence layer is maintained by Spring.
To put it simply, it is to wait for the framework to pass the persistence layer objects into the business layer without having to get them ourselves.
3.3dependency injection method for Bean
How to inject UserDao into UserService?
Set method
Add a setUserDao method to UserServiceImpl
Public class UserServiceImpl implements UserService {/ / define dao member variable private UserDao userDao;public void setUserDao (UserDao userDao) {this.userDao = userDao;} public void save () {/ create spring container instance / / ApplicationContext context = newClassPathXmlApplicationContext ("applicationContext.xml"); / get dao object / / UserDao userDao = (UserDao) context.getBean ("userDao") according to id; / call method userDao.save ();}}
Configure the Spring container to call the set method for injection
Set method: P Namespace injection
P namespace injection is also set method injection in nature, but it is more convenient than the above set method injection, which is mainly reflected in the configuration file, as follows:
First, you need to introduce the P namespace:
Xmlns:p= "http://www.springframework.org/schema/p"
Second, the injection method needs to be modified.
3.4 data types of dependency injection for Bean
The above operations are all injected reference Bean, except for object references that can be injected, common data types, etc., can be injected in the container.
Two data types of injected data
1. Common data type
two。 Reference data type
The reference data type is not discussed here. All the previous operations are used to inject the reference to the UserDao object. Here we will take the set method injection as an example to demonstrate the injection of common data types.
Data types of dependency injection for Bean
(1) injection of common data types
Exercise:
Public class UserDaoImpl implements UserDao {/ / normal data type injection private String name;private int age;public void setName (String name) {this.name = name;} public void setAge (int age) {this.age = age;} public void save () {System.out.println (name+ "- -" + age); System.out.println ("user add ~");}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.