一、Spring的Bean管理(注解方式)

1.1 什么是注解

  要使用注解方式实现Spring的Bean管理,首先要明白什么是注解。通俗地讲,注解就是代码里的特殊标记,使用注解可以完成相应功能。

  注解的写法:@注解名称(属性名称=属性值)。

  注解的用法:可以使用在类上、方法上和属性上。

1.2 注解开发准备工作

  ●  导入jar包,其中包括基本jar包和aop相关的jar包,如下图:

  

  ●  引入相关约束(除了第一节学习中用到的beans约束,还要加入新的约束),如下:

<?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" >

1.3 编写相关的测试类

package com.Kevin.anno;

import org.springframework.stereotype.Component;

/**
* 使用注解方式在service中调用dao方法
* @author Kevin
*
*/ @Component("userDao") //value属性名称可以省略不写
public class UserDao { public void add(){
System.out.println("Dao------");
} }

1.4 在配置文件中配置注解扫描

<!-- 开启注解扫描
到对应的包里扫描 类、属性、方法上的注解
-->
<context:component-scan base-package="com.Kevin.anno"></context:component-scan>

1.5 在相关的类上添加注解

package com.Kevin.anno;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* 使用注解方式在service中调用dao方法
* @author Kevin
*
*/ @Service("userService")
public class UserService { //得到dao对象
//1.定义dao类型属性
//在dao属性上使用注解完成对象注入
//@Autowired
//name属性值填写 注解创建dao对象的value值
@Resource(name="userDao")
private UserDao userDao; public void add(){
System.out.println("Service------");
userDao.add();
} }

1.6 编写测试类进行测试:

package com.Kevin.anno;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 测试注解创建类对象
* @author Kevin
*
*/ public class TestAnno { @Test
public void testService(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans1.xml"); UserService service=(UserService) context.getBean("userService");
System.out.println(service);
service.add();
} }

二、Spring的Bean管理中常用的注解

2.1 Component组件(作用在类上)

Spring 中提供@Component 的三个衍生注解:(功能目前来讲是一致的)

  ●  @Controller:WEB 层

  ●  @Service:业务层

  ●  @Repository:持久层

  这三个注解是为了让标注类本身的用途清晰,Spring 在后续版本会对其增强

2.2 属性注入的注解(使用注解注入的方式,可以不用提供set方法)

  ●  @Value:用于注入普通类型。

  ●  @Autowired:自动装配:

            ①按默认类型进行装配

            ②按名称注入:@Qualifier:强制使用名称注入。

  ●  @Resource:相当于@Autowired和@Qualifier一起使用。

2.3 Bean的作用范围的注解

  ●  @Scope:

    * singleton:单例

    * prototype:多例

2.4 Bean的生命周期的配置

  ●  @PostConstruct:相当于init-method。

  ●  @PreDestroy:相当于destroy-method。

三、Spring的Bean管理方式的比较

XML和注解比较:

  ●  XML:结构清晰.

  ●  注解:开发方便.(属性注入.)

实际开发中还有一种 XML 和注解整合开发:

  ●  Bean XML 配置,是使用的属性使用注解注入。

四、AOP的概述

4.1 什么是AOP

  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

4.2 为什么学习AOP

  面向切面编程,扩展功能不通过源代码修改。AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码。

4.3 Spring的AOP的由来

  AOP 最早由 AOP 联盟的组织提出的,制定了一套规范.Spring 将 AOP 思想引入到框架中,必须遵守 AOP 联盟的规范。

4.4 AOP的底层实现

代理机制:

  ●  Spring 的 AOP 的底层用到两种代理机制:

  ●  JDK 的动态代理 :针对实现了接口的类产生代理.

  ●  Cglib 的动态代理:针对没有实现接口的类产生代理. 应用的是底层的字节码增强的技术 生成当前类的子类对象.

五、Spring底层AOP

六、Spring 的基于 AspectJ 的 AOP 开发

6.1 AOP开发中相关的术语

  ●  Pointcut:切入点,在类中有很多方法可以被增强,而只有实际增强的方法称为切入点;
  ●  Advice:通知/增强,实际增强的逻辑,被称为通知/增强,比如拓展日志功能,日志功能被称为通知/增强;

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

  ●  Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field;

  ●  Target(目标对象):代理的目标对象;

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

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

  ●  Aspect:切面,将增强应用到具体方法上的过程称为切面(把增强用到切入点过程)
  ●  Joinpoint:连接点,类里面可以被增强的方法,被称为连接点

七、Spring 使用 AspectJ 进行 AOP 的开发:XML 的方式

7.1 编写被增强类

package com.Kevin.aop;
/**
* AOP操作被增强类
* @author Kevin
*
*/ public class Book { public void add(){
System.out.println("Bookadd-----");
} }

7.2 编写增强类

package com.Kevin.aop;

import org.aspectj.lang.ProceedingJoinPoint;

/**
* AOP操作增强类
* 前值增强
* @author Kevin
*
*/ public class StrBook { public void strbefore(){
System.out.println("Before Strength BookClass---");
} public void strafter(){
System.out.println("Atfer Strentgh BookClass---");
} //环绕增强
public void straround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//方法之前
System.out.println("Before around---");
//执行被增强方法
proceedingJoinPoint.proceed();
//方法之后
System.out.println("After around---");
} }

7.3 编写测试代码

package com.Kevin.aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestUsage { //AOP前置增强
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans3.xml"); Book book=(Book) context.getBean("book");
book.add();
} }

八、Spring 的Bean管理:XML+注解 的方式实现 

