Spring第一天

Spring第一天

整体课程安排(3天+2天):

第一天:Spring框架入门、IoC控制反转的配置管理、Spring Web集成、Spring Junit集成。

第二天:Spring AOP面向切面编程、AspectJ的集成配置、JdbcTemplate工具类。

第三天:Spring声明式事务管理、Spring和Struts2、Hibernate的整合

第四天、第五天:综合练习,熟悉SSH整合开发、jQuery Ajax、分页

第一天的主要内容(IoC相关):

Spring的概述

Spring IoC快速入门(工程环境构建、IoC和DI概念)

Spring IoC容器装配Bean的配置(XML方式)

Spring IoC容器装配Bean的配置(注解方式)

Spring Web集成

Spring Junit集成

学习目标:

掌握:什么是IoC,什么是Di,怎么通过spring装配Bean

了解:web集成和junit集成。

  1. Spring的概述

    1. 什么是Spring

Spring是分层的、JavaSE/EE一站式(full-stack)、轻量级开源框架。

  • JavaEE分层

JavaEE规范的三层结构体系:

  • 表现层(页面数据显示、页面跳转调度),例如jsp/servlet/struts2
  • 业务层(业务处理和功能逻辑、事务控制),例如service
  • 持久层(数据存取和封装、和数据库打交道),例如dao

如图:

  • 一站式

Spring提供了JavaEE各层的解决方案:

表现层:struts1、struts2、Spring MVC

业务层:Ioc、AOP、事务控制

持久层:JdbcTemplate、HibernateTemplate、ORM框架(对象关系映射)整合

  • 轻量级:Spring的出现取代了EJB的臃肿、低效、繁琐复杂、脱离现实的情况. 而且使用spring编程是非侵入式的。
  1. Spring的体系结构

Spring框架是一个分层架构,它包含一系列的功能要素并被分为大约20个模块。这些模块分为Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation和测试部分,如图:

核心容器(Core Container) 包括CoreBeansContextEL模块。

1CoreBeans模块提供了Spring最基础的功能,提供IoC和依赖注入特性。这里的基础概念是BeanFactory,它提供对Factory模式的经典实现来消除对程序性单例模式的需要,并真正地允许你从程序逻辑中分离出依赖关系和配置。

2Context模块基于CoreBeans来构建,它提供了用一种框架风格的方式来访问对象,有些像JNDI注册表。Context封装包继承了beans包的功能,还增加了国际化(I18N,事件传播,资源装载,以及透明创建上下文,例如通过servlet容器,以及对大量JavaEE特性的支持,如EJBJMX。核心接口是ApplicationContext

3Expression Language,表达式语言模块,提供了在运行期间查询和操作对象图的强大能力。支持访问和修改属性值,方法调用,支持访问及修改数组、容器和索引器,命名变量,支持算数和逻辑运算,支持从Spring 容器获取Bean,它也支持列表投影、选择和一般的列表聚合等。

数据访问/集成部分(Data Access/Integration)

1JDBC模块,提供对JDBC的抽象,它可消除冗长的JDBC编码和解析数据库厂商特有的错误代码。

2ORM模块,提供了常用的"对象/关系"映射APIs的集成层。其中包括JPAJDOHibernate iBatis 。利用ORM封装包,可以混合使用所有Spring提供的特性进行"对象/关系"映射,如简单声明性事务管理

3OXM模块,提供一个支持ObjectXML进行映射的抽象层,其中包括JAXBCastorXMLBeansJiBXXStream

4JMS模块,提供一套"消息生产者、消费者"模板用于更加简单的使用JMSJMS用于用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。

5Transaction模块,支持程序通过简单声明性事务管理,只要是Spring管理对象都能得到Spring管理事务的好处,即使是POJO,也可以为他们提供事务。

Web

1Web模块,提供了基础的web功能。例如多文件上传、集成IoC容器、远程过程访问、以及Web Service支持,并提供一个RestTemplate类来提供方便的Restful services访问

2Web-Servlet模块,提供了Web应用的Model-View-ControllerMVC)实现。Spring MVC框架提供了基于注解的请求资源注入、更简单的数据绑定、数据验证等及一套非常易用的JSP标签,完全无缝与Spring其他技术协作。

3Web-Struts模块,提供了对Struts集成的支持,这个功能在Spring3.0里面已经不推荐了,建议你迁移应用到使用Struts2.0SpringMVC

4Web-Portlet模块,提供了在Portlet环境下的MVC实现

AOP

1AOP模块,提供了符合AOP 联盟规范的面向方面的编程实现,让你可以定义如方法拦截器和切入点,从逻辑上讲,可以减弱代码的功能耦合,清晰的被分离开。而且,利用源码级的元数据功能,还可以将各种行为信息合并到你的代码中

2Aspects模块,提供了对AspectJ的集成。

3Instrumentation模块,提供一些类级的工具支持和ClassLoader级的实现,可以在一些特定的应用服务器中使用。

Test

1Test模块,提供对使用JUnitTestNG来测试Spring组件的支持,它提供一致的ApplicationContexts并缓存这些上下文,它还能提供一些mock对象,使得你可以独立的测试代码。

  1. Spring的核心

IoC(Inverse of Control 控制反转): 将对象创建权利交给Spring工厂进行管理。比如说:Book book = new Book();

Book book2 = Spring工厂.getBook();

AOP(Aspect Oriented Programming 面向切面编程),基于动态代理的功能增强方式。

今天主要学习IoC

  1. Spring的优点

Spring 出现为了解决JavaEE 实际问题

(1)方便解耦,简化开发

Spring就是一个大工厂,它可以将所有对象创建和依赖关系维护,交给Spring管理

(2)AOP编程的支持

Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

(3)声明式事务的支持

只需要通过配置就可以完成对事务的管理,而无需手动编程

(3)方便程序的测试

Spring对Junit4支持,可以通过注解方便的测试Spring程序

(5)方便集成各种优秀框架

Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持

(6)降低JavaEE API的使用难度

Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

关于框架的特性,我们也会俗称Spring为开发架构的粘合剂

  1. Spring IoC快速入门

Spring核心内容的基本开发步骤:

  • 下载开发包,导入jar包
  • 编写代码(基础代码和调用代码)
  • 编写配置文件(XML)
  1. Spring的开发包

开发包的下载

Spring官方:http://spring.io/

下载网址:http://repo.spring.io/libs-release-local/org/springframework/spring/

官方最新版本:

不同系列版本对开发环境的最低需求:

本次课程中我们采用的版本是:3.2.x的版本(企业主流版本,框架整合也需要对应版本jar):

Spring3.2版本开发包目录结构:

第一步:其中docs:

查看docsspring-framework-referencepdf中的spring-framework-reference.pdf规范文档中基本内容了解:

第三章 核心技术 (IoC和AOP)

第四章 数据访问 持久层 (JdbcTemplate、声明式事务管理)

第五章 表现层 SpringMVC ,整合Struts2

第六章 集成 零散技术 , 需要在项目实战讲解

第二步:其中jar:

打开spring-framework-3.2.0.RELEASE-distspring-framework-3.2.0.RELEASElibs

开发过程中还需要其他开源技术框架依赖Jar包集(dependencies,作用是方便依赖的其他技术的jar的导入):

第三步:其中schema:

打开:spring-framework-3.2.0.RELEASE-distspring-framework-3.2.0.RELEASEschemaeans,查看spring配置文件的规范约束

  1. 开发环境测试搭建(Jar的导入)

第一步:新建Web工程Spring3_day01,

第二步:导入jar包

1.Spring项目的核心容器的最基本Jar包(4个):

