Spring入门

本文介绍了Spring的基本概念,包括Spring的功能和作用,以及如何在Java Web项目中使用Spring 5.0.1.RELEASE。详细讲解了控制反转(IoC)和依赖注入的概念,展示了如何配置spring.xml文件,以及Spring与Web项目的集成,包括ServletContextListener的使用。
摘要由CSDN通过智能技术生成

1. 什么是spring,它能够做什么?

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

2. 什么是控制反转(或依赖注入)

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

3.如何使用Spring(5.0.1.RELEASE)

在pom.xml文件中引入spring-core、spring-context、junit、servlet的jar支持

配置web.xml(3.0)

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">

在resources目录右键 New -> Spring -> Spring Bean Configuration File->Spring config创建Spring,xml

3.3spring.xml文件的创建

1、在resources目录右键 New -> Spring -> Spring Bean Configuration File

        

 

2、创建spring.xml

在main=》resources=》右键new=》XML Configure File=》Spring Config

3、在spring.xml中配置Bean

  <bean id="hello" name="a b c" class="com.zking.entity.Hello"
    scope="prototype" init-method="init">
        <property name="name" >
            <value>xzc</value>
        </property>
    </bean>

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

7、   init-method:指定bean的初始化方法

8、 constructor-arg:使用有参数构造方法创建javaBean

9、日期格式注入

五、属性配置
1. 简单属性的配置:
   8+1+3
   8基础数据+String+3个sql
   java.util.Date
     java.sql.Date
     java.sql.Time
     java.sql.Timestamp
   通过<value>标签赋值即可
2. 复杂属性的配置
JavaBean
      ref bean=""
List或数组
 Map
 Properties

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

3.spring与web项目的集成
   WEB项目如何读取spring上下文
   通过监听器实现ServletContextListener
   contextConfigLocation:classpath:applicationContext-*.xml
   <!-- 配置SpringListener监听器的Spring配置文件路径 -->
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
   </context-param>
   <!-- 配置SpringListener监听器 -->
   <listener>
    <listener-class>com.zking.spring01.util.SpringListener</listener-class>
   </listener>

示例类Demo.java中的代码

package com.zking.util;


import com.zking.entity.Hello;
import com.zking.entity.Student;
import com.zking.entity.Worker;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        /*1.什么是spring
        spring是一个轻量级的控制反转和面向切面的容器框架
        核心概念:
        控制反转(IOC):控制权由应用代码移交给外部容器(spring)
        依赖注入(DI):由Spring容器动态的将某种依赖注入到指定的组件中
        示例:Biz层依赖于Dao的接口
        作用:用于管理各种各样的JavaBean对象
        理解:将以前由程序员实例化对象,赋值属性的过程交由Spring容器来管理*/

        //示例一:传统方式与Spring方式的区别
        /*Hello hello = new Hello();
        hello.setName("张三");
        System.out.println(hello);*/

        //初始化Spring上下文(Context),Spring IOC容器,用于管理各种各样的JavaBean对象的容器框架
        /*ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");*/
        /*Hello hello = ac.getBean("hello", Hello.class);
        System.out.println(hello.getName());*/

        //示例二:Spring Bean之id、class属性
        //id:在容器中查找Bean的id(唯一、且不能以/开头)
        //class:bean的完整类名
        /*Hello hello = ac.getBean("hello", Hello.class);
        System.out.println(hello.getName());*/

        //示例三:Spring Bean之name属性
        //name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
        /*Hello a = ac.getBean("a", Hello.class);
        Hello b = ac.getBean("b", Hello.class);
        Hello c = ac.getBean("c", Hello.class);
        System.out.println(a.getName());
        System.out.println(b.getName());
        System.out.println(c.getName());*/

        //示例四:Spring Bean之scope属性
        //scope:(singleton|prototype)默认是singleton
        //singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
        //prototype(原型模式/多例模式):一个bean定义对应多个对象实例
        /*Hello a = ac.getBean("a", Hello.class);
        Hello b = ac.getBean("b", Hello.class);
        Hello c = ac.getBean("c", Hello.class);
        System.out.println(a.getName());
        System.out.println(b.getName());
        System.out.println(c.getName());*/

        //示例五: Soring Bean之abstract、parent属性
        //abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
        //parent:指定一个父bean(必须要有继承关系才行)
        /*Worker worker = ac.getBean("worker", Worker.class);
        Student student = ac.getBean("student", Student.class);
        System.out.println(worker.getName());
        System.out.println(student.getName());*/

        //示例六:Spring Bean之init-method
        //init-method:指定bean的初始化方法
        /*Hello hello = ac.getBean("hello", Hello.class);
        System.out.println(hello);*/

        //示例七:Spring Bean之constructor-arg
        //constructor-arg:使用有参数构造方法创建javaBean
        /*Hello hello1 = ac.getBean("hello1", Hello.class);
        System.out.println(hello1);*/

        //示例八:Spring Bean 之日期类型注入
        /*Student student = ac.getBean("student", Student.class);
        System.out.println(student.getBirthDay().toLocaleString());*/

        //示例九: Spring Bean之ref属性引用
        /*Student student = ac.getBean("student", Student.class);
        System.out.println(student.getAddress().getCity());*/

        //示例十:Spring Bean之数组、集合以及Properties
        /*Student student = ac.getBean("student", Student.class);
        System.out.println(Arrays.toString(student.getArr()));
        System.out.println(student.getLst());
        System.out.println(student.getMap());
        System.out.println(student.getProp());*/

        //示例十一:配置文件的读取方式
        //读取加载单个文件
        //ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //读取加载多个文件
        //ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-address.xml"});
        //通配方式 *
        //ApplicationContext ac = new ClassPathXmlApplicationContext("spring-*.xml");

    }
}

Hello.java中的代码

package com.zking.entity;

import java.io.Serializable;

public class Hello implements Serializable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Hello() {
        System.out.println("new Hello()");
    }

    public Hello(String name) {
        System.out.println("使用有参的构造方法");
        this.name = name;
    }

    public void init(){
        System.out.println("正在初始化Hello中的init方法");
    }

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}

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

    <import resource="spring-address.xml"/>

    <bean id="hello" name="a,b,c" class="com.zking.entity.Hello" scope="prototype" init-method="init">
        <property name="name">
            <value>狗蛋</value>
        </property>
    </bean>

    <bean id="hello1" class="com.zking.entity.Hello">
        <constructor-arg index="0">
            <value>钉钉</value>
        </constructor-arg>
    </bean>

    <bean id="person" class="com.zking.entity.Person" abstract="true">
        <property name="name">
            <value>大高富帅</value>
        </property>
    </bean>

    <bean id="worker" class="com.zking.entity.Worker" parent="person">
    </bean>

    <bean id="student" class="com.zking.entity.Student" parent="person">
        <property name="name">
            <value>哈哈</value>
        </property>
        <property name="birthDay">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2000-12-2"/>
            </bean>
        </property>
        <property name="address">
            <ref bean="address"/>
        </property>
        <property name="arr">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>
        <property name="lst">
            <list>
                <value>3</value>
                <value>2</value>
                <value>1</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="id">
                    <value>111</value>
                </entry>
                <entry key="name">
                    <value>哈哈</value>
                </entry>
                <entry key="age">
                    <value>80</value>
                </entry>
            </map>
        </property>
        <property name="prop">
            <props>
                <prop key="class">
                    数学
                </prop>
                <prop key="score">
                    90
                </prop>
                <prop key="rank">
                    1
                </prop>
            </props>
        </property>
    </bean>

    <!-- 日期格式注入 -->
    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
    </bean>
</beans>

Spring模块 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值