8.1 使用注解方式注入对象

  创建BookDao和OrdersDao两个类,在BookService中使用注解的方式注入两个dao。

package com.Kevin.XMLanno;
/**
* 配置文件和注解混合使用
* 创建对象操作使用配置文件方式实现
注入属性的操作使用注解方式实现
* @author Kevin
*
*/ public class BookDao { public void book(){
System.out.println("BookDao----");
} }
package com.Kevin.XMLanno;
/**
* 配置文件和注解混合使用
* 创建对象操作使用配置文件方式实现
注入属性的操作使用注解方式实现
* @author Kevin
*
*/ public class OrdersDao { public void buy(){
System.out.println("OrdersDao----");
} }
package com.Kevin.XMLanno;

import javax.annotation.Resource;

/**
* 配置文件和注解混合使用
* 创建对象操作使用配置文件方式实现
注入属性的操作使用注解方式实现
* @author Kevin
*
*/ public class BookService {
//得到BookDao和OrdersDao的对象
@Resource(name="bookDao")
private BookDao bookDao; @Resource(name="ordersDao")
private OrdersDao ordersDao; public void add(){
System.out.println("BookService----");
bookDao.book();
ordersDao.buy();
} }

8.2 用xml配置文件的形式创建对象

<?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 --> <!-- 开启注解扫描
到对应的包里扫描 类、属性、方法上的注解
-->
<context:component-scan base-package="com.Kevin.XMLanno"></context:component-scan> <!-- 配置对象 -->
<bean id="bookService" class="com.Kevin.XMLanno.BookService"></bean>
<bean id="ordersDao" class="com.Kevin.XMLanno.OrdersDao"></bean>
<bean id="bookDao" class="com.Kevin.XMLanno.BookDao"></bean> </beans>

[ SSH框架 ] Spring框架学习之二(Bean的管理和AOP思想)的更多相关文章

  1. SSH 之 Spring的源码(二)——Bean实例化

    首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. <span ...

  2. [转]Spring Security学习总结二

    原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/red ...

  3. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  4. [ SSH框架 ] Spring框架学习之三(AOP开发和注解的使用)

    一.Spring 使用 AspectJ 进行 AOP 的开发:注解的方式 1.1 引入相关的jar包 1.2 引入spring的配置文件 <?xml version="1.0" ...

  5. Java框架spring Boot学习笔记(六):Spring Boot事务管理

    SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.

  6. 开源框架.netCore DncZeus学习(二)配置连接

    配置连接字符串,update-database,初始数据后,访问报错,提示offset错误.因为本机上使用的sql2008. .net Core 2.X中的EF访问sqlserver2008默认使用的 ...

  7. spring MVC学习之二

    什么是Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MV ...

  8. Spring Boot学习笔记二

    Spring Boot入门第二篇 第一天的详见:https://www.cnblogs.com/LBJLAKERS/p/12001253.html 同样是新建一个pring Initializer快速 ...

  9. [ SSH框架 ] Spring框架学习之一

    一.Spring概述 1.1 什么是Spring Spring是一个开源框架, Spring是于2003年兴起的一个轻量级的Java开发框架,由 Rod Johnson在其著作 Expert One- ...

随机推荐

  1. CCRenderBuffer初始化中的render state参数

    绘制纹理三角形的渲染状态(render state)已经被CCSprite基类设置过了,所以你可以简单的将self.renderState传递过去就可以了. 渲染状态是混合模式(blend mode) ...

  2. 关于iOS中几种第三方对XML/JSON数据解析的使用

    Json XML 大数据时代,我们需要从网络中获取海量的新鲜的各种信息,就不免要跟着两个家伙打交道,这是两种结构化的数据交换格式.一般来讲,我们会从网络获取XML或者Json格式的数据,这些数据有着特 ...

  3. Android进阶(二十一)创建Android虚拟机

    创建Android虚拟机

  4. 数据库用户映射到SQL Server登录名

    由于不是固定在一如电脑开始,经常需要把数据备份与恢复.或者是帮助客户修改程序,接收到客户备份好的数据,需要在电脑恢复. 这样就需要将数据库用户映射到 SQL Server 登录名.如何映射?可使用下面 ...

  5. mysql清空表

    清空某个mysql表中所有内容 delete from 表名; truncate table 表名; 不带where参数的delete语句可以删除mysql表中所有内容,使用truncate tabl ...

  6. MySQL 和 JDBC(Java数据库连接)

    1.MySQL 1.1   MySQL简介 a)MySQL是一个开源免费的关系型数据库管理系统. b)默认用户:root c)默认端口号: 2.MySQL常用命令 2.1连接MySQL mysql   ...

  7. Android 加载gif图片强大框架(支持预加载、缓存,还支持显示静态图片,一行代码全搞定)

    之前项目中没有涉及到显示gif图片的功能,也没有着重研究过,最近项目中要用到显示gif图片,于是就在网上一顿搜,用过之后发现如下几个缺点. 1.加载大的gif图片会出现oom. 2.没有预加载和缓存功 ...

  8. storm中的Scheduler

    Scheduler是storm的调度器,负责为topology分配当前集群中可用的资源.Storm分别提供了3中调度器: EvenScheduler:会将系统中的可用资源均匀地分配给当前需要任务分配的 ...

  9. JavaScript中将对象数组中的某个属性值,批量替换成另一个数值

    原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...

  10. Linux下高并发socket最大连接数

    http://soft.chinabyte.com/os/285/12349285.shtml (转载时原文内容做个修改) 1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是 ...