Introduction to Spring core functions and development steps

Introduction

Rod Johnson (the father of Spring) , released the latest version of Spring Spring 5.0 General Edition (GA) in September 2017. Spring is a layered Java SE/EE application full-stack lightweight open source framework, with IoC (Inverse Of Control) and AOP (Aspect Oriented Programming) as the core. Provides many enterprise-level application technologies such as the presentation layer (SpringMVC), persistence layer (pring JDBCTemplate), and business layer transaction management. It can also integrate many well-known third-party frameworks and class libraries in the open source world, gradually becoming the most used Java EE enterprise Apply open source framework!

Features

1. Facilitate decoupling and simplify development

The IOC container provided by Spring allows Spring to control the dependencies between objects to avoid over-coupling caused by hard coding. Users no longer need to write code for low-level requirements such as singleton pattern classes and property file analysis, and can focus more on upper-level applications.

2. AOP programming support

Spring's AOP function facilitates aspect-oriented programming. Many functions that are not easy to implement with traditional OOP can be easily implemented through AOP.

3. Declarative transaction support

It can free us from the monotonous and boring transaction management code, and flexibly carry out transaction management in a declarative way to improve development efficiency and quality.

4. Convenient for program testing

Almost all testing work can be performed in a non-container-dependent programming method. Testing is no longer an expensive operation, but something that can be done at hand.

5. Convenient integration of various excellent frameworks

Spring supports various excellent frameworks (Struts, Hibernate, Hessian, Quartz, etc.).

6. Reduce the difficulty of using JavaEE API

Spring has a thin encapsulation layer for JavaEE APIs (such as JDBC, JavaMail, remote calls, etc.), which greatly reduces the difficulty of using these APIs.

7. Java source code is a classic learning paradigm

Spring's source code design is exquisite, clear in structure, and original ingenuity. It reflects the master's flexible use of Java design patterns and advanced knowledge of Java technology. Its source code is not intended to be a good practice example of Java technology.

Two core functions

IOC (Inversion of Control)

1.Bean标签范围配置
singleton       默认值,单例的  
prototype       多例的                                              
request         WEB项目中,Spring创建一个 Bean 的对象,将对象存入到 request 域中 
session         WEB项目中,Spring创建一个 Bean 的对象,将对象存入到 session 域中 
global session  WEB项目中,应用在 Portlet环境,如果没有 Portlet 环境那么globalSession 

2.当scope的取值为singleton时

​ Bean的实例化个数:1个
​ Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
​ Bean的生命周期:
    对象创建:当应用加载,创建容器时,对象就被创建了
    对象运行:只要容器在,对象一直活着
    对象销毁:当应用卸载,销毁容器时,对象就被销毁了

3.当scope的取值为prototype时

​ Bean的实例化个数:多个
​ Bean的实例化时机:当调用getBean()方法时实例化Bean
  Bean的生命周期:
    对象创建:当使用对象时,创建新的对象实例
    对象运行:只要对象在使用中,就一直活着
    对象销毁:当对象长时间不用时,被 Java 的垃圾回收器回收了

4.Bean的三种实例化方式

  1) 使用无参构造方法实例化
  <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
  
  ---
  2) 工厂静态方法实例化
  public class StaticFactoryBean {
      public static UserDao createUserDao(){    
      return new UserDaoImpl();
      }
  }
  
  ---
  <bean id="userDao" class="com.itheima.factory.StaticFactoryBean"  factory-method="createUserDao" />
  
  3) 工厂实例方法实例化
  public class DynamicFactoryBean {  
  	public UserDao createUserDao(){        
  		return new UserDaoImpl(); 
  	}
  }
  
  ---
  <bean id="factoryBean" class="com.itheima.factory.DynamicFactoryBean"/>
  <bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>

5.Bean的依赖注入的数据类型

  1)  普通数据类型
  
  2)  引用数据类型
  
  3)  集合数据类型

6.引入其他配置文件 <import resource="applicationContext-xxx.xml"/>

7.ApplicationContext的继承体系(spring相关API)

  1)ClassPathXmlApplicationContext 它是从类的根路径下加载配置文件 推荐使用这种
  
  2)FileSystemXmlApplicationContext 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
  
  3)AnnotationConfigApplicationContext 当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

8.注解开发
@Configuration   声明该类是核心配置类,容器创建时从该类上加载注解        
@ComponentScan   注解扫描                               
@Bean            实例化对象                                                                                                
@PropertySource  加载配置资源
@Import          引入资源     
@Component       实例化bean
@Controller      web层实例化bean
@Service         service层实例化bean
@Repository		 dao层实例化bean     
@Autowired       依赖注入
@Qualifier       结合@Autowired一起使用用于根据名称进行依赖注入
注意:
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要
进行扫描以便识别使用注解配置的类、字段和方法。

AOP (Aspect Oriented Programming)

Introduction

AOP is the abbreviation of Aspect Oriented Programming, which means aspect-oriented programming. It is a technology that achieves unified maintenance of program functions through pre-compilation and runtime dynamic agents.

effect

During the running of the program, the method is enhanced without modifying the source code

Advantage

Reduce repetitive code, improve development efficiency, and facilitate maintenance

Low-level implementation

The bottom layer of AOP is realized by the dynamic proxy technology provided by Spring. During operation, Spring dynamically generates proxy objects through dynamic proxy technology, and when the proxy object method is executed, the enhanced function is involved, and the method of the target object is called to complete the function enhancement.
JDK agent: dynamic agent technology based on interface
cglib agent: dynamic agent technology based on parent class

Common terms

- Target(目标对象):代理的目标对象

- Proxy (代理):一个类被 AOP 织入增强后,就产生一个结果代理类

- Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点

- Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义

- Advice(通知/ 增强):所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知

- Aspect(切面):是切入点和通知(引介)的结合

- Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入

Development steps

①导入 AOP 相关坐标(aspectlweaver)

②创建目标接口和目标类(内部有切点)

③创建切面类(内部有增强方法)

④将目标类和切面类的对象创建权交给 spring

⑤在 applicationContext.xml 中配置织入关系

⑥测试代码

Cutpoint expression

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
例如:
execution(public void com.aop.Target.method())	
execution(void com.aop.Target.*(..))
execution(* com.aop.*.*(..))
execution(* com.aop..*.*(..))
execution(* *..*.*(..))

If you have any questions or different opinions, please leave a message and exchange together. The blogger will reply as soon as he sees it...

Guess you like

Origin blog.csdn.net/mrhs_dhls/article/details/106052589