The next spring the Spring IOC and AOP concepts of operation

1 spring's bean management (notes)

1.1 Introduction Notes

Wording: @注解名称(属性名称=属性值)
Usage: used in class, the above methods and properties

1.2 Development Readiness comment

  • Jar package into the base, and then introduced into the aoppackage for the development of annotations
  • Create classes, methods,
  • Create a profile, the introduction of constraint beans, re-introduced spring-context.xsdfor the annotation Development
  • Open comments Scan: xml configuration file
    Here Insert Picture Description

1.3 notes to create objects

In the class to create objects using the above comments realization, so you do not have to configure each class in xml, only need to set the id in the definition of class time on it.
Here Insert Picture Description
Notes include the following spring,:
(1) @Component
(2) @Controller
(3) @Service
(4) the @Repository
currently four annotation function is the same, the object is created

Create a single instance or multi-instance objects:
Here Insert Picture Description

1.4 annotation object properties injection

  • @Autowired(value=“类名”)
    Here Insert Picture Description

  • @Resource (name = "Class id")
    Here Insert Picture Description

2. AOP

2.1 concepts and principles (master)

(1) aop: Oriented Programming, extended functions without modifying the source code
(2) aop taken transverse extraction mechanism, replaces the conventional vertical type inheritance hierarchy repeating codes
(3) aop underlying dynamic proxy implementation

  • An interface, the interface implementation using dynamic proxy class to create a proxy object
  • No interface, the proxy object subclass dynamic proxy class is created
    Here Insert Picture Description

2.2 Procedural Terminology (control)

  • JoinPoint (connection points) : class which can be enhanced method , referred to as connection point of these methods
  • Pointcut(切入点): 实际增强的方法
  • Advice(通知/增强): 即增强,所要做的事情就是通知.
    – 通知分为前置通知,后置通知,异常通知,最终通知(后置之后),环绕通知(方法的前面和后面)
  • Aspect(切面): 把增强应用到切入点过程
  • Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
  • Target(目标对象):要增强的
  • Weaving(织入):是把增强应用到目标对象的过程.
  • Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类

2.3 aop操作

1 在spring里面进行aop操作,使用aspectj实现
(1)aspectj不是spring一部分,和spring一起使用进行aop操作
(2)Spring2.0以后新增了对AspectJ支持

2 使用aspectj实现aop有两种方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式

3.开发准备
(1)除了导入基本的jar包之外,还需要导入aop相关的jar包
Here Insert Picture Description
(2)创建spring核心配置文件,导入aop的约束

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
  1. 使用表达式配置切入点
    常用的表达式 execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
    (1)execution(* cn.itcast.aop.Book.add(…)) Book类中的方法add
    (2)execution(* cn.itcast.aop.Book.(…)) Book类中的所有方法
    (3)execution(
    .(…)) 所有类中的所有方法
    (4)execution(* save*(…)) 匹配所有save开头的方法
  2. Aspectj的aop操作-使用XML方法
    Here Insert Picture Description
    环绕的增强有些区别:
    Here Insert Picture Description

3. dom4j

1 通过log4j可以看到程序运行过程中更详细的信息
(1)经常使用log4j查看日志
2 使用
(1)导入log4j的jar包:log4j.properties
(2)复制log4j的配置文件到src下面
3 在log4j.properties中设置日志级别
(1)info:看到基本信息(常用)
(2)debug:看到更详细信息

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout

4. Spring整合web项目演示

1 演示问题
功能:action调用service,service调用dao
缺陷:每次访问action时候,都会加载spring配置文件

Solution 2:
(1) start time in the server, create object to load the configuration file
(2) the use of the underlying listener, ServletContext objects

3 In the spring there is no need to write code to achieve our own, to help package
(1) encapsulates a listener, only need to configure the listener will be able to
do something before (2) to configure the Listener: imported spring integrated web project jar package
Here Insert Picture Description
(3 ) specify the load spring configuration file location
Here Insert Picture Description

Published 135 original articles · won praise 5 · Views 7085

Guess you like

Origin blog.csdn.net/qq_27921205/article/details/104530648