spring的介绍及ioc的使用

1. 什么是spring,它能够做什么?
   Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
   Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
   然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
   目的:解决企业应用开发的复杂性
   功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
   范围:任何Java应用
   简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
   
   1.1 中间层框架、万能胶
       struts2
       spring
       hibernate
   1.2 容器框架
         JavaBean    项目中的一个个类
         IOC和AOP
        


2. 什么是控制反转(或依赖注入) 
   控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
   IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)”  ,即由容器动态的将某种依赖关系注入到组件之中 
   案例:实现Spring的IoC

   IOC/DI
     将以前由程序员实例化对象/赋值的工作交给了spring处理

3. 如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)
   3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
   3.2 class:bean的完整类名
   3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
   3.4 scope:(singleton|prototype)默认是singleton
     3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
     3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
   3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
   3.5 parent:指定一个父bean(必须要有继承关系才行)
   3.6 init-method:指定bean的初始化方法
   3.7 constructor-arg:使用有参数构造方法创建javaBean

   
   注1:struts2的Action请使用多例模式


4. 简单属性的配置:
   8+1+3
   8基础数据+String+3个sql
   java.util.Date
     java.sql.Date
     java.sql.Time
     java.sql.Timestamp
   通过<value>标签赋值即可


5. 复杂属性的配置
  5.1 JavaBean
      ref bean=""
  5.2 List或数组
  5.3 Map
  5.4 Properties


6. 针对项目,配置文件路径的2种写法
   ApplicationContext
   String path = "applicationContext.xml";
   String path = "classpath:applicationContext-*.xml";//src
   String[] path = new String[] { "applicationContext-a.xml", "applicationContext-b.xml" };//分模块开发


7. spring与web项目的集成

   WEB项目如何读取spring上下文
   通过监听器实现ServletContextListener
   contextConfigLocation:classpath:applicationContext-*.xml


8. log4j2
  


9. spring.pom
   spring-context
   spring-orm
   spring-web
   spring-aspects
   注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持
   
   
简介
spring-context.xml
ioc
    set注入
        基本数据类型注入
        集合注入
        对象注入
    构造注入
        基本数据类型注入
    自动装配
        
tomcat整合ioc容器

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.10.RELEASE</version>
</dependency>

1、spring tool suite官方下载地址:http://spring.io/tools/sts/all

2、很详细的网文在线安装介绍:http://www.cnblogs.com/liuyungao/p/6213997
    
    

spring的主配置文件

<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    default-autowire="byName"
    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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- ioc的用途 -->
<!-- 配置实现类 -->
    <bean class="com.zking.ioc.impl.UserBizImpl" id="userBiz"></bean>
    <bean class="com.zking.ioc.impl.UserBizImpl2" id="userBiz2"></bean>
    
    <!-- 给需要填充的类配置属性 -->
    <bean class="com.zking.web.UserAction" id="userAction">
        <property name="userBiz" ref="userBiz"></property>
    </bean>
    
    <bean class="com.zking.web.TeaAction" id="teaAction">
        <!-- <property name="userBiz" ref="userBiz2"></property> -->
    </bean>
    
    <!-- ioc注入的类型以及方式 -->
    <bean class="com.zking.web.StudentAction" id="studentAction">
    
        <!-- 通过提供set方法注入 -->
        <!-- <property name="sid" value="22"></property>
        <property name="sname" value="骡子"></property> -->
        
        <!-- 通过提供有参的构造方法注入 -->
        <constructor-arg name="sid" value="1"></constructor-arg>
        <constructor-arg name="sname" value="刘骡子"></constructor-arg>
        
        <property name="hobyy">
            <list>
                <value>吃饭</value>
                <value>打游戏</value>
            </list>
        </property>
    </bean>
    
    <!-- spring新特性自动注入
        修改配置文件为default-autowire
        
        ="byType"根据类型自动注入    当多个类实现了同一个接口时会报错
        byType:根据管理的javabean的接口属性,在spring的上下文中自动寻找实现类去注入,当找到两个及两个以上时会报错
        
        byName:根据管理的javabean的接口名,在spring上下文中去寻找id进行注入
     -->
    <bean class="com.zking.ioc.impl.StudentBizImpl" id="stuBiz"></bean>
    
    <bean class="com.zking.web.ClzAction" id="clzAction">
<!--         <property name="userBiz" ref="userBiz"></property> -->
<!--         <property name="stuBiz" ref="stuBiz"></property> -->
    </bean>
    
</beans>
 

猜你喜欢

转载自blog.csdn.net/qq_41915592/article/details/83755602