打开spring-framework-3.2.0.RELEASE-distspring-framework-3.2.0.RELEASElibs,可以看到

2.Spring框架所需的日志包(2个,依赖jar库中找):

默认采用apache commons-logging(JCL)日志框架+log4j的日志实现,还需要添加log4j的配置文件。

打开:spring-framework-3.0.2.RELEASE-dependencies

找到:org.apache.commons文件夹和org.apache.log4j文件夹

关于java日志的补充阅读:

Java的常见的日志系统有log4J,jdk-jul,logback等,这些日志系统各自独立,编程方式也不一致。如果你一个系统要用到多个框架库,而这些框架库又可能要用不同的日志系统,那么你一个系统中就得使用多套日志系统,那多套日志系统如何融合在一起是个很大的问题。

那么如何解决呢?进行抽象,抽象出一个接口层,对每个日志实现都适配或者转接,这样这些提供给别人的库都直接使用抽象层即可,不需要关注具体的日志实现。常见的日志抽象出来的框架有Apache commons-logging和slf4j

这里有个故事:最新是开源社区提供了commons-logging抽象,被称为JCL日志框架,出色地完成了兼容主流的日志实现(log4j、JUL、simplelog),基本一统江湖,就连顶顶大名的spring也是依赖了JCL。

看起来事物确实是美好,但是美好的日子不长,接下来另一个优秀的日志框架slf4j的加入导致了更加混乱的场面。比较巧的是slf4j的作者(Ceki Gülcü)就是log4j的作者,他觉得JCL不够优秀,所以他要自己搞一套更优雅的出来,于是slf4j日志体系诞生了,并为slf4j实现了一个亲子——logback,确实更加优雅。

但是由于之前很多代码库已经使用JCL,虽然出现slf4j和JCL之间的桥接转换,但是集成的时候问题依然多多,对很多新手来说确实会很懊恼,因为比单独的log4j时代"复杂"多了,可以关注下这个,抱怨声确实很多。到此本来应该完了,但是Ceki Gülcü觉得还是得回头拯救下自己的"大阿哥"——log4j,于是log4j2诞生了,同样log4j2也参与到了slf4j日志体系中。日志体系的江湖又将面临血雨腥风的混乱了。

hibernate-àjboss-àhibernate3(slf4j+log4j)àhibernate4:jboss logging(整合其他的日志)

导好的jar:

添加log4j的日志文件:

添加log4j.properties文件放置到src下。

### direct log messages to stdout ###

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.err

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

 

### direct messages to file mylog.log ###

log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=c:mylog.log

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

 

### set log levels - for more verbose logging change 'info' to 'debug' ###

 

log4j.rootLogger=info, stdout

  1. 传统方式业务代码编写(业务层、数据持久层)

采用的示例业务是模拟用户登录操作。

第一步:创建包cn.itcast.spring.a_quickstart

第二步:创建dao

1:创建接口IUserDao

package cn.itcast.spring.a_quickstart;

//用户的dao

public interface IUserDao {

    //向数据查询数据,根据用户名和密码

    public void findByUsernameAndPassword();

}

2:创建IUserDao接口的实现类UserDaompl

package cn.itcast.spring.a_quickstart;

//dao的实现类

public class UserDaoImpl implements IUserDao {

    @Override

    public void findByUsernameAndPassword() {

        System.out.println("UserDaoImpl-dao层被调用了");

    }

}

第三步:创建service

1:创建接口IUserService

package cn.itcast.spring.a_quickstart;

//业务层

public interface IUserService {

    //登录

    public void login();

}

2:创建IUserService接口的实现类UserServiceImpl

package cn.itcast.spring.a_quickstart;

 

//业务层实现

public class UserServiceImpl implements IUserService{

 

    public void login() {

        System.out.println("UserServiceImpl-service层被调用了。。。");

        //实例化dao

        //传统方式

        IUserDao userDao = new UserDaoImpl();

        userDao.findByUsernameAndPassword();

    }

}

第四步:测试

创建SpringTest类进行测试:

public class SpringTest {

    //测试

    @Test

    public void test(){

        //创建service的示例

        IUserService userService = new UserServiceImpl();

        userService.login();

        

    }

    

}

控制台:

【思考分析】

存在问题:代码过于耦合,上层代码过度依赖于下一层代码的实现:

例如:UserDao userDao = new UserDaoImpl();

如果要更换实现类,或者实现类换一个名字,此时代码会报错,必须要修改原来的业务代码!

传统方式:

怎么解决类与类之间如此密切的耦合问题呢?

  1. IoC控制反转的实现

采用IoC(Inverse of Control,控制反转)的思想解决代码耦合问题。

简单的说就是引入工厂(第三者),将原来在程序中手动创建管理的依赖的UserDaoImpl对象,交给工厂来创建管理。

IoC方式:

步骤一:提供userDAO实例对象的工厂

UserDAOFactory.java:

public class UserDAOFactory {

    

    //提供获取对象的方法

    public UserDAOImpl getUserDAO(){

        //返回实例对象

        return new UserDAOImpl ();

    }

    

}

步骤二: 修改UserServiceImpl中获得对象的方式

UserServiceImpl.java:

public class UserServiceImpl implements IUserService{

 

    public void login() {

         System.out.println("UserServiceImpl-service层方法调用了");

        

         //调用dao层的方法

//         IUserDAO userDAO = new UserDAOImpl();

//         userDAO.findUserByUsernameAndPassword();

        

         //ioc方式:

         //创建工厂,利用工厂提供依赖的对象

         UserDAOFactory userDAOFactory = new UserDAOFactory();

         UserDAOImpl userDAO = userDAOFactory.getUserDAO();

         userDAO.findUserByUsernameAndPassword();

        

    }

 

}

发现问题:工厂方法仍然需要返回具体类型的实例对象,存在代码耦合

解决方案:使用反射技术传入具体类型的类字符串生产对象的实例:

UserDAOFactory.java:

//利用反射技术生产具体类型的实例对象

    public Object getBean(){

        Object bean = null;

        try {

            //传入类字符串,生产对象实例

            bean = Class.forName("cn.itcast.spring.a_quickstart.UserDAOImpl").newInstance();

        } catch (Exception e) {

            e.printStackTrace();

        }

        //返回具体类型的对象类型实例

        return bean;

    }

UserServiceImpl.java:

//使用反射方法获取对象

         IUserDAO userDAO = (IUserDAO) userDAOFactory.getBean();

         userDAO.findUserByUsernameAndPassword();

发现问题:类字符串是固定的,怎么动态的传入不同的类字符串呢?

解决方案: 使用xml配置文件动态传入类字符串

IoC底层实现:工厂(设计模式)+反射(机制) + 配置文件(xml)。

  1. Spring核心配置文件的编写

IoC控制反转的理解和实现

步骤一:在src下建立applicationContext.xml (位置:applicationContext.xml文件放置到任何目录都可以,习惯上放在src目录或者 WEB-INF目录)

步骤二:参考规范文档配置xml的头信息:beans schema

文档位置:spring3_day01/课前资料/spring-framework-3.2.0.RELEASE/docs/spring-framework-reference/html/xsd-config.html

找到下列章节的示例,拷贝到工程中即可:

<?xml version="1.0" encoding="UTF-8"?>

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

步骤三:配置本地提示:(联网的情况下会Myeclipse会自动下载关联相关约束文件)

步骤四:配置applicationContext.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"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

 

<!-- bean: spring工厂创建的一个对象(反射机制)

    id/name:对象的名字,可以用来引用或者获取对象, 一般为类名或接口名称的首字母小写

    class:要创建的对象类型的类字符串,类名全路径

