Spring必须掌握的技术

Spring框架的概述

    * Spring是一个开源框架
    * Spring是于2003 年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。
    * 它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。
    * Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以   从Spring中受益。
    * Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。


轻量级,面向切面,控制反转,方便开发,可以把spring看做一个平台可以搭载其他优秀的框架
    * EE开发分成三层结构
        * WEB层      -- Spring MVC
        * 业务层   -- Bean管理:(IOC)
        * 持久层   -- Spring的JDBC模板.ORM模板用于整合其他的持久层框架


注解和配置文件都是把需要使用的类提交加载,也就是说把要使用的类配置在xml文件中

Spring框架的特点

1. 为什么要学习Spring的框架
    * 方便解耦,简化开发
        * Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
    * AOP编程的支持
        * Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
    * 声明式事务的支持
        * 只需要通过配置就可以完成对事务的管理,而无需手动编程
    * 方便程序的测试
        * Spring对Junit4支持,可以通过注解方便的测试Spring程序
    * 方便集成各种优秀框架
        * Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持
    * 降低JavaEE API的使用难度
        * Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低


IOC 
1. 什么是IOC的功能?
    * IoC       -- Inverse['ɪnvɜːs]  of Control[kən'trəʊl],控制反转,将对象的创建权反转给Spring!!
* 使用IOC可以解决的程序耦合性高的问题!!
高内聚,是指让一个类或者一个方法让他专注去做一件事情。
低耦合,是类与类之间的联系低
 
2. 步骤一:下载Spring框架的开发包
    * 官网:http://spring.io/
    * 下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring解压:(Spring目录结构:)
        * docs      -- API和开发规范
        * libs      -- jar包和源码
        * schema    -- 约束


3. 步骤二:创建JavaWEB项目,引入Spring的开发包
    * 引入Spring框架IOC核心功能需要的具体的jar包
        * Spring框架的IOC的功能,那么根据Spring框架的体系结构图能看到,只需要引入如下的jar包
            * Beans
            * Core
            * Context
            * Expression Language


        * Spring框架也需要引入日志相关的jar包
            * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
                * com.springsource.org.apache.commons.logging-1.1.1.jar


            * 还需要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15
                * com.springsource.org.apache.log4j-1.2.15.jar


4. 步骤三:创建对应的包结构,编写Java的类,要注意:以后使用Spring框架做开发,都需要来编写接口与实现类!!
    * com.itcast.demo1
        * UserService           -- 接口
        * UserServiceImpl       -- 具体的实现类


5. 步骤四:想把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件,完成配置
    * 在src目录下创建applicationContext.xml的配置文件,名称是可以任意的,但是一般都会使用默认名称!!


    * 引入spring的约束,需要先找到具体的约束头信息!!
        * spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html
        * 具体的约束如下:      
           
<beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd">
 </beans>


    * 完成UserService的配置
        <!-- Spring的快速入门 -->
        <bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>


6. 步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!
   
 public void demo2(){
        // 使用Spring的工厂:
        ApplicationContext context = 
new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过工厂获得类:
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHello();
    }


________________________________________
Spring框架中的工厂(了解)
ApplicationContext接口
    * 使用ApplicationContext工厂的接口,使用该接口可以获取到具体的Bean对象
    * 该接口下有两个具体的实现类
        * ClassPathXmlApplicationContext            -- 加载类路径下的Spring配置文件
当读取配置文件时会加载配置文件中的所有对象
        * FileSystemXmlApplicationContext           -- 加载本地磁盘下的Spring配置文件


BeanFactory工厂(是Spring框架早期的创建Bean对象的工厂接口)
    * 使用BeanFactory接口也可以获取到Bean对象
        public void run(){
            BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
            UserService us = (UserService) factory.getBean("us");
            us.sayHello();
        }


BeanFactory和ApplicationContext的区别
        * BeanFactory               -- BeanFactory采取延迟加载,第一次getBean时才会初始化Bean
        * ApplicationContext        -- 在加载applicationContext.xml时候就会创建具体的Bean对象的实例,还提供了一些其他的功能
            * 事件传递
            * Bean自动装配
            * 各种不同应用层的Context实现
