3

Spring安装配置教程

 2 years ago
source link: https://blog.p2hp.com/archives/9626
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

  1. package entity;
  2. public class Student {
  3. private int stuNo;
  4. private String stuName;
  5. private int stuAge;
  6. public int getStuNo() {
  7. return stuNo;
  8. }
  9. public void setStuNo(int stuNo) {
  10. this.stuNo = stuNo;
  11. }
  12. public String getStuName() {
  13. return stuName;
  14. }
  15. public void setStuName(String stuName) {
  16. this.stuName = stuName;
  17. }
  18. public int getStuAge() {
  19. return stuAge;
  20. }
  21. public void setStuAge(int stuAge) {
  22. this.stuAge = stuAge;
  23. }
  24. @Override
  25. public String toString() {
  26. // TODO Auto-generated method stub
  27. return this.stuNo+","+this.stuName+","+this.stuAge;
  28. }
  29. }

Test.java

  1. package test;
  2. import entity.Student;
  3. public class Test {
  4. public static void main(String[] args) {
  5. Student student = new Student();
  6. student.setStuName("zs");
  7. student.setStuAge(23);
  8. student.setStuNo(001);
  9. System.out.println(student);
  10. }
  11. }

现在我们使用Spring IOC方式来做
先到刚刚建立的xml来配置
xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- 该文件中产生的所有对象,被spring放入到了一个springIOC的容器中 -->
  6. <!-- id:唯一标识符 class:指定类型 -->
  7. <bean id = "student" class = "entity.Student">
  8. <!-- property:该class所代表的类的属性 -->
  9. <property name="stuNo" value = "002"></property>
  10. <property name="stuName" value = "ls"></property>
  11. <property name="stuAge" value = "20"></property>
  12. </bean>
  13. </beans>

在对Test测试类进行修改

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK