Spring入门

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

2. 什么是控制反转(或依赖注入) ?
   控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。

 如何使用Spring(5.0.1.RELEASE)
   1)在pom.xml文件中引入spring-core、spring-context的jar支持
   2)在resources目录右键 New -> Spring -> Spring Bean Configuration File
   3)勾选aop、context支持

核心概念:
 1)控制反转(IOC):控制权由应用代码移交给外部容器
 2)依赖注入(DI):由Spring容器动态的将某种依赖注入到指定的组件中

三、maven实现Spring的IOC

3.1 创建项目成功后,第一步在pom.xml配置为了引入使用的jar包:

maven-compiler-plugin插件、  servlet和Junit 、Spring-core、Spring-context            

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
 
    <!-- junit-->
    <junit.version>4.13</junit.version>
    <!--servlet-->
    <servlet.version>4.0.1</servlet.version>
    <!-- spring-->
    <spring.version>5.0.1.RELEASE</spring.version>
 
  </properties>
 
  <dependencies>
    <!--junit+servlet-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>
 
  <!--spring-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
 
  </dependencies>
 
 
  <build>
    <finalName>spring01</finalName>
  <plugins>
    <!--第一步就是配置maven-compiler-plugin插件-->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
        <encoding>${project.build.sourceEncoding}</encoding>
      </configuration>
    </plugin>
 
  </plugins>

3.2配置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">
</web-app>


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>宝贝</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定义对应多个对象实例

//示例1:传统方式和Spring方式的区别
        //自动导包 File=>settings=>auto import=>2个以on the fly结尾打勾
       /* Hello hello=new Hello();
        hello.setName("lisi");
        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的完整类名(类的路径名)
    /*    Object hello = ac.getBean("hello", Hello.class);
        System.out.println(((Hello) 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);
        Hello d = ac.getBean("d", Hello.class);
        System.out.println(a.getName());
        System.out.println(b.getName());
        System.out.println(c.getName());
        System.out.println(d.getName());*/
        //示例四:spring bean 之scope属性
        // scope:(singleton|prototype)默认是singleton单例模式
        //singleton(单例模式):在每个Spring IoC容器中一个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());*/

5、  abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
6、   parent:指定一个父bean(必须要有继承关系才行)

示例类Demo.java中的代码

//示例五:spring bean之间abstract parent属性
        //4 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(student.getName());
        System.out.println(worker.getName());*/

spring.xml中的代码

 <bean id="preson" class="com.zking.entity.Preson" abstract="true">
        <property name="name">
            <value>佚名</value>
        </property>
    </bean>
 
    <bean id="worker" class="com.zking.entity.Worker" parent="preson">
        <property name="name">
            <value>宝贝</value>
        </property>
    </bean>

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

示例类Demo.java中的代码

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

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

 在Hello类中要有Init()方法

   public void init(){
        System.out.println("正在初始化hello中的init方法——");
    }
    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }

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

示例类Demo.java中的代码

  //示例七:spring bean之constructor-arg
        /*  Hello hello1 = ac.getBean("hello1", Hello.class);
        System.out.println(hello1);*/

spring.xml中的构造方法Bean代码

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

在Hello类中要有构造方法

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

9、日期格式注入

示例类Demo.java中的代码

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

spring.xml中的日期Bean

 <bean id="student" class="com.zking.entity.Student" parent="preson">
        <property name="name">
            <value>宝贝</value>
        </property>
        <property name="birthDay">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2022-3-12"/>
            </bean>
        </property>
 
 
 
 
    <!-- 日期格式注入 -->
    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
    </bean>

五、属性配置

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>

spring的模块

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值