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.jar2 创建类,创建方法
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 { }1 创建对象操作使用配置文件方式实现
2 注入属性的操作使用注解方式实现
AOP概念
1 aop:面向切面(方面)编程,扩展功能不修改源代码实现2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码AOP原理
动态代理
在类里面可以有很多的方法被增强,实际被增强的方法称为切入点
增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强
前置通知:在方法之前执行后置通知:在方法之后执行异常通知:在方法出现异常最终通知:在后置之后执行环绕通知:在方法之前和之后执行把增强应用到具体方法上面,过程称为切面,把增强用到切入点过程
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*(..))1 通过log4j可以看到程序运行过程中更详细的信息
2 使用
导入log4j的jar包
复制log4j的配置文件,复制到src下面
log4j.properties
3 设置日志级别
log4j.rootLogger=info, stdout
info: 看到基本信息debug: 看到更详细信息原理:
在服务器启动时候,创建对象加载配置文件底层使用监听器,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>