________________________________________
入门总结之配置Spring框架编写XML的提示
1. 步骤一:先复制, http://www.springframework.org/schema/beans/spring-beans.xsd    
2. 步骤二:搜索XML Catalog,点击Add按钮
3. 步骤三:先选择Location的schema的约束地址
    * E:\class\2016\JavaEE28\day35_Spring框架第一天\资料\spring-framework-4.2.4.RELEASE-schema\beans\spring-beans-4.2.xsd
4. 步骤四:注意:Key type要选择:Schema location
5. 步骤五:Key把http://www.springframework.org/schema/beans/spring-beans.xsd复制上
________________________________________
技术分析之Spring框架的Bean管理的配置文件方式
________________________________________
技术分析之Spring框架中标签的配置
1. id属性和name属性的区别
    * id        -- Bean起个名字,在约束中采用ID的约束,唯一
        * 取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号  id:不能出现特殊字符


    * name      -- Bean起个名字,没有采用ID的约束(了解)
        * 取值要求:name:出现特殊字符.如果<bean>没有id的话 , name可以当做id使用
        * Spring框架在整合Struts1的框架的时候,Struts1的框架的访问路径是以/开头的,例如:/bookAction


2. class属性          -- Bean对象的全路径
3. scope属性          -- scope属性代表Bean的作用范围
    * singleton         -- 单例(默认值)
    * prototype         -- 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例!!
    * request           -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean
    * session           -- 应用在Web项目中,同一个HTTP Session 共享一个Bean
    * globalsession     -- 应用在Web项目中,多服务器间的session


4. Bean对象的创建和销毁的两个属性配置(了解)
    * 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
    * init-method       -- 当bean被载入到容器的时候调用init-method属性指定的方法
    * destroy-method    -- 当bean从容器中删除的时候调用destroy-method属性指定的方法
        * 想查看destroy-method的效果,有如下条件
            * scope= singleton有效
            * web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
________________________________________
技术分析之依赖注入(DI)
1. IOC和DI的概念
    * IOC       -- Inverse of Control,控制反转,将对象的创建权反转给Spring!!
    * DI        -- Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!!


2.  DI(依赖注入)
    * 例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来!!
    * 具体的配置如下
        <bean id="us" class="com.itheima.demo1.UserServiceImpl">
            <property name="uname" value="小风"/>
        </bean>
________________________________________
技术分析之Spring框架的属性注入
1. 对于类成员变量,常用的注入方式有两种
    * 构造函数注入
    * 属性setter方法注入


2. 在Spring框架中提供了前两种的属性注入的方式
    1. 构造方法的注入方式,两步
        * 编写Java的类,提供构造方法
            public class Car {
                private String name;
                private double money;
                public Car(String name, double money) {
                    this.name = name;
                    this.money = money;
                }
                @Override
                public String toString() {
                    return "Car [name=" + name + ", money=" + money + "]";
                }
            }


        * 编写配置文件
            <bean id="car" class="com.itheima.demo4.Car">
                <constructor-arg name="name" value="大奔"/>
                <constructor-arg name="money" value="100"/>
            </bean>
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:mail.properties"/>
    2. 属性的setter方法的注入方式
        * 编写Java的类,提供属性和对应的set方法即可
        * 编写配置文件


    3. 如果Java类的属性是另一个Java的类,那么需要怎么来注入值呢?
        * <property name="name" rel="具体的Bean的ID或者name的值"/>
        * 例如:
            <bean id="person" class="com.itheima.demo4.Person">
                <property name="pname" value="美美"/>
                <property name="car2" ref="car2"/>
            </bean>
________________________________________
技术分析之Spring的2.5版本中提供了一种:p名称空间的注入(了解)
1. 步骤一:需要先引入 p 名称空间
    * 在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"


2. 步骤二:使用p名称空间的语法
    * p:属性名 = ""
    * p:属性名-ref = ""


3. 步骤三:测试
    * <bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
