1. Spring 环境搭建以及第一个spring程序
1. Spring 环境搭建以及第一个spring程序
eclipse版本:
下载jar包
建议先在spring官网下载一个sts:集合了spring插件的eclipse
spring官网下载的jar包:
网址:https://maven.springframework.org/release/org/springframework/spring/
目前下载的是4.3版本进行学习
第三方日志jar:
建立java项目或者web项目
将jar包放入构建路径中
至少需要的jar包(5+1):
spring-aop-4.3.9.RELEASE.jar
开发aop特性时需要的jar
spring-beans-4.3.9.RELEASE.jar
处理Bean的jar
spring-context-4.3.9.RELEASE.jar
处理spring上下文的jar
spring-core-4.3.9.RELEASE.jar
spring核心jar
spring-expression-4.3.9.RELEASE.jar
spring表达式
(第三方提供)commons-logging.jar
三方提供的的日志jar
添加到构建路径中
- 如图
添加第三方提供的日志jar
- 将 commons-logging.jar 添加到 构建路径 中
编写配置文件
为了编写时有一些提示、自动生成一些代码,可以安装已经集成好了spring插件的eclipse工具sts,然后在插件里面下载spring tools 3 就OK了,这样方便一些
新建配置文件
- bean configuration .. --applicationContext.xml
开发spring程序
建立一个保存学生数据的类:
package org.yibinxueyuan.entity; /** * 学生信息类 */ public class Student { private int stuno; //学号 private String stuName = null; //姓名 private int stuAge; //年龄 public Student() { // TODO Auto-generated constructor stub } public Student(int stuno, String stuName, int stuAge) { super(); this.stuno = stuno; this.stuName = stuName; this.stuAge = stuAge; } @Override public String toString() { return "Student [stuno=" + stuno + ", stuName=" + stuName + ", stuAge=" + 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; } }测试类
package org.yibinxueyuan.test; import org.yibinxueyuan.entity.Student; /** * 测试类 * @author ASUS * */ public class Test { public static void main(String[] args) { /** * 传统的方式 * Student student = new Student(); student.setStuno(1); * student.setStuName("zs"); student.setStuAge(23); * System.out.println(student.toString()); */ // 使用spring后的方式 } }配置文件
<?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放入 了一个称为spring ioc容器的地方 --> <!-- id作为bean的唯一标识, class是需要实例化的类的位置(指定类的具体位置) --> <bean id = "student" class = "org.yibinxueyuan.entity.Student"> <!-- property: 该class 所代表的类的属性 name : 属性名 value : 属性值--> <property name="stuno" value = "2"></property> <property name="stuName" value= "ls"></property> <property name="stuAge" value = "24"></property> </bean> </beans>
SpringIoc容器
springIoc容器的作用是创建对象、给对象属性的属性赋值
xml文件必须放在项目根路径
代码结构为:
id作为bean的唯一标识
class是需要实例化的类的位置(指定类的具体位置)
property: 该class 所代表的类的属性
name : 属性名
value : 属性值<?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放入 了一个称为spring ioc容器的地方 --> <!-- id作为bean的唯一标识, class是需要实例化的类的位置(指定类的具体位置) --> <bean id = "student" class = "org.yibinxueyuan.entity.Student"> <!-- property: 该class 所代表的类的属性 name : 属性名 value : 属性值--> <property name="stuno" value = "2"></property> <property name="stuName" value= "ls"></property> <property name="stuAge" value = "24"></property> </bean> </beans>
idea版本:
简介
spring理念 : 使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架
核心 : 控制反转(IOC) , 面向切面编程(AOP)
SSH : Struct2 + Spring + Hibernate 过时
SSM : SpringMvc +Spring + Mybatis
配置
- 在mvn库中下载如图中的Spring Web MVC 版本的jar包
或者导入maven:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.9.RELEASE</version> </dependency>还要导入Spring JDBC jar包
或者maven:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.9.RELEASE</version> </dependency>
浙公网安备 33010602011771号