SSM——Spring 学习总结(一) Spring IOC容器

Spring 学习总结(一) Spring IOC容器

1.Spring简介

  • 简介:Spring是一个分层的JavaSE/EE一站式轻量级开源框架,解决业务逻辑层和其他各层的耦合问题,将面型接口的编程思想贯穿整个系统。
  • 目的:解决企业应用开发的复杂性
  • 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
  • 范围:任何Java应用
  • 优点:将已有框架无缝对接、整合,可以替代EJB,完全独立于各种应用服务器等等……

2.学前准备

  • 搭建Spring环境
    1.下载jar
    下咋地址:
    https://maven.springframework.org/release/org/springframework/spring/
    在maven库中找到“Spring/-4.3.9.RELEASE”包 点进去后下载后缀为dist.zip的二进制文件
    下载好后解压,在解压好的文件中有一个libs文件夹点进去有好多jar,我们只需要部分jar
    ⑴ 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 必不可少
    ⑸ spring-expression-4.3.9.RELEASE.jar        //Spring表达式
    这是我们需要用到的6个jar中的5个,还有一个日志jar需要另外下载。
    下载地址:https://mvnrepository.com/artifact/commons-logging/commons-logging/1.1.1
    ⑹commons-logging-1.1.1.jar                           //日志jar
    这个jar也不是最新版本的但是我们现在用这个就好可以自己下载高版本的看一看有什么区别。
    接下来的学习过程中还会涉及到其他jar,我会在用到的时候再给大家介绍。
    需要注意的是有的搜索引擎可能不支持这两个地址建议使用百度。
  • 编译器
    2. 其实普通的Eclipse就可以编写Spring,但是没有提示,无法自动生成代码,我们可以下载Spring tool suite插件
    下载地址:https://spring.io/tools/sts/all
    如何将插件导入Eclipse就不讲解了
    注:需要下载和你自己的Eclipse版本相同的插件不然是安装不到Eclipse上的
    建议:直接下载sts编译器
    下载地址:https://spring.io/tools/sts/
    直接点击 “DOWNlOAD STS” 加载即可。下载好后解压完成后文件夹里有一个STS.exe直接点击即可使用。使用方法和Eclipse一样。
    以上就是学习Spring的前期准备工作。准备好后我们就可以开会通过程序学习Spring IOC
    我的网盘中就有这些文档:https://pan.baidu.com/s/1QpC6sgdPiPd9eSNOBCQnCQ 提取码:ly1a
    我不建议大家都通过网盘下载,最好自己通过浏览器下载这个jar ,如果实在找不到在通过网盘下载。

3、编写代码

  • 创建项目,可以创界Spring项目,也可以创建Java项目。
  • 导包,将之前提到的6个jar包导入到项目中。
  • 开始编程
    在这里插入图片描述
  • 建立一个Student类
package test.student;
public class Student {
/* 
    * 
    * 创建学生属性学号、年龄、姓名。
    * */
   private int num,age;
   private String name;
   /* 
    * 
    * 创建 get、set方法
    * */
   public int getNum() {
   	return num;
   }
   public void setNum(int num) {
   	this.num = num;
   }
   public int getAge() {
   	return age;
   }
   public void setAge(int age) {
   	this.age = age;
   }
   public String getName() {
   	return name;
   }
   public void setName(String name) {
   	this.name = name;
   }
   /* 
    * 
    * 创建 toString方法
    * */
   @Override
   public String toString() {                                            
   	// TODO Auto-generated method stub
   	return this.name+","+this.age+","+this.num;
   }
}
  • 编写applicationContext.xml 配置文件
  • 1.通过set方法赋值
<?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">
	<beans>
	<!-- 通过set方法向类中注入值-->
	   <bean id = "student" class = "test.student.Student">
	   <!-- propeity只能通过set方法对 class所指向的类中注入值 
	   注:该方法使用时class所指向的类中不能出现有参数构造器-->
	      <property name="age" value = "14"></property>
	      <property name="name" value = "张三"></property>
	      <property name="num" value = "1"></property>
	   </bean>
	</beans>
</beans>
  • 通过构造器赋值
<?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">
	<beans>
<!-- 	   <bean id = "student" class = "test.student.Student">
	      <property name="age" value = "14"></property>
	      <property name="name" value = "张三"></property>
	      <property name="num" value = "1"></property>
	   </bean> -->
	   <!-- 通过构造器注入 index可以不加。
	   如果不加index则<constrctor/>中参数顺序必须与构造器中参数顺序相同
	   切index中下表从‘0’开始 name参数双引号中的值是class所指向的类中的
	   变量名-->
	   <bean id = "student" class = "test.student.Student">
	         <constructor-arg value = "1" index = "0" name = "num"></constructor-arg>
	         <!-- 该语句可以理解为向test.student包中Student类中通过构造器给变量构造器中第一个参数‘num’赋值‘1’-->
	         <constructor-arg value = "13" index = "1" name = "age"></constructor-arg>
	         <constructor-arg value = "张三" index = "2" name = "name"></constructor-arg>
	        <!-- 注:该<constructor-arg/>方法中 index name 两个参数可有可无,
	        若添加则必须与你要住入值的变量的位置和变量名完全相同。若不添加
	        该语句的顺序必须与Class类的构造器中的参数顺序保持一致-->
	   </bean>
	</beans>
</beans>
  • 编写测试类
package test.student.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.student.Student;
public class Test {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student student = (Student)context.getBean("student");
		/*
		 *控制反转:将创建Student对象的方式从new 改为了get
		 *DI(依赖注入):将属性值从set方法给变量赋值更改为通过配置文件注入到属性中
		 **/
		System.out.println(student);
	}
}
  1. Spring IOC容器帮我们节省了new 的过程不需要我们再创建类的对象。在大规模、大任务量的项目中如果有一个类的类名或者赋值方式发生更该,那我们可能整个文件大规模报错很难维护,但是在Spring IOC容器 完美的解决了这个麻烦,我们只需要将applicatioContext.xml中引用该类的"bean"中的class更改即可,大大的增加了程序的可维护性。

>>SSM——Spring 学习总结(二) Spring IOC容器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值