________________________________________
技术分析之Spring的3.0提供了一种:SpEL注入方式(了解)
1. SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法
2. 语法
    * #{SpEL}


3. 例如如下的代码
    <!-- SpEL的方式 -->
    <bean id="person" class="com.itheima.demo4.Person">
        <property name="pname" value="#{'小风'}"/>
        <property name="car2" value="#{car2}"/>
    </bean>


4. 还支持调用类中的属性或者方法
    * 定义类和方法,例如
        public class CarInfo {
            public String getCarname(){
                return "奇瑞QQ";
            }
        }
________________________________________

技术分析之数组,集合(List,Set,Map),Properties等的注入

1. 如果是数组或者List集合,注入配置文件的方式是一样的
   
<bean id="collectionBean" class="com.it.demo5.CollectionBean">
        <property name="arrs">
            <list>
                <value>美美</value>
                <value>小风</value>
            </list>
        </property>
 </bean>




2. 如果是Set集合,注入的配置文件方式如下:
   
<bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
        <property name="sets">
        <set>
            <value>哈哈</value>
            <value>呵呵</value>
        </set>
    </property>
 </bean>




3. 如果是Map集合,注入的配置方式如下:
 
<bean id="collectionBean" class="com.it.demo5.CollectionBean">
       <property name="map">
        <map>
            <entry key="老王2" value="38"/>
            <entry key="凤姐" value="38"/>
            <entry key="如花" value="29"/>
        </map>
    </property>
 </bean>


4. 如果是properties属性文件的方式,注入的配置如下:
   
<bean id="collectionBean" class="com.it.demo5.CollectionBean">
       <bean id="collectionBean" class="com.it.demo5.CollectionBean">
       <property name="map">
        <map>
            <entry key="老王2" value="38"/>
            <entry key="凤姐" value="38"/>
            <entry key="如花" value="29"/>
        </map>
    </property>
 </bean>

将map list set 抽离

list:
<bean id="names" class="java.util.ArrayList">  
        <constructor-arg>  
        <list>  
            <value>倩倩</value>  
  	    <value>夕夕</value>  
        </list>  
        </constructor-arg>  
</bean>




map:

<bean id="map" class="java.util.HashMap">  
	        <constructor-arg>  
	        <map>  
	            <entry key="name" value="倩倩"/>
	            <entry key="age" value="20"/> 
	        </map>  
	        </constructor-arg>  
</bean>

set:

<bean id="set" class="java.util.TreeSet">  
         <constructor-arg>  
        <set>  
            <value>倩倩</value>  
  	    <value>夕夕</value>  
        </set>  
        </constructor-arg>  
</bean>


________________________________________
技术分析之Spring框架的配置文件分开管理(了解)
1. 例如:在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种!
    * 主配置文件中包含其他的配置文件:
        <import resource="applicationContext2.xml"/>


    * 工厂创建的时候直接加载多个配置文件:
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    "applicationContext.xml","applicationContext2.xml");
________________________________________
Spring框架整合WEB
________________________________________
Spring框架整合WEB(不是最终的方案)
1. 创建JavaWEB项目,引入Spring的开发包。编写具体的类和方法。
    * 环境搭建好后,启动服务器来测试项目,发送每访问一次都会加载一次配置文件,这样效率会非常非常慢!!


2. 解决上面的问题
    * 将工厂创建好了以后放入到ServletContext域中.使用工厂的时候,从ServletContext中获得.
        * ServletContextListener:用来监听ServletContext对象的创建和销毁的监听器.
        * 当ServletContext对象创建的时候:创建工厂 , 将工厂存入到ServletContext


3. Spring整合Web项目
    * 引入spring-web-4.2.4.RELEASE.jar包
    * 配置监听器
       
 <!-- 配置Spring的核心监听器: -->
         <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
         </context-param>



4. 修改servlet的代码
    * 从ServletContext中获得工厂
    * 具体代码如下
        ServletContext servletContext = ServletActionContext.getServletContext();
        // 需要使用WEB的工厂的方式
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        CustomerService cs = (CustomerService) context.getBean("customerService");
        cs.save();  
