Spring深入研究(二)

Spring的bean管理(注解)

Spring注解开发准备

1 导入jar包

(1)导入基本的jar包

    • commons-logging.jar
    • log4j.jar
    • spring-beans.RELEASE.jar
    • spring-context.RELEASE.jar
    • spring-core.RELEASE.jar
    • spring-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.jar
    • aspectjweaver.jar
    • spring-aop-RELEASE.jar
    • spring-aspects-RELEASE.jar
  • 2 创建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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值