Spring初识、新建工程

1、spring与三层架构的关系:

 spring负责管理项目中的所有对象,是一个一站式的框架,容器中的对象决定了spring的功能。

2、spring核心架构

Spring框架主要由六个模块组成,在开发时可以根据需要选择合适的模块。

 (1)核心容器模块:提供了框架的最基础部分,是其它组件的基础,提供了IoC容器、Spring框架的基础核心工具类。

 (2)数据访问/集成模块:减少了JDBC代码量、提供声明式事务管理的功能等。

 (3)Web模块:封装了Web应用开发使用Spring框架时所需要的核心类。

 (4)AOP和Instrumentation模块:Instrumentation对服务器的代理接口。

 (5)Messaging模块:基于消息发送应用的基础。

 (6)测试模块:对JUnit等测试框架的简单封装。

3、spring项目搭建

(1)新建一个web项目

(2)导入jar包:

spring-framework-3.0.2.RELEASE-dependencies:集成了很多jar包,是最新版本。

spring-framework-4.2.4.RELEASE:与spring相关,目录结构如下:

 导入Spring的核心包:它们是Spring其它功能的基础

 导入日志文件相关的jar包:

spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1

另外一个日志文件相关的包(支持老版本):

spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15

(3)创建一个对象(Student):

package pers.zhb.domain;
public class Student {
    private String snum;
    private String sname;
    private String sex;
    public String getSnum() {
        return snum;
    }

    public void setSnum(String snum) {
        this.snum = snum;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "snum='" + snum + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

(4)创建配置文件:

位置:任意(这里放在src目录下)

名字:任意(这里用applicationContext)

导入外部约束(IDEA):

File下选择Settings:

 

 

 URI填入网络地址,File填入约束文件在本地的路径,然后点击确定配置完成。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
</beans>

 有提示即表明约束导入成功。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student">

 </bean>
</beans>

以上的配置文件的作用是将Student对象交给Spring容器管理。

(5)测试:

创建测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        student.setSname("zhai");
        student.setSnum("202012");
        student.setSex("nv");
        System.out.println(student);
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

创建Spring的容器对象后将Student对象从容器中取出,通过set方法对对象进行赋值,打印结果如下:

猜你喜欢

转载自www.cnblogs.com/zhai1997/p/12287054.html