________________________________________
Spring第二讲
IOC(注解方式)
IOC之注解的快速入门
1. 步骤一:导入注解开发所有需要的jar包
    * 引入IOC容器必须的6个jar包
    * 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包


2. 步骤二:创建对应的包结构,编写Java的类
    * UserService           -- 接口
    * UserServiceImpl       -- 具体的实现类


3. 步骤三:在src的目录下,创建applicationContext.xml的配置文件,然后引入约束。注意:因为现在想使用注解的方式,那么引入的约束发生了变化
    * 需要引入context的约束,具体的约束如下
 < 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">
</beans>
4. 步骤四:在applicationContext.xml配置文件中开启组件扫描
    * Spring的注解开发:组件扫描
       <context:component-scan base-package="com.itheima "/> 
执行了两步:开启注解   扫描包
       意思是扫描com.itheima包下所有的内容
注意:不加扫描注解也可以用,只是启动时没有创建对象
5. 步骤五:在ServiceImpl的实现类上添加注解
@Component("service")   -- 相当于在XML的配置方式中 
<bean id="service " class="全限定名"/>
可省略为: @Component 等于 @Component("类名")


Bean管理的常用注解

@Component[kəm'pəʊnənt]:组件.(作用在pojo类上)

Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
    * @Controller         -- 作用在WEB层
    * @Service           -- 作用在业务层
    * @Repository      -- 作用在持久层

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


Spring中基于Java的配置@Configuration和@Bean用法


属性注入的注解

(说明:使用注解注入的方式,可以不用提供set方法)

    * 如果是注入的普通类型,可以使用value注解
        * @Value            -- 用于注入普通类型(基本数据类型和字符串)


    * 如果注入的是对象类型,使用如下注解:
        * @Autowired        -- 默认按类型进行自动装配(去IOC容器去找他注解的对象实现的类,自动装配注入)
        * 如果想按名称注入:    @Qualifier    -- 强制使用名称注入

必须一块使用: 

                        @Autowired

                        @Qualifier("dao")

    * @Resource             -- 相当于@Autowired和@Qualifier一起使用
        * 强调:Java提供的注解
        * 属性使用name属性


Bean的作用范围和生命周期的注解

1. Bean的作用范围注解
    * 注解为@Scope[skəʊp] (value="prototype"),作用在类上。值如下:
        * singleton['sɪŋg(ə)lt(ə)n]     -- 单例,默认值
        * prototype['prəʊtətaɪp]      -- 多例


2. Bean的生命周期的配置(了解)
    * 注解如下(作用在方法上):
        * @PostConstruct    -- 相当于init-method(当bean被载入到容器的时候调用)
        * @PreDestroy       -- 相当于destroy-method


整合JUnit单元测试

1. 为了简化了JUnit的测试,使用Spring框架也可以整合测试
2. 具体步骤
    * 要求:必须先有JUnit的环境(即已经导入了JUnit4的开发环境)!!


    * 步骤一:在程序中引入:spring-test.jar
    * 步骤二:在具体的测试类上添加注解
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration("classpath:applicationContext.xml")   
多个  @ContextConfiguration(locations = {"classpath:/spring/*.xml", "classpath:/spring/*.xml })   
           作用在类上,可以加载配置文件.第二个注解是加载配置文件



AOP的概述



1. 什么是AOP的技术?
    * 在软件业,AOP为Aspect  Oriented  Programming 的缩写,意为:面向切面编程
    * AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构
    * AOP最早由AOP联盟的组织提出的,制定了一套规范.Spring将AOP思想引入到框架中,必须遵守AOP联盟的规范
    * 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
    * AOP是OOP(面向对象)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型
    * 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率


2. AOP:面向切面编程.(思想.---解决OOP遇到一些问题)
3. AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)


4. 为什么要学习AOP
    * 可以在不修改源代码的前提下,对程序进行增强!!   

AOP的底层实现

1. Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种
    1. 基于JDK的动态代理
        * 必须是面向接口的,只有实现了具体接口的类才能生成代理对象


    2. 基于CGLIB动态代理
        * 对于没有实现了接口的类,也可以产生代理,产生这个类的子类的方式


2. Spring的传统AOP中根据类是否实现接口,来采用不同的代理方式
    1. 如果实现类接口,使用JDK动态代理完成AOP
    2. 如果没有实现接口,采用CGLIB动态代理完成AOP
________________________________________
JDK的动态代理(代码了解,理解原理)
1. 使用Proxy类来生成代理对象的一些代码如下:
   
 public class MyProxyUtils {
	public static Service getProxy(final Service s){
		Service service = (Service) Proxy.newProxyInstance(MyProxyUtils.class.getClassLoader() ,s.getClass().getInterfaces() ,new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method , Object[] args ) throws Throwable {
				System.out.println("代理执行了执行了");
				if (method.getName().equals("save")) {
					s.save();
				}
				return null;
			}
		} );
		
		return service;
	}
} 
MyProxyUtils.class.getClassLoader()  : 当前类的类加载器

s.getClass().getInterfaces() :被装饰者实现的所有接口

new InvocationHandler() {}接口的实现类,是个匿名内部类;

Method method :通过反射传来的被代理对象的方法对象,用来实现代理功能

 Object[] args 通过反射传来的被代理对象的方法对象,被代理对象传来方法对象的参数



使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath :applicationContext.xml")
public class Demo {
@Resource(name="service")
private Service s;
   @Test
   public void method(){
     Service proxy = MyProxyUtils.getProxy(s);
    proxy.save();
   }
}


CGLIB的代理技术(代码了解)
当被代理对象没有实现接口是spring框架会默认使用CGLIB实现代理
1. 引入CBLIB的开发包
    * 如果想使用CGLIB的技术来生成代理对象,那么需要引入CGLIB的开发的jar包,在Spring框架核心包中已经引入了CGLIB的开发包了。所以直接引入Spring核心开发包即可!


2. 编写相关的代码
 
 public static ServiceImpl2 getcglibProxy(){
//创建cglib核心类
Enhancer enhancer = new Enhancer();
// 设置父类
enhancer.setSuperclass(ServiceImpl2.class);
// 设置回调函数
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args,
                    MethodProxy methodProxy) throws Throwable {
                if("save".equals(method.getName())){
                    // 记录日志
                    System.out.println("记录日志了...");
                }
                return methodProxy.invokeSuper(obj, args);
            }
        });
        // 生成代理对象
        ServiceImpl2 proxy = (ServiceImpl2) enhancer.create();
        return proxy;
}


