Spring深入研究(二)

xiaoxiao2021-02-28  53

Spring的bean管理(注解)

Spring注解开发准备

1 导入jar包

(1)导入基本的jar包

commons-logging.jarlog4j.jarspring-beans.RELEASE.jarspring-context.RELEASE.jarspring-core.RELEASE.jarspring-expression.RELEASE.jar

(2) 导入aop的jar包

spring-aop-RELEASE.jar

2 创建类,创建方法

3 创建spring配置文件,引入约束

(1) ioc基本功能,引入约束beans

(2) 做spring的ioc注解开发,引入新的约束

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> </beans>

4 开启注解扫描

<!-- 开启注解扫描 (1)到包里面扫描类.方法.属性上面是否有注解 --> <context:component-scan base-package="com.jia"></context:component-scan> <!-- 扫描属性上面的注解 --> <context:annotation-config></context:annotation-config>

注解创建对象

1 在创建对象的类上面使用注解实现

@Component(value="user") //<bean id="user" class=""/> public class User { }

2 创建对象有四个注解:

Spring中提供@Component的三个衍生注解:

@Component

@Controller: WEB层

@Service: 业务层@Repository: 持久层

3 创建对象单实例还是多实例

@Component(value="user") //<bean id="user" class=""/> @Scope(value="prototype") //多实例 public class User { }

注解注入属性

//创建service类,创建dao类,在service得到dao对象 @Component(value="userService") public class UserService { /** * 得到dao对象 * 定义dao类型属性 * 在dao属性上面使用注解完成对象注入 * 使用注解方时候不需要set方法 * @Autowired */ @Autowired private UserDao dao; /** * @Resource * name属性值写注解创建dao对象value值 */ @Resource(name="userDao") private UserDao userDao; }

配置文件和注解混合使用

1 创建对象操作使用配置文件方式实现

2 注入属性的操作使用注解方式实现

AOP

AOP概念

1 aop:面向切面(方面)编程,扩展功能不修改源代码实现2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码

AOP原理

动态代理

aop操作术语

Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连接点

Pointcut(切入点):

在类里面可以有很多的方法被增强,实际被增强的方法称为切入点

Advice(通知/增强):

增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强

前置通知:在方法之前执行后置通知:在方法之后执行异常通知:在方法出现异常最终通知:在后置之后执行环绕通知:在方法之前和之后执行

Aspect(切面):

把增强应用到具体方法上面,过程称为切面,把增强用到切入点过程

基于aspectJ的XML准备工作

Spring的aop操作:

1 在spring里面进行aop操作,使用aspectj实现

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

2 使用aspectJ实现aop有两种方式

(1)基于aspectJ的Xml配置(2)基于aspectJ的注解方式

Aop操作准备

1 除了导入基本的Jar包之外,还需要导入aop相关的jar包 aopalliance.jaraspectjweaver.jarspring-aop-RELEASE.jarspring-aspects-RELEASE.jar2 创建spring核心配置文件,导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> <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"> <!-- bean definitions here --> </beans>

使用表达式配置切入点

1 切入点:实际增强的方法

2 常用的表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)execution(* com.jia.aop.Book.add(…))execution(* com.jia.aop.Book.*(…))execution(* * . *(…))匹配所有save开头的方法execution(* save*(..))

aspectJ的aop操作

public class MyBook { private void before1() { System.out.println("前置增强............."); } private void after1() { System.out.println("后置增强............."); } /** * 环绕增强 * * @throws Throwable */ private void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 方法之前 System.out.println("方法之前............"); // 执行被增强的方法 proceedingJoinPoint.proceed(); // 方法之后 System.out.println("方法之后..................."); } } <!-- 1 配置对象 --> <bean id="book" class="com.jia.aop.Book"></bean> <bean id="myBook" class="com.jia.aop.MyBook"></bean> <!-- 2 配置aop操作 --> <aop:config> <!-- 2.1 配置切入点 --> <aop:pointcut expression="execution(* com.jia.aop.Book.*(..))" id="pointcut1" /> <!-- 2.2 配置切面 把增强用到方法上面 --> <aop:aspect ref="myBook"> <!-- 配置增强类型 method: 增强类里面使用哪个方法作为前置 --> <aop:before method="before1" pointcut-ref="pointcut1" /> <!-- 后置增强 --> <aop:after-returning method="after1" pointcut-ref="pointcut1" /> <!-- 环绕增强 --> <aop:around method="around1" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config>

Log4j介绍

1 通过log4j可以看到程序运行过程中更详细的信息

2 使用

导入log4j的jar包

复制log4j的配置文件,复制到src下面

log4j.properties

3 设置日志级别

​ log4j.rootLogger=info, stdout

info: 看到基本信息debug: 看到更详细信息

Spring整合web项目

原理:

在服务器启动时候,创建对象加载配置文件底层使用监听器,ServletContext对象

在web.xml中配置Spring

封装了一个监听器,只需要配置监听器就可以

配置监听器之前:导入Spring整合web项目jar包

spring-web-RELEASE.jar

<!-- 配置Spring监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

指定加载Spring配置文件位置

<!-- 指定Spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>

转载请注明原文地址: https://www.6miu.com/read-58707.html

最新回复(0)