spring入门基础

来源:互联网 发布:ext3 windows 编辑:程序博客网 时间:2024/06/05 03:57

spring介绍

1.spring是一个服务层的框架,它的优点很多,主要核心是 (IOC,AOP)无缝对接框架,提供对AOP的支持,和以前的Struts Hibenrate 组成了一套ssh框架,ssh框架也算的上曾经的霸主,如今 spring 和springMvc MyBatis组成了ssm框架,ssh已经渐渐被代替

IOC介绍

IOC 的全称是 Inversion Of Control 翻译过来是控制反转的意思。 通俗的说: 就是把对象的创建工作交给spring来完成。

之前我们讲了BeanFactory,spring框架IOC的底层实际上就是使用BeanFactory来创建对象,

IOC托管

其实就是在BeanFactory工厂读取对象的xml文件里加上一行 id跟class而已,提供BeanFactory读取然后通过反射来创建对象

spring入门

1.下载jar包
http://repo.spring.io/release/org/springframework/spring/

2.导入核心jar包
在下载的文件里找到lib,在lib中找到以下五个jar包

  • spring-beans-xxx.jar
  • spring-context-xxx.jar
  • spring-context-support-xxx.jar
  • spring-core-xxx.jar
  • spring-expression-xxx.jar

3.导入slf4日志jar包,如果没有导入会报错,资源我会上传,找不到也没关系,

4.编写spring的配置文件,

  • xml文件以applicationContext命名,
  • 导入约束
    约束的查找方式,打开下载的文档,找到,进入docs文件夹,进入spring-framework-reference文件夹,进入html文件夹,找到xsd-configuration.html ,双击打开
    找到40.2.12 the beans schema的约束,复制到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">    <!--id可以随意写,但是不能重复,因为我们需要根据id取值 -->    <!--class 是类路径-->    <bean id="p" class="cn.itcast.ioc.Person"></bean>   </beans>

写完上面的代码,我们Person就已经算是托管了,我们就可以让spring帮我们创建了

5.创建一个类

public class Person {    public void eat(){        System.out.println("人在吃");    }}

6.创建一个测试类

public class Demo {    @Test    public void Test_01(){        //获取Person对象         //传统方式  Person p = new Person();        //使用Spring获取的方式  ,             //1.加载xml配置文件 , spring会创建所有Bean对象存入ServletContext中,        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        //使用id,来取值        Person p = (Person)context.getBean("p");        p.eat();    }}

运行代码输出结果如下

这里写图片描述

IOC的实例化方式

IOC示例化方式有三种,我们之前演示的一种算是比较常用的,接下来我们来介绍另外俩种对象示例化方式
####静态工厂
还是使用原来的例子, 一个Person,一个Demo
1.静态工厂类需要我们提供工厂,所以我们需要自己创建一个

public class BeanFactory {    public static Object getPerson(){        return new Person();    }}

2.配置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">                               <!-- factory-bean:工厂类路径                                         factory-method 需要工厂调用的方法(方法必须是静态的) -->    <bean id="p" factory-bean="cn.itcast.ioc.BeanFactory" factory-method="getPerson" ></bean></beans>

使用原来的Demo类,跟原来的Person类,

public class Demo {    @Test    public void Test_01(){        //获取Person对象         //传统方式  Person p = new Person();        //使用Spring获取的方式  ,             //1.加载xml配置文件 , spring会创建所有Bean对象存入ServletContext中,        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");        //使用id,来取值        Person p = (Person)context.getBean("p");        p.eat();    }}

输出结果如下

这里写图片描述

实例工厂

见名之意,我们需要提供工厂的实例,方法是非静态的
1.我们把原来工厂的静态方法改为非静态的

public class BeanFactory2 {    public Object getPerson(){        return new Person();    }}

创建一个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">       <!-- 实例化工厂,我们要提供实例,id = factory创建了一个实例,id="p"的bean拿了实例,调用了getPerson的方法,返回了返回值 -->                <bean id="factory" class="cn.itcast.ioc.BeanFactory2"></bean>     <bean  id="p" factory-bean="factory" factory-method="getPerson"></bean></beans>

还是使用原来的Person类 ,和Demo类

public class Demo {    @Test    public void Test_01(){        //获取Person对象         //传统方式  Person p = new Person();        //使用Spring获取的方式  ,             //1.加载xml配置文件 , spring会创建所有Bean对象存入ServletContext中,        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");        //使用id,来取值        Person p = (Person)context.getBean("p");        p.eat();    }}

输出结果如下
这里写图片描述

以上俩种方式基本不会使用,因为spring已经提供了BeanFactory,所以就不使用我们自己的了,开发通常使用第一种,或注解的方式来获取对象


DI注入

DI注入的意思其实就是在获取对象的时候,同时给对象里的某个参数赋值,该操作一般使用注解来做,不过既然是入门,那么我们就老老实实的先学基础把,

构建环境

1.导入之前导入的jar包
2.创建一个Person类

public class Person {//定义一个属性    private String name;    public void eat(){        System.out.println("name = "+name+"在吃");    }}

创建一个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 id="p" class="cn.itcast.demo.di.Person"></bean></beans>

//赋值的写法有俩种,
一种是使用有参构造来赋值, 一种是使用set来赋值,

我们先使用有参构造来演示

1.让Person提供一个有参构造

public class Person {    private String name;    public Person(String name){        this.name = name;    }    public void eat(){        System.out.println("name = "+name+"在吃");    }}

2.修改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里加一个constructor-arg标签,spring就会走有参构造方法,参数类型需要和有参里面的对应 ->    <bean id="p" class="cn.itcast.demo.di.Person">        <constructor-arg value="zs"></constructor-arg>    </bean></beans>

3.创建一个Demo类

public class Demo {    @Test    public void Test_01(){        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");        Person p = (Person)context.getBean("p");        p.eat();    };}

运行结果如下
这里写图片描述

我们使用set来演示

之前我们使用String类型的参数演示,这次我们使用对象类型演示
创建一个类Ruiwen

public class Ruiwen {//为了方便看结果,我就给name赋值了    private String name = "瑞文";    @Override    public String toString() {        return "Ruiwen [name=" + name + "]";    }}

在Person里提供set方法

public class Person {    private Ruiwen r;    //提供set方法    public void setR(Ruiwen r) {        this.r = r;    }    public void eat(){        System.out.println("name = "+r+"在吃");    }}

编写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 id="rw" class="cn.itcast.demo.di.Ruiwen"/>                       <!-- 使用property标签,获取对象时会走set方法 -->         <bean id="p" class="cn.itcast.demo.di.Person">        <!-- property的name属性是参数属性名 -->        <property name="r" ref="rw"></property>  <!-- 由于是对象类型的,我们不能使用value属性,我们需要使用Bean给它提供一个对象,ref就是从id=rw的标签拿一个对象 -->    </bean></beans>

使用原来的Demo类

public class Demo {    @Test    public void Test_01(){        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext5.xml");        Person p = (Person)context.getBean("p");        p.eat();    };}

运行结果如下 : name属性是我们自己手动赋值的, 但是Ruiwen属性是spring赋值的
这里写图片描述

今天就先讲那么多,下次会讲IOC注解的用法跟DI的注解用法,