Spring基于AspectJ的AOP的开发



AOP的相关术语
1. Join point (连接点)   -- 所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(被代理对象的所有方法)
2. Point cut(切入点)        -- 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义(被代理对象加强的方法)
3. Advice (通知/增强)    -- 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
4. Introduction (引介) -- 引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field
5. Target  (目标对象)     -- 代理的目标对象
6. Weaving(织入)      -- 是指把增强应用到目标对象来创建新的代理对象的过程(生成代理对象的过程)
7. Proxy(代理)        -- 一个类被AOP织入增强后,就产生一个结果代理类
8. Aspect (切面)           -- 是切入点和通知的结合,以后咱们自己来编写和配置的
 
AspectJ的XML方式完成AOP的开发
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包
    * 先引入Spring框架开发的基本开发包
    * 再引入Spring框架的AOP的开发包
        * spring的传统AOP的开发的包
            * spring-aop-4.2.4.RELEASE.jar
            * com.springsource.org.aopalliance -1.0.0.jar


        * aspectJ的开发包
            * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
            * spring-aspects-4.2.4.RELEASE.jar


2. 步骤二:创建Spring的配置文件,引入具体的AOP的schema约束
    <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>




3. 步骤三:创建包结构,编写具体的接口和实现类
    * com.itheima.demo2
        * CustomerDao           -- 接口
        * CustomerDaoImpl       -- 实现类


4. 步骤四:将目标类配置到Spring中
    <bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>


