spring4简单实例(1)

来源:互联网 发布:python数据挖掘常用包 编辑:程序博客网 时间:2024/06/05 03:41

这篇文章简单介绍下spring4.3.5的使用。
所用的jar包:

controller类:

箭头指向的那两个,别的不用看,其中Test是测试类,如下:


springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
    <!-- scan the package and the sub package -->
    <context:component-scan base-package="test.SpringMVC"/>
    <!--
        配置bean
        class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
        id:标识容器的bean,id唯一。
     -->
    <bean id="helloWorld" class="test.ceshi.HelloWorld">
        <property name="name" value="springs"></property>
    </bean>

</beans>

注释:

ApplicationContext的主要实现类:

  ClassPathXmlApplicationContext:从类路径下加载配置文件

FileSystemXmlApplicationContext:从文件系统中加载配置文件

ConfigurableApplicationContext扩展于ApplicationContext,新增加两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力

ApplicationContext在初始化上下文时就实例化所有单例的Bean。

WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作

0 0
原创粉丝点击