-->

<bean id="userDAO" class="cn.itcast.spring.a_quickstart.UserDAOImpl" />

 

</beans>

  1. 通过Spring的工厂获取Bean完成相关操作

在程序中创建spring工厂对象, 通过工厂对象加载spring的xml配置文件,生产配置文件中配置 的bean对应的对象

UserServiceImpl.java:

//spring配置方式,创建spring工厂,加载spring配置文件

     ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

     //spring工厂中获取对象,通过beanid/name

     IUserDAO userDAO = (IUserDAO) ac.getBean("userDAO");

     userDAO.findUserByUsernameAndPassword();

运行测试:

发现问题:该方式虽然解决了类与类之间的耦合关系,但却需要在获取对象的时候创建spring工厂,有没有更方便获取对象的依赖的方法呢?

  1. DI依赖注入的实现

DI:Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件(简单的说,可以将另外一个bean对象动态的注入到另外一个bean中。)

【面试题】IoC和DI的区别 ?

DI和IoC是同一件事情,都是将对象控制权交给第三方(Spring)管理,只是站在不同角度而已。

IoC:(参考:spring3_day1_课前资料参考图书IoC框架(依赖注入 DI).mht)

回顾之前的代码:

Di的做法是:由Spring容器创建了Service、Dao对象,并且在配置中将Dao传入Servcie,那么Service对象就包含了Dao对象的引用。

步骤一:将service对象也交给spring容器管理

applicationContext.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"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

 

<!-- bean: spring工厂创建的一个对象(反射机制)

    id/name:对象的名字,可以用来引用或者获取对象, 一般为类名或接口名称的首字母小写

    class:要创建的对象类型的类字符串,类名全路径

-->

<bean id="userDAO" class="cn.itcast.spring.a_quickstart.UserDAOImpl" />

 

<bean id ="userService" class="cn.itcast.spring.a_quickstart.UserServiceImpl">

    <!-- 注入对象 -->

    <!-- property 根据类中的setter方法进行属性注入 -->

    <!-- name:setter方法的后缀小写,比如setXxx 对应的namexxx -->

    <!-- ref:引用哪一个bean(对象),值为beanid/name -->

    <property name="userDAO" ref="userDAO" />

</bean>

 

 

</beans>

步骤二:在程序中定义属性提供setter方法:

UserServiceImpl.java

public class UserServiceImpl implements IUserService{

    

    //定义属性

    private IUserDAO userDAO;

 

    public void setUserDAO(IUserDAO userDAO) {

        this.userDAO = userDAO;

    }

 

    public void login() {

         System.out.println("UserServiceImpl-service层方法调用了");

        

         //ioc:依赖注入

         userDAO.findUserByUsernameAndPassword();

        

    }

步骤三:测试运行,此时获取对象必须从spring工厂获取(在spring容器配置中才有依赖注入,自己创建的对象没有注入依赖关系)

public class SpringTest {

    //测试

    @Test

    public void test(){

        //创建service的示例

        //IUserService userService = new UserServiceImpl();

        //userService.login();

        

        //创建spring工厂,获取spring管理的对象

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        IUserService userService = (IUserService) ac.getBean("userService");

        

        userService.login();

        

    }

    

}

运行结果:

小结:

IOC:控制反转,将对象创建管理的权利交给spring容器,获取对象通过spring工厂创建

DI:在spring容器中创建管理多个对象,通过 property标签将对象注入到需要依赖的对象中

  1. Spring的工厂(了解)

ApplicationContext直译为应用上下文,是用来加载Spring框架配置文件,来构建Spring的工厂对象,它也称之为Spring容器的上下文对象,也称之为Spring的容器。

ApplicationContext 只是BeanFactory(Bean工厂,Bean就是一个java对象) 一个子接口:

为什么不直接使用顶层接口对象来操作呢?

* BeanFactory 采取延迟加载,第一次getBean时才会初始化Bean

* Beanfactory的用法:

BeanFactory ac = new XmlBeanFactory(new FileSystemResource("D:\applicationContext.xml"));

* ApplicationContext是对BeanFactory扩展,提供了更多功能

国际化处理

事件传递

Bean自动装配

各种不同应用层的Context实现

ApplicationContext 更加强大, 所以现在开发基本没人使用BeanFactory。

提示:后面还有个FactoryBean,注意区别。

【扩展】

Bean获取的两种方式:

    @Test

    public void getBean(){

        

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        

        //获取bean的两种方式

        //1.通过spring容器中beanid/name获取

        //IUserService userService = (IUserService) ac.getBean("userService");

        

        //2.根据bean的类型或者bean接口的类型获取,一般使用接口类型

        IUserService userService = (IUserService) ac.getBean(IUserService.class);

        

        userService.login();

        

    }

常用根据名称获取(id/name),即第一种方式,使用spring容器中的标识获取对象

如果根据类型获取,配置了多个类型的话,则抛出异常:

applicationContext.xml:

<bean id ="userService1" class="cn.itcast.spring.a_quickstart.UserServiceImpl">

    

    <property name="userDAO" ref="userDAO" />

</bean>

 

<bean id ="userService" class="cn.itcast.spring.a_quickstart.UserServiceImpl">

    <!-- 注入对象 -->

    <!-- property 根据类中的setter方法进行属性注入 -->

    <!-- name:setter方法的后缀小写,比如setXxx 对应的namexxx -->

    <!-- ref:引用哪一个bean(对象),值为beanid/name -->

    <property name="userDAO" ref="userDAO" />

</bean>

抛出异常

  1. IoC容器装配Bean_基于XML配置方式

    1. 实例化Bean的四种方式 (了解)

创建包:cn.itcast.spring.b_xmlnewbean

第一种方式 无参数构造器 最常用

第一步:创建Bean1.java

//1。默认构造器(spring在创建bean的时候自动调用无参构造器来实例化,相当于new Bean1())

public class Bean1 {

}

第二步:在spring容器applicationContext.xml中配置

<!-- 实例化 bean的四种方式 -->

    <!-- 1.默认构造器实例化对象 -->

    <bean id ="bean1" class="cn.itcast.spring.b_xmlnewbean.Bean1" />

第三步:创建测试类获取bean对象

SpringTest.java:

public class SpringTest {

    

    @Test

    public void test(){

        //创建spring工厂

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        //1.默认构造器获取bean对象

        Bean1 bean1 = (Bean1) ac.getBean("bean1");

        System.out.println(bean1);

    }

 

}

运行结果:

【错误演示】:

public class Bean1 {

    

    //错误演示

    private String name;

    

    public Bean1(String name) {

        this.name = name;

    }

 

}

运行报错:

第二种方式: 静态工厂方法

第一步:创建Bean2.java

//1.静态工厂方法构造:用来在初始化bean2的时候,可以初始化其他的东西

public class Bean2 {

}

第二步:创建Bean2Factory.java类

//静态工厂

public class Bean2Factory {

    

    //静态方法,用来返回对象的实例

    public static Bean2 getBean2(){

        //在做实例化的时候,可以做其他的事情,即可以在这里写初始化其他对象的代码

        //Connection conn....

        return new Bean2();

    }

 

}

第三步:Spring的容器applicationContext.xml

<!-- 2.静态工厂获取实例化对象 -->

    <!-- class:直接指定到静态工厂类, factory-method: 指定生产实例的方法, spring容器在实例化工厂类的时候会自动调用该方法并返回实例对象 -->

    <bean id = "bean2" class="cn.itcast.spring.b_xmlnewbean.Bean2Factory" factory-method="getBean2" />

 

第四步:测试类进行测试

@Test