5. 步骤五:定义切面类
    public class MyAspectXml {
        // 定义通知
        public void log(){
            System.out.println("记录日志...");
        }
    }


6. 步骤六:在配置文件中定义切面类
    <bean id="myAspectXml" class="com.itheima.demo3.MyAspectXml"/>


7. 步骤七:在配置文件中完成aop的配置
<bean id="service" class="com.itheima.service.ServiceImpl"/>
   <aop:config>
        <!-- 引入切面类 -->
        <aop:aspect ref="service">
            <!-- 定义通知类型:切面类的方法和切入点的表达式 -->
            <aop:before method="log" pointcut="execution(public * com.itheima.demo3.CustomerDaoImpl.save(..))"/>
        </aop:aspect>
    </aop:config>


   


切入点的表达式

1. 再配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(..)),具体展开如下:
    * 切入点表达式的格式如下:
         execution([修饰符] 返回值类型 包名.类名.方法名(参数))


    * 修饰符可以省略不写,不是必须要出现的。
    * 返回值类型是不能省略不写的,根据你的方法来编写返回值。可以使用 * 代替。
    * 包名例如:com.itheima.demo3.BookDaoImpl
        * 首先com是不能省略不写的,但是可以使用 * 代替
        * 中间的包名可以使用 * 号代替
        * 如果想省略中间的包名可以使用 ..  例:*..*


    * 类名也可以使用 * 号代替,也有类似的写法:*DaoImpl
    * 方法也可以使用 * 号代替 例:*()  add*()
    * 参数如果是一个参数可以使用 * 号代替,如果想代表任意参数使用 ..
