Spring安装配置教程 - Go语言中文社区

Spring安装配置教程


1.Spring简介

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
2002 Rod Johnon <Expoer One-on-one j2eedevelopment and Design>
Spring 2003 ,IOC Aop
Spring data,spring boot,spring cloud,spring framework ,spring social

IOC :控制反转 (DI:依赖注入)


2.搭建Spring环境

下载jar
http://maven.springframework.org/release/org/springframework/spring/
spring-framework-4.3.9.RELEASE-dist.zip
开发spring至少需要使用的jar(5个+1个):
spring-aop.jar 开发AOP特性时需要的JAR
spring-beans.jar 处理Bean的jar 
spring-context.jar 处理spring上下文的jar 
spring-core.jar spring核心jar
spring-expression.jar spring表达式
三方提供的日志jar
commons-logging.jar 日志

2.编写配置文件
方式一:
为了编写时有一些提示、自动生成一些配置信息:
方式一:增加sts插件
可以给eclipse增加 支持spring的插件:spring tool suite(https://spring.io/tools/sts/all)
下载springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite.zip,然后在Eclipse中安装:Help-Install new SoftWare.. - Add
因为我的eclipse版本不高因为你下载时的sts版本需要对应你的eclipse插件才有效,如果想要装插件的可以参考下面文章:
https://jingyan.baidu.com/article/2d5afd69208f8a85a2e28eb6.html

方式二:
直接下载sts工具(相当于一个集合了Spring tool suite的Eclipse): https://spring.io/tools/sts/
下载后会得到一个压缩包,解压后即可使用,

界面如eclipse一样,并且装上了sts插件

首先,我们新建一个Java project
然后我们来配置xml文件


新建:bean configuration .. - applicationContext.xml

3.开发Spring程序(IOC)

ApplicationContext conext = new ClassPathXmlApplicationContext(“applicationContext.xml”) ;
建立一个学生实体类以及测试类
Student.java

package entity;

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;
    public int getStuNo() {
        return stuNo;
    }
    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getStuAge() {
        return stuAge;
    }
    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.stuNo+","+this.stuName+","+this.stuAge;
    }
}

Test.java

package test;

import entity.Student;

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        student.setStuName("zs");
        student.setStuAge(23);
        student.setStuNo(001);
        System.out.println(student);
    }
}

现在我们使用Spring IOC方式来做
先到刚刚建立的xml来配置
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">

    <!-- 该文件中产生的所有对象,被spring放入到了一个springIOC的容器中 -->
    <!-- id:唯一标识符 class:指定类型 -->
    <bean id = "student" class = "entity.Student">
        <!-- property:该class所代表的类的属性 -->
        <property name="stuNo" value = "002"></property>
        <property name="stuName" value = "ls"></property>
        <property name="stuAge" value = "20"></property>
    </bean>

</beans>

在对Test测试类进行修改

下面是springIOC容器的工作原理:

//执行从springIOC容器中获取一个 id为student的对象
Student student = (Student)conext.getBean(“student”) ;
可以发现,springioc容器 帮我们new了对象,并且给对象赋了值

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lytwy123/article/details/84497149
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