    public void test(){

        //先构建实例化获取spring的容器(工厂、上下文)

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.静态工厂

        Bean2 bean2=(Bean2) applicationContext.getBean("bean2");

        System.out.println(bean2);

    }

第三种方式: 实例工厂方法

第一步:创建Bean3.java

//第三种bean,实例工厂方式创建

public class Bean3 {

 

}

第二步:创建实例工厂Bean3Factory类

//实例工厂:必须new工厂--》bean

public class Bean3Factory {

    //普通的方法,非静态方法

    public Bean3 getBean3(){

        //初始化实例对象返回

        return new Bean3();

    }

}

第三步:Spring容器的配置:applicationContext.xml

<!-- 3:实例工厂的方式实例化bean -->

    <bean id="bean3Factory" class="cn.itcast.spring.b_xmlnewbean.Bean3Factory"/>

    <!-- factory-bean相当于ref:引用一个bean对象 -->

    <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"/>

第四步:使用测试代码,进行测试:

@Test

    public void test(){

        //先构建实例化获取spring的容器(工厂、上下文)

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //3.实例工厂

        Bean3 bean3=(Bean3) applicationContext.getBean("bean3");

        System.out.println(bean3);

    }

第四种方式:FactoryBean方式。(源码底层用的多)

第一步:创建Bean4.java

public class Bean4 {

 

}

第二步:创建工厂Bean,MyFactoryBean.java,实现FactoryBean<Bean3>的接口

//4。实现FactoryBean接口的方式

//泛型:你要返回什么类型的对象,泛型就是什么

public class Bean4FactoryBean implements FactoryBean<Bean4>{

    //用来获取bean的实例,对象

    public Bean4 getObject() throws Exception {

        //写一些初始化数据库连接等等其他代码

        return new Bean4();

    }

    public Class<?> getObjectType() {

        return null;

    }

    public boolean isSingleton() {

        return false;

    }

}

FactoryBean提供getObject方法,返回目标类型对象.

第三步:spring容器中的配置

<!-- 4.实现FactoryBean接口实例化对象 -->

    <!-- spring在实例化Bean4Factory的时候会判断是否实现了FactoryBean接口,如果实现了就调用getObject方法返回实例 -->

    <bean id="bean4" class="cn.itcast.spring.b_xmlnewbean.Bean4Factory" />

四种方式:

第一种:最常用

  1. 第三种:一些框架初始化的时候用的多。

    第四种:spring底层用的多。

    【面试题】 BeanFactory和FactoryBean的区别?

    BeanFactory:是一个工厂(其实是构建了一个spring上下文的环境,容器),用来管理和获取很多Bean对象。

    FactoryBean:是一个Bean生成工具,是用来获取一种类型对象的Bean,它是构造Bean实例的一种方式。

    1. Bean的作用域

    由spring创建的bean对象在什么情况下有效。

    项目开发中通常会使用:singleton 单例、 prototype多例

