Spring学习笔记--Spring入门hello world

1. Spring概述

Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架

2. Spring体系架构

在这里插入图片描述

2.1. Core Container(核心容器)
  • Beans模块:提供了BeanFactory 。
  • Core核心模块:提供了Spring框架的基本组成部分,包括IoC和DI功能。
  • Context上下文模块:是访问定义和配置的任何对象的媒介。
  • SpEl模块:是Spring3.0新增模块,提供Spring Expression Lanuage支持。
2.2. Data Access/Integration(数据访问/集成)
  • JDBC模块:提供一个JDBC的抽象层。
  • ORM模块:对流行的对象关系映射API提供了集成层支持。
  • OXM模块:提供了一个支持对象/XML映射的抽象层实现。
  • JMS模块:指java消息传递服务。
  • Transactions模块:支持对实现特殊接口以及所有POJO类的编程和声明式的事务管理。
2.3. Web
  • WebScoket模块:提供了WebScoket 和 SockJS的实现。
  • Servlet模块:也称为Spring-webmvc模块。
  • Web模块:提供了基本的Web开发集成特性。
  • Portlet模块:提供了在Portlet环境中使用MVC实现,类似Servlet模块的功能。
2.4. 其他模块
  • AOP模块:提供了面向切面编程实现。
  • Aspects模块:提供了与AspectJ的集成功能。
  • Instrumentation模块:提供了类工具的支持和类加载器的实现。
  • Messaging模块:提供对消息传递体系结构和协议的支持。
  • Test模块:提供了对单元测试和集成测试的支持。

3.Spring下载

地址:https://repo.spring.io/simple/libs-release-local/org/springframework/spring/
在这里插入图片描述

  • docs文件夹中包括Spring的API文档和开发规范。
  • libs中包含开发需要的jar包和源码。
  • schema文件夹中包括开发所需要的schema文件,这些文件中定义了spring相关配置文件的约束。
3.1. Spring框架包

libs中有四个基础包:如下
在这里插入图片描述

3.2. 第三方依赖包

使用上图的基础包,还需要依赖commons.logging的jar包
地址:http://commons.apache.org/proper/commons-logging/

4.Spring的入门程序

4.1. 在eclipse中一个创建Web项目,将Spring的四个基础包和commons-logging的jar包复制到Lib目录下。

在这里插入图片描述
在这里插入图片描述

4.2. 在src目录下创建包,面向接口编程

在这里插入图片描述

  • 在传统模式下,调用者通常会采用“ new 被调用者 ”的代码方式来创建对象,这种方式会导致调用者与被调用者之间的耦合性增加,不利于后期项目的升级和维护。

例如:
在这里插入图片描述

  • 使用Spring框架后,对象的实例不再有调用者来创建,而是由Spring容器来创建,Spring容器负责控制程序之间的关系,而不是由调用者的程序代码直接控制。这样,控制权由应用代码转移到了Spring容器,控制权发生了反转,这就是Spring的控制反转(IoC)

例如:
在这里插入图片描述

  • Spring容器负责将被依赖对象赋值个调用者的成员变量,这相当于为调用者注入了它依赖的实例,这就是Spring的依赖注入(DI)

实现方式

  • 属性setter方法注入:
    例如:
    在这里插入图片描述
package com.xhh.service.Impl;


import com.xhh.service.HelloWorld;
import com.xhh.service.HelloWorld2;

public class HelloWorldImpl2 implements HelloWorld2 {

	// 我需要用到HelloWorld中实现的方法,使用依赖注入实例化
	private HelloWorld helloWorld;
	
	// 实现setter注入
	public void setHelloWorld(HelloWorld helloWorld) {
		this.helloWorld = helloWorld;
	}


	@Override
	public void say2() {
		helloWorld.say();
		System.out.println("你好,欢迎来到spring的世界2--依赖注入");
		System.out.println("我使用HelloWorld的方法,但我没有在这个类中实例化它,只是定义出来");
	}

}

<?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">

    <bean id="helloWorld" class="com.xhh.service.Impl.HelloWorldImpl">
    </bean>

	<bean id="helloWorld2" class="com.xhh.service.Impl.HelloWorldImpl2">
	<!--name值为HelloWorldImpl2类中未实例的值,ref为本xml中bean的关于HelloWorld的id -->
		<property name="helloWorld" ref="helloWorld"></property>
	</bean>
</beans>
package com.xhh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xhh.service.HelloWorld2;

public class HelloWorldTest2 {

	public static void main(String[] args) {
		ApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld2 helloWorld2 = (HelloWorld2) applicationContext.getBean("helloWorld2");
		
		helloWorld2.say2();
	}

}

  • 构造函数注入

例如:

<?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">

    <bean id="helloWorld" class="com.xhh.service.Impl.HelloWorldImpl">
    </bean>

	<bean id="helloWorld2" class="com.xhh.service.Impl.HelloWorldImpl2">
	
 		<!--setter注入,name值为HelloWorldImpl2类中未实例的值,ref为本xml中bean的关于HelloWorld的id -->
<!-- 		<property name="helloWorld" ref="helloWorld"></property> -->
		
		<!-- 构造函数注入 -->
		<constructor-arg index="0" ref="helloWorld"></constructor-arg>
	</bean>
</beans>
package com.xhh.service.Impl;


import com.xhh.service.HelloWorld;
import com.xhh.service.HelloWorld2;

public class HelloWorldImpl2 implements HelloWorld2 {

	// 我需要用到HelloWorld中实现的方法,使用依赖注入实例化
	private HelloWorld helloWorld;
	
	// 实现setter注入
//	public void setHelloWorld(HelloWorld helloWorld) {
//		this.helloWorld = helloWorld;
//	}
	
	public HelloWorldImpl2(HelloWorld helloWorld) {
		this.helloWorld = helloWorld;
	}


	@Override
	public void say2() {
		helloWorld.say();
		System.out.println("你好,欢迎来到spring的世界2--依赖注入");
		System.out.println("我使用HelloWorld的方法,但我没有在这个类中实例化它,只是定义出来");
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值