________________________________________
AOP的通知类型
1. 前置通知
    * 在目标类的方法执行之前执行。
    * 配置文件信息:<aop: before[bɪ'fɔː] method="before" pointcut-ref="myPointcut3"/>
    * 应用:可以对方法的参数来做校验


2. 最终通知
    * 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
    * 在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
    * 应用:例如像释放资源


3. 后置通知
    * 方法正常执行后的通知。       
    * 在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
    * 应用:可以修改方法的返回值


4. 异常抛出通知
    * 在抛出异常后通知
    * 在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/> 
    * 应用:包装异常的信息


5. 环绕通知
    * 方法的执行前后执行。
    * 在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
    * 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。
环绕通知:
public void around(ProceedingJoinPoint joinPoint){
System.out.println("环绕通知1...");
try {
// 手动让目标对象的方法去执行
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知2...");
}


环绕通知使用灵活.其他方式都能用环绕方式实现








. 使用Spring框架的AOP技术对DAO层的功能进行增强

Classpath:    spring特有的
________________________________________
AOP(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包
    * 先引入Spring框架开发的基本开发包
    * 再引入Spring框架的AOP的开发包
        * spring的传统AOP的开发的包
            * spring-aop-4.2.4.RELEASE.jar
            * com.springsource.org.aopalliance-1.0.0.jar


        * aspectJ的开发包
            * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
            * spring-aspects-4.2.4.RELEASE.jar


2. 步骤二:创建Spring的配置文件,引入具体的AOP的schema约束
    <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>


3. 步骤三:创建包结构,编写具体的接口和实现类
    * com.itheima.demo1
        * UserDao           -- 接口
        * UserDaoImpl       -- 实现类


4. 步骤四:将目标类配置到Spring中
    <bean id ="userDao1" class="com.itheima.demo.UserDaoImpl"/>
切面类中的注解
5. 步骤五:定义切面类
    * 添加切面和通知的注解
        * @Aspect                   -- 定义切面类的注解


        * 通知类型(注解的参数是切入点的表达式)
             * @Before        -- 前置通知
    * @AfterReturing  -- 后置通知
    *@Around        -- 环绕通知(目标对象方法默认不执行的,需要手动执行)
    * @After          -- 最终通知
    * @AfterThrowing  -- 异常抛出通知


    * 具体的代码如下
        @Aspect 
public class MyAspectAnno {
@Before ("execution(public * com.itheima.demo.UserDaoImpl.save(..))" )
public void log(){
System.out.println("切面执行了..");
}

6. 步骤六:在配置文件中定义切面类
    <bean id ="myAspectAnno" class="com.itheima.demo.MyAspectAnno"/>


7. 步骤七:在配置文件中开启自动代理
    <aop:aspectj-autoproxy />


8. 完成测试
    
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo01 {
@Resource(name="userDao1")
private UserDao userDao; 
@Test
public void method01(){
System.out.println(userDao);
userDao.save();
userDao.update();
}
}
________________________________________
注意测试类不需要在ioc容器中加载,所以不要为了给Deme01中的属性赋值而配置测试类


Spring加载properties配置文件

 Spring中有个<context:property-placeholder location=""/>标签,可以用来加载properties配置文件,location是配置文件的路径,我们现在在工程目录的src下新建一个conn.properties文件,里面写上上面dataSource的配置:

[plain]  view plain  copy
  1. dataSource=com.mchange.v2.c3p0.ComboPooledDataSource  
  2. driverClass=com.mysql.jdbc.Driver  
  3. jdbcUrl=jdbc\:mysql\://localhost\:3306/shop  
  4. user=root  
  5. password=root  
        现在只需要在beans.xml中做如下修改即可:

[html]  view plain  copy
  1. <context:property-placeholder location="classpath:conn.properties"/><!-- 加载配置文件 -->  
  2.   
  3. <!-- com.mchange.v2.c3p0.ComboPooledDataSource类在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->  
  4.  <bean id="dataSource" class="${dataSource}"> <!-- 这些配置Spring在启动时会去conn.properties中找 -->  
  5.     <property name="driverClass" value="${driverClass}" />  
  6.     <property name="jdbcUrl" value="${jdbcUrl}" />  
  7.     <property name="user" value="${user}" />  
  8.     <property name="password" value="${password}" />  
  9.  </bean>  
        <context:property-placeholder location=""/>标签也可以用下面的<bean>标签来代替,<bean>标签我们更加熟悉,可读性更强:

[html]  view plain  copy
  1. <!-- 与上面的配置等价,下面的更容易理解 -->  
  2. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.     <property name="locations"> <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->  
  4.         <array>  
  5.             <value>classpath:conn.properties</value>  
  6.         </array>  
  7.     </property>  
  8. </bean>  
        虽然看起来没有上面的<context:property-placeholder location=""/>简洁,但是更加清晰,建议使用后面的这种。但是这个只限于xml的方式,即在beans.xml中用${key}获取配置文件中的值value。


通过注解方式加载properties文件

详细配置请点这里

        还有一种就是通过注解的方式,在java代码中使用@Value注解来加载配置文件中的值。

        我们来看一个例子:假如我们要在程序中获取某个文件的绝对路径,我们很自然会想到不能在程序中写死,那么我们也可以卸载properties文件中。还是在src目录下新建一个public.properties文件,假设里面写了一条记录:

[plain]  view plain  copy
  1. filePath=E\:\\web\\apache-tomcat-8.0.26\\webapps\\E_shop\\image  
        如果想在java代码中通过注解来获取这个filePath的话,首先得在beans.xml文件中配置一下注解的方式(加载多个properties):

[html]  view plain  copy
  1. <!-- 第二种方式是使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值 -->  
  2. <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.     <property name="locations"><!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样  
  4.         <array>  
  5.             <value>classpath:public.properties</value>  
  6.             <value>classpath:db.properties</value>  
  7.         </array>  
  8.     </property>  
  9. </bean>  
        现在我们可以在java代码中使用注解来获取filePath的值了:

[java]  view plain  copy
  1. @Component("fileUpload")  
  2. public class FileUploadUtil implements FileUpload {  
  3.       
  4.     private String filePath;  
  5.     @Value("#{prop.filePath}")   
  6.     //@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=filePath的对应的value值  
  7.     public void setFilePath(String filePath) {  
  8.         System.out.println(filePath);  
  9.         this.filePath = filePath;  
  10.     }  

  • 10
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘彦青-Yannis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值