    Singleton: 在一个spring容器中,对象只有一个实例。(默认值

    Prototype: 在一个spring容器中,存在多个实例,每次getBean 返回一个新的实例

    建立包:cn.itcast.spring.c_xmlscope

    第一步:创建类SingletonBean.java和PrototypeBean.java

    创建类SingletonBean.java类

    //单例bean

    public class SingletonBean {

        public SingletonBean() {

            System.out.println("SingletonBean:初始化了单例");

        }

    }

    创建类PrototypeBean.java类

    //多例bean

    public class PrototypeBean {

        public PrototypeBean() {

            System.out.println("--PrototypeBean初始化了多例的");

        }

    }

    第二步:定义spring容器,applicationContext.xml:

    <!--

            bean的作用范围

            scope:配置作用范围的,默认值就是singleton单例

         -->

        <!-- 单例 -->

        <!-- <bean id="singletonBean" class="cn.itcast.spring.c_xmlscope.SingletonBean" scope="singleton"/> -->

        <bean id="singletonBean" class="cn.itcast.spring.c_xmlscope.SingletonBean"/>

        <!-- 多例 -->

        <bean id="prototypeBean" class="cn.itcast.spring.c_xmlscope.PrototypeBean" scope="prototype"/>

          

    第三步:测试代码,创建SpringTest.java:

    //newbean的方式

    public class SpringTest {

        

        @Test

        public void test(){

            //先构建实例化获取spring的容器(工厂、上下文)

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

            //目标1:看看多次获取bean的时候,是不是同一个

            //目标2:看看bean什么时候初始化的

            //获取单例的bean:应该是同一个

            //单例:每次从spring容器中获取的对象,是同一个对象

            //单例初始化:是在spring容器初始化的时候,就初始化了

            SingletonBean singletonBean1=(SingletonBean)applicationContext.getBean("singletonBean");

            SingletonBean singletonBean2=(SingletonBean)applicationContext.getBean("singletonBean");

            System.out.println(singletonBean1);

            System.out.println(singletonBean2);

            //获取多例的bean:

            //多例:每次从spring容器中获取的对象,不是同一个对象

            //多例初始化:是在getBean的时候初始化,相当于每次getbean就是在new Bean()

            PrototypeBean prototypeBean1=(PrototypeBean)applicationContext.getBean("prototypeBean");

            PrototypeBean prototypeBean2=(PrototypeBean)applicationContext.getBean("prototypeBean");

            System.out.println(prototypeBean1);

            System.out.println(prototypeBean2);

            

        }

     

    }

    运行查看,测试结果:

    【注意】

    单例是默认值,如果需要单例对象,则不需要配置scope。

    1. Bean的生命周期

    通过spring工厂,可以控制bean的生命周期。

    1. 在xml配置Bean的初始化和销毁方法

    通过 init-method属性 指定初始化后的调用方法

    通过 destroy-method属性 指定销毁对象前的方法

    创建包cn.itcast.spring.d_xmllifecycle

    第一步:创建LifeCycleBean,指定一个init的方法,和一个destroy的方法。

    //测试生命周期过程中的初始化和销毁bean

    public class LifeCycleBean {

     

        //定义构造方法

        public LifeCycleBean() {

            System.out.println("LifeCycleBean构造器调用了");        

        }

        

        //初始化后自动调用方法:方法名随意,但也不能太随便,一会要配置

        public void init(){

            System.out.println("LifeCycleBean-init初始化时调用");

        }

        

        //bean销毁时调用的方法

        public void destroy(){

            System.out.println("LifeCycleBean-destroy销毁时调用");

        }

     

    }

    第二步:Spring的核心容器,applicationContext.xml的配置

    <!-- 生命周期调用的两个方法

        init-method:初始化时(后)调用的,bean中的共有方法即可

        destroy-method:销毁时(前)被调用的。

        -->

        <bean id="lifeCycleBean" class="cn.itcast.spring.d_xmllifecycle.LifeCycleBean" init-method="init" destroy-method="destroy" />

        

    第三步:SpringTest.java测试代码:

    @Test

        public void test(){

            //先获取spring的容器,工厂,上下文

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

            //对于单例此时已经被初始化

            //获取bean

            LifeCycleBean lifeCycleBean=(LifeCycleBean) applicationContext.getBean("lifeCycleBean");

            System.out.println(lifeCycleBean);

            //为什么没有销毁方法调用。

            //原因是:使用debug模式jvm直接就关了,spring容器还没有来得及销毁对象。

            //解决:手动关闭销毁spring容器,自动销毁单例的对象

            ((ClassPathXmlApplicationContext)applicationContext).close();

            

        }

    测试时查看控制台打印,发现销毁方法没有执行。

    原因:销毁方法的执行必须满足两个条件:

    1. 单例(singleton)的bean才会可以手动销毁。
    2. 必须手动关闭容器(调用close的方法)时,才会执行手动销毁的方法。

     

    【扩展】

    关于数据(一些属性等等)的初始化的。

  • 直接赋值
  • 在构造器中初始化
  • 使用单独的初始化的方法

//测试生命周期过程中的初始化和销毁bean

public class LifeCycleBean {

    //成员变量

    private String name="Tom";//1.赋值成员变量

      

    

    public LifeCycleBean() {

        System.out.println("LifeCycleBean构造器调用了");

    }

 

//2.构造器初始化

    public LifeCycleBean(String name) {

        System.out.println("LifeCycleBean构造器调用了-赋值");

        this.name="Rose";

    }

    

    //初始化后自动调用方法:方法名随意,但也不能太随便,一会要配置

    public void init(){

        System.out.println("LifeCycleBean-init初始化时调用");

        //3.单独的初始化方法来初始化数据

        this.name="Jack";

    }

    

    //bean销毁时调用的方法

    public void destroy(){

        System.out.println("LifeCycleBean-destroy销毁时调用");

        System.out.println(name);

    }

}

三种方式:根据代码情况任选。

第一种方式:直接的,但耦合性最强。一般用于给默认值的。

第二种方式:使用有参构造来初始化。代码new的时候就能直接将属性初始化,但有点耦合。

第三种方式:使用单独的方法,来初始化。完全解耦,初始化方法专门用来写初始化的一系列代码,构造器,只是用来构造class对象。

  1. 后处理Bean(BeanPostProcessor接口) 了解

后处理Bean也称之为Bean的后处理器,作用是:在Bean初始化的前后,对Bean对象进行增强。它既可以增强一个指定的Bean,也可以增强所有的Bean,底层很多功能(如AOP等)的实现都是基于它的,Spring可以在容器中直接识别调用。

【示例】

要对"所有"的bean的初始化的时候进行增强(打印一句话)

第一步:创建MyBeanPostProcessor类,实现接口BeanPostProcessor

//后处理bean,:用来对bean进行功能增强,可以实现,对所有,或某个bean的初始化进行增强

public class MyBeanPostProcessor implements BeanPostProcessor{

 

    //初始化时(之前)调用的

    //参数1:bean对象,参数2,bean的名字,id、name

    public Object postProcessBeforeInitialization(Object bean, String beanName)

            throws BeansException {

//        System.out.println(beanName+"在初始化前开始增强了");

        //如何只增强一个bean

        if(beanName.equals("lifeCycleBean")){

            System.out.println(beanName+"在初始化前开始增强了");

        }

        return bean;//放行

    }

 

    //初始化时(之后)调用

    public Object postProcessAfterInitialization(Object bean, String beanName)

            throws BeansException {

//        System.out.println(beanName+"在初始化后开始增强了");

        if(beanName.equals("lifeCycleBean")){

            System.out.println(beanName+"在初始化后开始增强了");

        }

        return bean;

    }

 

}

第二步:定义applicationContext.xml文件

<!-- 后处理bean:spring在初始化MyBeanPostProcessor的时候,判断是否实现了BeanPostProcessor,如果实现了,就采用动态代理的方式,对所有的bean对象增强 -->

    <bean class="cn.itcast.spring.d_xmllifecycle.MyBeanPostProcessor"/>

执行任意bean操作的测试,控制台输出:

BeanPostProcessor接口,提供增强途径,在不修改原来代码情况下,增添新的功能!

  1. Bean属性的依赖注入

    1. 属性依赖注入的三种方式

什么是Bean属性的注入?就是对一个对象的属性赋值。有三种方式:

  • 第一种:构造器参数注入
  • 第二种:setter方法属性注入(setter方法的规范需要符合JavaBean规范)
  • 第三种:接口注入

Spring 框架规范中通过配置文件配置的方式,只支持构造器参数注入和setter方法属性注入,不支持接口注入

创建包cn.itcast.spring.e_xmlpropertydi

  1. 构造器参数注入 constructor-arg

【示例】

第一步:构造器参数注入属性值。

创建Car类,定义构造方法

//目标,构造器参数注入,new car直接将参数的值直接赋值

public class Car {

    private Integer id;

    private String name;

    private Double price;

    //有参构造

    public Car(Integer id, String name, Double price) {

        this.id = id;

        this.name = name;

        this.price = price;

    }

    

    //取值要用getter

    public Integer getId(){

        return this.id;

    }

      

    

    @Override

    public String toString() {

        return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";

    }

 

}

第二步:配置applicationContext.xml

<!-- 构造器注入属性的值 -->

    <bean id="car" class="cn.itcast.spring.e_xmlpropertydi.Car">

        <!--constructor-arg:告诉spring容器,要调用有参构造方法了,不再调用默认的构造方法了

        new Car(1,"宝马",99999d)

        参数第一组:定位属性

         * index:根据索引定位属性0表示第一个位置

            * name:根据属性参数名称定位属性

            * type:根据属性数据类型定位属性

        参数第二组:值

            * value:简单的值,字符串

            * ref:复杂的(spring容器创建的bean对象)

        -->

        <!-- <constructor-arg index="0" value="1"/> -->

        <constructor-arg index="0" name="id" value="1"/>

        <!-- <constructor-arg name="name" value="宝马1代"/> -->

        <constructor-arg name="name" >

            <value>宝马2代</value>

        </constructor-arg>

        <constructor-arg type="java.lang.Double" value="99999d"/>

    </bean>

第三步:使用SpringTest.java测试:

@Test

    public void test(){

        //spring容器

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //获取car

        Car car =(Car) applicationContext.getBean("car");

     System.out.println(car);

    }

【补充】

1.定位属性的标签,可以混用

<constructor-arg index="0" name="id" value="1"/>

2.自标签的属性赋值问题,可以使用子标签的value,效果和value属性一样

<constructor-arg name="name" value="宝马1代"/>

等同于

<constructor-arg name="name" >

    <value>宝马2代</value>

</constructor-arg>

  1. setter方法属性注入 property

使用的默认的构造器(new Bean()),但必须提供属性的setter方法,使用setter方法也是企业经常使用的属性注入方式

两步:在类中加入setter方法,在配置文件中使用property

【示例】

第一步:创建Person.java,定义id、name、car属性

/**

* 定义人类

* setter方法属性注入

* 相当于new Person();

*/

public class Person {

    private Integer id;

    private String name;

    private Car car;

    //必须提供setter属性方法

    public void setId(Integer id) {

        this.id = id;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setCar(Car car) {

        this.car = car;

    }

    

    @Override

    public String toString() {

        return "Person [id=" + id + ", name=" + name + ", car=" + car + "]";

    }

 

}

第二步:配置spring容器applicationContext.xml

<!-- setter方法属性注入:调用默认构造器,相当于new Person() -->

    <bean id="person" class="cn.itcast.spring.e_xmlpropertydi.Person">

        <!--

        property:专门进行setter属性注入用的标签 。

            * name:setter方法的属性的名字,例如SetXxx-那么name的属性值为xxx

            * value:简单的值

            * ref:bean的名字,对象的引用

        -->

        <property name="id" value="1001"/>

        <property name="name" value="Tom"/>

        <!-- <property name="car" ref="car"/> --><!--等同于-->

        <property name="car">

            <ref bean="car"/>

        </property>

    </bean>

第三步:使用SpringTest.java测试:

@Test

    public void test(){

        //spring容器

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        

        //获取人

        Person person=(Person)applicationContext.getBean("person");

        System.out.println(person);

    }

【扩展】

  1. <ref>标签的用法:

            <!-- <property name="car" ref="car"/> -->

    <!--等同于-->

            <property name="car">

                <ref bean="car"/>

            </property>

    1. p名称空间的使用-了解

    什么是名称空间?

    作用:Schema区分同名元素。(有点类似于java的包)

    回顾:Xmlns没有前缀是默认的名称空间。

    为简化XML文件的配置,Spring2.5版本开始引入了一个新的p名称空间。简单的说,它的作用是为了简化setter方法属性依赖注入配置的,它不是真正的名称空间。

    它的使用方法:

    p:<属性名>="xxx" 引入常量值

    p:<属性名>_ref="xxx" 引用其它Bean对象

    操作步骤:

    第一步:引入p名称空间

    <beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:p="http://www.springframework.org/schema/p"

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

    第二步:将<property> 子元素 简化为 元素的属性注入

    <!-- 使用p名称空间简化setter方法属性注入 -->

    <!--

    p:name:简单数据类型的属性注入

    P:car-ref:复杂数据类型(bean)的属性注入

    -->

    <bean id="person2" class="cn.itcast.spring.e_xmlpropertydi.Person" p:id="1002" p:name="关羽" p:car-ref="car"/>

    第三步:测试

    @Test

        public void test(){

            //spring容器

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

            Person person2=(Person)applicationContext.getBean("person2");

            System.out.println(person2);

        }

    配置时不需要<property > 子元素,简化了配置 .

    1. spEL表达式的使用 –会用即可

    spEL(Spring Expression Language)是一种表达式语言,它是spring3.x版本的新特性。

    它的作用是:支持在运行时操作和查询对象,其语法类似统一的EL语言,但是SpEL提供了额外的功能,功能更强大。

    什么是EL、OGNL、spEL?

    EL:操作servlet相关的一些对象和相关的值

    OGNL:主要操作struts2值栈

    spEL:操作bean相关的

    语法: #{…} , 引用另一个Bean 、属性、 方法 , 运算

    SpEL表达式的使用功能比较多,Bean操作相关的通常有:

  • #{beanid} 引用Bean(具体对象)
  • #{beanId.属性} 引用Bean的属性
  • #{beanId.方法(参数)} 调用Bean的方法

案例一:配置applicationContext.xml

<!-- spEL表达式 -->

<!-- car.name相当于car.getName() -->

<bean id="person3" class="cn.itcast.spring.e_xmlpropertydi.Person" p:id="#{car.id}" p:name="#{car.name}" p:car="#{car}"/>

如果抛出异常:

需要在Car对象中调用getId和getName的方法,获取id和name的属性值,然后赋值到Person对象name的属性中。

public class Car {

//取值要用getter

    public Integer getId(){

        return this.id;

    }

    

    public String getName(){

        return this.name;

    }

}

案例二:配置applicationContext.xml

<!-- spEL表达式 -->

<!-- car.id相当于car.getId() -->

<bean id="person3" class="cn.itcast.spring.e_xmlpropertydi.Person" p:id="#{1+1}" p:name="#{'Jack'.toUpperCase()}" p:car="#{car}"/>

更多参考 : spring3_day1_课前资料参考图书Spring_表达式语言.pdf

  1. IoC容器装配Bean_基于注解配置方式

    1. Bean的定义(注册) -- 扫描机制

新建web项目:spring3_day01_annotation

第一步

导入jar包,

导入log4j.properties,

导入applicationContext.xml

Spring XML开发和注解开发 导入jar包是相同的

第二步: 编写Service和DAO 的注册

    xml做法 : <bean id="customerService" class="…" />,用<bean>的方式创建对象

    注解做法 : spring2.5引入 @Component 注解 如果放置到类的上面,相当于在spring容器中定义<bean id="" class="">

创建包:cn.itcast.spring.a_ioc

创建类:CustomerService.java类

/**

* @Component注解放置到类上

* 相当于spring容器中定义:<bean id="customerService" class="cn.itcast.spring.a_ioc.CustomerService">

* 其中id属性默认bean的名字是类名的小写

* ——————————————————————————————————————————————————————

* @Component(value="customerService")//自定义bean的名字

* 相当于spring容器中定义:<bean id="customer" class="cn.itcast.spring.a_ioc.CustomerService">

* ——————————————————————————————————————————————————————

*/

@Component(value="customerService")

public class CustomerService {

    

    //保存业务方法

    public void save(){

        System.out.println("CustomerService业务层被调用了。。。");

    }

 

}

2016/10/13:

创建类:
UserServiceImpl.java:

@Component("userService")//相当在配置文件中配置了<bean id ="userService" class.... />

public class UserServiceImpl implements IUserService{

      

 

 

 

    public void login() {

        System.out.println("UserServiceImpl login方法调用了");

         //传统方式

        //new UserDAOImpl().findUserByUserNameAndPassword();

        

    }

 

}

 

UserDAOImpl.java

@Component("userDAO")

public class UserDAOImpl implements IUserDAO {

 

    public void findUserByUserNameAndPassword() {

         System.out.println("UserDAOImpl登录了...");

 

    }

 

}

 

测试:

public class SpringTest {

 

    @Test

    public void test() {

        //构建spring工厂

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

        IUserService userService = (IUserService) ac.getBean("userService");

        userService.login();

 

    }

 

}

 

 

 

 

第三步: 配置注解开启和注解Bean的扫描。配置的示例如下:配置applicationContext.xml

参考:spring-framework-3.2.0.RELEASE/docs/spring-framework-reference/html/xsd-config.html,搜索context关键字即可

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

    

    <!-- 开启spring的注解功能 :让注解有效了,识别注解-->

    <context:annotation-config/>

    <!-- 配置注解扫描

            context:component-scan:专门扫描含有@Component注解的类,自动将其作为bean

            base-package:要扫描包的路径,包含子包,cn.itcast.spring表示子包下的所有类定义注解都有效

            注解扫描配置的时候,会自动开启注解功能

    -->

    <context:component-scan base-package="cn.itcast.spring"/>

      

        

</beans>

 

 

 

 

 

    引入context 名称空间 :

 

 

配置本地提示关联:

 

第四步:测试:

public class SpringTest {

 

    @Test

    public void test(){

        //spring容器

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //获取bean

        CustomerService customerService=(CustomerService) applicationContext.getBean("customerService");

        customerService.save();

        

    }

}

 

 

扩展优化:

1.注解扫描配置

在配置包扫描的时候,spring会自动开启注解功能,所以,注解开启功能可以不配置。

即去掉:<context:annotation-config/>

    因为<context:componet-scan> 具有 <context:annotation-config> 作用 !

 

2.衍生注解的问题

实际开发中,使用的是@Component三个衍生注解("子注解")

子注解的作用:有分层的意义(分层注解)。

Spring3.0为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。

除了@Component外,Spring提供了3个功能基本和@Component等效的注解

功能介绍

@Service用于标注业务层组件、(如Service层)

@Controller用于标注控制层组件(如struts中的action层)

@Repository用于标注数据访问组件,(如DAO层组件)。

而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

 

第一步:

修改CutomerService.java

//@Component(value="customerService")//注释掉

@Service(value="customerService")

public class CustomerService {

    

    //保存业务方法

    public void save(){

        System.out.println("CustomerService业务层被调用了。。。");

    }

 

}

创建CustomerDao.java

//持久层

@Repository("customerDao")

public class CustomerDao {

    

    public void save(){

        System.out.println("CustomerDao层被调用了");

    }

 

}

问题:如果将Dao注入到Service呢?

回顾:如果使用xml的配置,那么可以使用setter方法进行注入

<bean id="" class="">

<property name="" ref=""></property>

</bean>

  1. Bean属性的依赖注入

    1. 简单数据类型依赖注入(了解)

Spring3.0后,提供 @Value注解,可以完成简单数据的注入

//@Component(value="customerService")

@Service(value="customerService")

public class CustomerService {

    //简单类型的成员变量

    @Value("Rose")//参数的值简单类型

    private String name="Jack";

    

    //保存业务方法

    public void save(){

        System.out.println("CustomerService业务层被调用了。。。");

        System.out.println("name:"+name);

    }

}

  1. 复杂类型数据依赖注入

下面完成,将Dao类的对象注入到Service类进行使用。

注解实现属性依赖注入,将注解加在setXxx方法上 或者 属性定义上 !(任选其一,省代码了)

 

第一种: 使用@Value 结合SpEL ---- spring3.0 后用

//@Component(value="customer")

@Service(value="customer")

public class CustomerService {

    //简单类型的成员变量

    @Value("Rose")//参数的值简单类型

    private String name="Jack";

    

    //在属性声明上面注入,底层自动还是生成setCustomerDao()

    //第一种: 使用@Value 结合SpEL ---- spring3.0 后用

//其中customerDao表示<bean>节点id的属性值

    @Value("#{customerDao}")

    private CustomerDao customerDao;

    

    //保存业务方法

    public void save(){

        System.out.println("CustomerService业务层被调用了。。。");

        System.out.println("name:"+name);

        customerDao.save();

    }

 

}

第二种:使用@Autowired 结合 @Qualifier

    单独使用@Autowired ,表示按照类型注入,会到spring容器中查找CustomerDao的类型,对应<bean class="">,class的属性值,如果找到,可以匹配。

//第二种:使用spring的@Autowired

    @Autowired//默认按照类型注入

    private CustomerDao customerDao;

    使用@Autowired + @ Qualifier 表示按照名称注入,回到spring容器中查找customerDao的名称,对应<bean id="">,id的属性值,如果找到,可以匹配。

    //第二种:使用spring的@Autowired 结合 @Qualifier

    @Autowired//默认按照类型注入的

    @Qualifier("customerDao")//必须配合@Autowired注解使用,根据名字注入

    private CustomerDao customerDao;

 

第三种: JSR-250标准(基于jdk) 提供注解@Resource

单独使用@Resource注解,表示先按照名称注入,会到spring容器中查找customerDao的名称,对应<bean id="">,id的属性值,如果找到,可以匹配。

如果没有找到,则会按照类型注入,会到spring容器中查找CustomerDao的类型,对应<bean class="">,class的属性值,如果找到,可以匹配,如果没有找到会抛出异常。

    //第三种: JSR-250标准(jdk) 提供@Resource

    @Resource//默认按照名称进行匹配,再按照类型进行匹配

    private CustomerDao customerDao;

 

如果@Resource注解上添加name名称

使用@Resource注解,则按照名称注入,会到spring容器中查找customerDao的名称,对应<bean id="">,id的属性值,如果找到,可以匹配。

如果没有找到,抛出异常。

//第三种: JSR-250标准(jdk) 提供@Resource

    @Resource(name="customerDao")//只能按照customerDao名称进行匹配

    private CustomerDao customerDao;

第四种: JSR-330标准(jdk) 提供 @Inject (麻烦点)

    需要先导入 javax.inject 的 jar ,在课前资料中查找。

使用@Inject注解,则按照类型注入,

//第四种: JSR-330标准(jdk) 提供 @Inject ,配合@name注解

    @Inject//默认按照类型注入

    private CustomerDao customerDao;

使用@inject和@Named注解,则按照名称注入

//第四种: JSR-330标准(jdk) 提供 @Inject ,配合@name注解

    @Inject//默认按照类型注入

    @Named("customerDao")//按照名字注入,必须配合@Inject使用

    private CustomerDao customerDao;

提示:

开发过程中第二种方式使用@Autowired 结合 @Qualifier 的频率最高~

 

  1. Bean的初始化和销毁

使用注解定义Bean的初始化和销毁

Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。

回顾配置文件的写法:<bean id="foo" class="...Foo" init-method="setup"destory-method="teardown"/>

注解的写法:

(1)当bean被载入到容器的时候调用setup ,

注解方式如下: 

@PostConstruct

初始化

(2)当bean从容器中删除的时候调用teardown(scope= singleton有效)

注解方式如下:

@PreDestroy

销毁

 

使用 @PostConstruct 注解, 标明初始化方法 ---相当于 init-method 指定初始化方法

使用 @PreDestroy 注解, 标明销毁方法 ----相当于 destroy-method 指定对象销毁方法

第一步:创建类:LifeCycleBean.java,定义构造方法、初始化的方法、销毁的方法。

//测试生命周期过程中的初始化和销毁bean

@Component("lifeCycleBean")

public class LifeCycleBean {

    

    public LifeCycleBean() {

        System.out.println("LifeCycleBean构造器调用了");

    }

    

    //初始化后自动调用方法:方法名随意,但也不能太随便,一会要配置

    @PostConstruct//初始化的方法

    public void init(){

        System.out.println("LifeCycleBean-init初始化时调用");

    }

    

    //bean销毁时调用的方法

    @PreDestroy

    public void destroy(){

        System.out.println("LifeCycleBean-destroy销毁时调用");

    }

 

}

 

第二步:配置文件,配置spring容器applicationContext.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">

    <!-- 配置注解扫描

    context:component-scan:专门扫描含有@Component注解的类,自动将其作为bean

    base-package:要扫描包的路径,包含子包,cn.itcast.spring表示子包下的所有类定义注解都有效

    注解扫描配置的时候,会自动开启注解功能

    -->

    <context:component-scan base-package="cn.itcast.spring"/>

      

        

</beans>

第三步:使用SpringTest.java完成测试

@Test

    public void testLifeCycle() throws Exception{

        //spring容器

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //单例;此时初始化的方法已经被调用

        LifeCycleBean lifeCycleBean = (LifeCycleBean)applicationContext.getBean("lifeCycleBean");

//方案一:

//((ClassPathXmlApplicationContext)applicationContext).close();

//方案二:

        //反射的机制调用close方法。

        //接口只是引用了一个对象。对象本身有这个方法。

        //目标:通过接口引用,调用对象本来的拥有的方法

        //1。获取对象具体类的某个方法:参数1方法名,参数2:方法里面的参数类型

        Method method = applicationContext.getClass().getMethod("close");

        //参数1:拥有该方法的对象的名字,参数2:方法里面的参数的值

        method.invoke(applicationContext);

        

    }

注意:如果要执行对象的销毁方法

    条件一: 单例Bean (在容器close时,单例Bean才会执行销毁方法

    条件二: 必须调用容器 close 方法

 

  1. Bean的作用域

通过@Scope注解,指定Bean的作用域(默认是 singleton 单例)

 

//测试生命周期过程中的初始化和销毁bean

@Component("lifeCycleBean")

//@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)

@Scope("prototype")//默认是单例(singleton),更改为多例(prototype)

public class LifeCycleBean {

 

}

 

 

  1. XML和注解混合配置 (重点

一个项目中XML和注解都有(特殊年代产物)

  • Spring2.0 就有@Autowired注解
  • Spring2.5 之后才有@Component注解

 

使用

XML 完成Bean定义

注解 完成Bean属性注入

 

创建包:cn.itcast.spring.b_mixed

 

第一步

(1)创建ProductDao类

//产品的数据层

public class ProductDao {

    

    public void save(){

        System.out.println("查询保存到数据口--数据层调用了");

    }

 

}

(2)创建ProductService类

//产品的业务层

public class ProductService {

    

    //注入dao

    //强调:注入必须是bean注入bean

    @Autowired

    private ProductDao productDao;

    

    //产品的保存

    public void save(){

        System.out.println("产品保存了,--业务层");

        //调用dao

        productDao.save();

    }

 

}

 

第二步:使用XML的方式完成Bean的定义

创建applicationContext-mixed.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">

    

    <!-- xml方式定义bean -->

    <bean id="productDao" class="cn.itcast.spring.b_mixed.ProductDao"/>

    <bean id="productService" class="cn.itcast.spring.b_mixed.ProductService"/>

    

    <!-- 需要单独开启注解功能 -->

    <context:annotation-config/>

</beans>

 

备注:这里配置 <context:annotation-config>

才能使用 @PostConstruct @PreDestroy @Autowired @Resource

<!-- 需要spring容器中单独开启注解功能 -->

    <context:annotation-config/>

 

提示:因为采用注解开发时, <context:component-scan> 具有<context:annotation-config>的功能 。

如果没有配置注解扫描,需要单独配置 <context:annotation-config>, 才能使用注解注入!

 

  1. Spring的web集成

 

第一步: 新建web项目 spring3_day01_web ,导入jar包,导入applicationContext.xml和log4j.properties文件

结构参考:

第二步创建cn.itcast.spring.service包,

编写HelloService.java 业务类

//业务层

public class HelloService {

    

    public void sayHello(){

        System.out.println("嘿,传智播客。。。。");

    }

 

}

 

将对象的创建,交给Spring容器管理,applicationContext.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">

 

    <!-- HelloService的bean -->

    <bean id="helloService" class="cn.itcast.spring.service.HelloService"></bean>

</beans>

第三步: 创建cn.itcast.spring.servlet包,编写HelloServlet.java ,调用HelloService类

使用ApplicationContext applicationContext = new ClassPathXmlApplicationContext()的方式加载spring容器;

public class HelloServlet extends HttpServlet {

    

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

 

        //传统方式:

        //new service

        //HelloService helloService = new HelloService();

        //spring方式:只要看到new,你就想到spring容器中的<bean>

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        HelloService helloService=(HelloService)applicationContext.getBean("helloService");

        helloService.sayHello();

    }

 

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        this.doGet(request, response);

    }

 

}

第四步:将程序部署到tomcat测试:

 

 

 

【思考、阅读】直接new ClassPathXmlApplicationContext()有什么缺点?

缺点:在创建Spring容器同时,需要对容器中对象初始化。而每次初始化容器的时候,都创建了新的容器对象,消耗了资源,降低了性能。

解决思路:保证容器对象只有一个。

解决方案:将Spring容器绑定到Web Servlet容器上,让Web容器来管理Spring容器的创建和销毁。

分析:ServletContext在Web服务运行过程中是唯一的, 其初始化的时候,会自动执行ServletContextListener 监听器 (用来监听上下文的创建和销毁),具体步骤为:

编写一个ServletContextListener监听器,在监听ServletContext到创建的时候,创建Spring容器,并将其放到ServletContext的属性中保存(setAttribute(Spring容器名字,Spring容器对象) )。

我们无需手动创建该监听器,因为Spring提供了一个叫ContextLoaderListener的监听器,它位于spring-web-3.2.0.RELEASE.jar中。

 

开发步骤:

 

第一步:导入spring web的jar

第二步:在web.xml 配置Spring的核心监听器

<!-- spring的核心监听器 -->

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

第三步:启动tomcat服务器,结果发现异常,因为默认会加载

根据异常提示:发现spring的BeanFactory没有初始化,说明没有找到spring容器,即applicationContext.xml文件

 

第四步:在web容器中配置spring文件路径

为什么没有找到applicationContext.xml文件呢?因为此时加载的是WEB-INF/applicationContext.xml,而不是src下的applicationContext.xml文件

原因:找到ContextLoaderListener.class,再找到ContextLoader.class,发现默认加载的WEB-INF/applicationContext.xml

解决方案:需要在web.xml中配置,加载spring容器applicationContext.xml文件的路径

<!-- spring的核心监听器 -->

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 全局参数变量 -->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <!-- applicationContext.xml文件的位置,使用classpath定义 -->

    <param-value>classpath:applicationContext.xml</param-value>

</context-param>

重新启动tomcat服务器,没有异常,问题解决。

第五步:修改Servlet代码。在Servlet 中通过ServletContext 获取Spring容器对象

第一种方式: 使用getAttribute

//每次获取的都是一个spring容器

ApplicationContext applicationContext =

(ApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

第二种方式:使用WebApplicationContextUtils (推荐)

//工具类

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

  1. Spring的junit测试集成

Spring提供spring-test-3.2.0.RELEASE.jar 可以整合junit。

优势:可以简化测试代码(不需要手动创建上下文)

 

使用spring和junit集成

第一步:项目导入junit 开发包

第二步:导入spring-test-3.2.0.RELEASE.jar

第三步: 通过@RunWith注解,使用junit整合spring

通过@ContextConfiguration注解,指定spring容器的位置

//目标:测试一下spring的bean的某些功能

@RunWith(SpringJUnit4ClassRunner.class)//junit整合spring的测试//立马开启了spring的注解

@ContextConfiguration(locations="classpath:applicationContext.xml")//加载核心配置文件,自动构建spring容器

public class SpringTest {

    //使用注解注入要测试的bean

    @Autowired

    private HelloService helloService;

    

    @Test

    public void testSayHello(){

        //获取spring容器

//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //从spring容器中获取bean对象

//        HelloService helloService=(HelloService)applicationContext.getBean("helloService");

        //测试业务功能

        helloService.sayHello();

        

    }

}

上述代码表示:在测试类运行前的初始化的时候,会自动创建ApplicationContext对象

 

第四步: 通过@Autowired注解,注入需要测试的对象

在这里注意2点:

  1. 将测试对象注入到测试用例中
  2. 测试用例不需要配置<context:annotion-config/>,因为使用测试类运行的时候,会自动启动注解的支持。

    //使用注解注入要测试的bean

        @Autowired

        private HelloService helloService;

     

     

    第五步:调用测试方法完成测试

    @Test

        public void testSayHello(){

            helloService.sayHello();

            

        }

     

     

     

    知识点梳理:

    1. 复习Spring学习路线 (第一节)
    2. IoC和DI概念区分(helloword)
    3. XML配置

    实例化Bean方式(4—前三种都好好练习) ----- FactoryBean 使用(重点) 区分BeanFactory和FactoryBean

    作用域 singleton和prototype

    初始化和销毁 ------ 了解 BeanPostProcessor 后处理Bean

    依赖注入(2种):构造器注入 <constructor-arg> 、 属性setter注入 <property>

        了解 p名称空间 spEL 表达式

        了解 集合类型注入

    1. 注解配置

    Bean扫描注册 (配置属性)

    属性依赖注入(4种—至少要记住一种@autowaired)

    初始化、销毁

    作用域(@scope)

    混合配置

    1. web集成,配置Spring监听器 (ContextLoaderListener)—classpath:的使用webappp…util.get(context)
    2. 测试集成 (@runwith @ContextConfiguration(核心配置))---熟悉

     

     

     

     

原文地址:https://www.cnblogs.com/beyondcj/p/6271038.html