spring-1-模块及开始

spring模块

Core Container: 核心容器(IOC) :

spring-core,spring-beans,spring-context,spring-expression,spring-context-support

spring-core 和 spring-beans

spring-core 和 spring-beans 是 Spring 的基石,提供了基本的 DI 功能。

spring-expression

spring-expression 定义了强大的 Spring 表达式语言,用于在运行时查询和操作对象。

spring-context 和 spring-context-support

spring-context 建立在 core 和 beans 之上,提供了更高层的 API。其核心是 ApplicationContext。
spring-context-support 则是为将第三方库整合进 Spring 应用上下文 提供支持。

AOP+Aspects(面向切面) :

spring-aop,spring-aspects
spring-aop 提供了面向切面编程的实现,它允许你定义拦截器 (interceptors) 和 切点 (pointcuts),用以解耦(decouple) 关注点不同的代码。
spring-aspects 整合了 Java 最强大的一个 AOP 库:AspectJ.

Instrumentation:

spring-instrument,spring-instrument-tomcat

数据访问与集成 :

spring-jdbc,spring-tx,spring-orm,spring-oxm,spring-jms,spring-messaging

Web和远程调用:

spring-web,spring-webmvc,spring-websocket,spring-webmvc-portlet

Spring测试:

spring-test
在这里插入图片描述
IOC(Inversion of Control) 就是“控制反转”,它不是一种技术,而是一种设计思想。在spring中,使用IOC则意味着将设计好的对象交给IOC容器控制,而不是像传统的方式由我们来在对象内部控制。
DI(Dependency Injection) 就是“依赖注入”。组件之间依赖关系由容器在运行期间决定,即容器动态的将某个依赖关系注入到组件之中。
举个例子 A同学使用铅笔写字, 写错了, 没必要自己去买一个橡皮擦, 而是向B同学借一个. B同学就相当于一个容器, 因为A的需要, 将橡皮擦借给A, 就完成了一个依赖注入. A同学向 B借了一个橡皮擦, 这个橡皮擦所有权(控制权)不属于A, 属于B, 这个叫控制反转(与A同学自己买一个相比较而言).

入门程序

  1. 创建项目并导入依赖包
    spring运行依赖日志jar:
    在这里插入图片描述
    spring-core :
    在这里插入图片描述
  2. 写配置
    spring的配置文件中, 集合了spring的IOC容器管理的所有组件(会员清单) ;
    创建一个Spring Bean Configuration File ( Spring的bean的配置文件 )

在这里插入图片描述

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

	<!-- 注册一个Person对象, Spring会自动创建这个Person对象 -->
	<!-- 一个Bean标签可以注册一个组件 (对象, 类) 
		class, 注册的组件的全类名
		id, 唯一标识
		name, 指定属性名
		value, 指定属性值
	 -->
	<bean id="person01" class="com.xian.bean.Person">
		<!-- 使用property标签为Person对象的属性赋值 -->
		<property name="name" value="张三"></property>
		<property name="age" value="18"></property>
		<property name="email" value="zhangsan@xixi.com"></property>
		<property name="gender" value="男"></property>
	</bean>
	
	
</beans>
  1. 测试
//get, set方法建议自动生成, 要符合规范
public class Person {
	
	private String name;
	private Integer age;
	private String gender;
	private String email;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", email=" + email + "]";
	}
	
}
package spring_test;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xian.bean.Person;

class IOCTest {

	@Test
	void test() {
		String xmlPath = "ioc.xml";
		//ApplicationContext, 代表IOC容器
		//ClassPathXmlApplicationContext加载xml配置文件在ClassPath下
		ApplicationContext ioc = new ClassPathXmlApplicationContext(xmlPath);
		//获取person实例
		Person bean = (Person)ioc.getBean("person01");
		System.out.println(bean);
	}

}

在这里插入图片描述

  • ApplicationContext, ioc容器
  • 给容器中注册一个组件, 我们通过组件id从容器中拿到了这个组件的对象
  • 组件的创建工作, 是容器完成的
  • 容器中的对象再容器创建完成的时候就已经创建好了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值