spring容器

来源:互联网 发布:java的用户登录界面 编辑:程序博客网 时间:2024/06/05 19:38

一.spring容器是什么?

是spring框架中的一个核心模块,用于管理对象

二.spring容器如何启动?

先创建一个Maven Project工程
(1).导包
双击pom.xml文件,选择Dependencies,选择Add
这里写图片描述
在Enter groupId, artifactId or sha1 prefix or pattern (*):框中输入spring-webmvc
这里写图片描述
等待一段时间后,在Search Results:框中会出现一堆包,找到如下这个包,然后选择ok,然后Ctrl+S保存
这里写图片描述
这里写图片描述
ps. 若出现Search Results框中没有包的现象,可尝试采取如下操作:
Window->preferences->Maven->将Download repository index updates on startup选项框选中
Window->Show View->Other->Maven->双击Maven Repositories
这里写图片描述
在Central上右击选择Full Index Enable
这里写图片描述
等待一段较长的时间,然后全部下载完毕后就可以进行导包操作了(我是这样做之后才成功的)

(2). 添加配置文件(applicationContext.xml或xxx.xml)
在项目的src/main/resources文件夹下,添加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"    xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"></beans>

(3).启动spring容器
在src/main/java文件夹中可创建包和类,如下:
这里写图片描述
在方法体中添加如下代码来实现容器的启动:
//启动spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
这里写图片描述

三.如何让容器帮我们创建对象

方式一.使用无参构造器(需重点掌握)
(1).给类添加无参构造器,或是直接系统给的缺省构造器
(2).配置.xml文件内bean标签中的元素(id属性为bean的名称,class属性需写类的全限定名,包括包名)
(3).调用容器的getBean()方法来获得对象

代码演示(以Student类为例):
Student.java中写如下代码:

package first;public class Student {    //无参构造方法    public Student() {        System.out.println("Student()");    }}

applicationContext.xml配置文件的/beans根标签内添加如下代码:

    <!-- 使用无参构造器创建对象         id属性:bean的名称,要求唯一         class属性:类的全限定名(包括包名)    -->    <bean id="stu1" class="first.Student">    </bean>

FirstSpring .java的main方法中添加如下代码:

//获得对象Student stu = ac.getBean("stu1",Student.class);//id需对应System.out.println(stu);

运行结果:
这里写图片描述

方式二.使用静态工厂方法 (仅需了解,在此暂不做详述)
通过调用类的静态工厂方法创建对象(bean的factory-method属性赋一个静态方法的值)

方式三.使用实例工厂方法(仅需了解,在此暂不做详述)
通过调用对象的实例方法来创建对象(bean的factory-bean属性指定一个bean的id,factory-method属性指定一个实例方法)

四.作用域
默认情况下,容器对于某个bean只会创建一个实例(单例)
可以设置scope属性值为prototype,这样,容器对某个bean会创建多个实例(原型)
applicationContext.xml配置文件中对bean标签作用域的编写如下代码:

    <!--     配置作用域    scope属性:用来配置作用域,    缺省值:singleton(单例,一个bean只创建一个实例),    如果值为prototype(原型,一个bean会创建多个实例)     -->    <bean id="s1" class="scope.ScopeBean"     scope="prototype" />

五.生命周期(需重点掌握)
初始化方法:使用init-method属性来指定初始化方法
销毁方法:使用destroy-method属性来指定销毁方法

ps:
(1)spring容器在创建对象之后,会立即调用初始化方法;
spring容器在关闭之前,会先销毁对象,在销毁对象之前,会先调用对象的销毁方法;
(2)使用ApplicationContext创建的容器不可使用.close()方法关闭
使用AbstractApplicationContext创建的容器可以使用.close()方法关闭。

代码演示如下:
学生类中添加如下方法:

public void init(){        System.out.println("init()");    }    public void destroy(){        System.out.println("destroy()");    }

applicationContext.xml配置文件中添加如下代码:

<bean id="s1" class="first.Student"    init-method="init" destroy-method="destroy" />

测试类TestCase中添加如下方法:

    @Test    //测试生命周期    public void test(){        //启动spring容器        //启动spring容器        //ApplicationContext:接口        //AbstractApplicationContext:是ac接口的子接口        //ClassPathXmlApplicationContext 是上诉接口的具体实现类        AbstractApplicationContext ac =                 new ClassPathXmlApplicationContext(                        "applicationContext.xml");        //获得对象        Student s1 = ac.getBean("s1",Student.class);        //关闭容器        ac.close();    }

运行结果:
这里写图片描述

六.延迟加载(仅了解)
spring容器启动后,会将所有的作用域为单例的bean创建好(没有调用也会创建好)
bean的lazy-init属性可用来指定是否延迟加载,true为延迟加载,即不会创建bean
一般情况下都默认要创建好单例的bean,所以该属性一般不用

ps:由于需使用JUnit进行单元测试,在此对JUnit的用法进行说明
(1).导包:
和导spring包类似,先双击项目工程的pom.xm文件,打开Dependencies模块,双击Add,输入junit,选择4.12的包,如下图:
这里写图片描述
(2).在src/main/java文件夹下添加测试类的包test,添加测试类TestCase:
这里写图片描述
(3).进行测试类的编写
(4).启动时,需打开window->show view->Outline,在Outline进行如下操作即可运行测试类:
这里写图片描述

原创粉丝点击