今天开个新坑(づ。◕ᴗ◕。)づ介绍一下Spring框架???
- Spring是个开源框架
- Spring是一个Ioc(ID)和AOP容器框架
- 轻量级:Spring是得侵入性的,我们不需要使用Spring的提供接口或者继承他的类
- 依赖注入
- 面向切面编程
- 容器
- 框架
- 一站式框架
最底层核心容器分为四块:Beans,Core,Context,SpEL
具体细节,之后章节再一一讲解
配置Maven
大家创建好项目之后,可以去https://mvnrepository.com/ 找我们需要的jar包,我们需要四个基础的jar包,也就是上面图上最底层核心容器的相关jar包,除此之外,我们还需要一个spring必须依赖的日志包:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency>
写个HelloWorld
Emmmm,基本流程,先写一个非常简单的类,还有方法ヾ(❀●◡●)ノ
-
package com.figsprite.bean;
-
-
public class HelloWorld {
-
private String name;
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public void hello(){
-
System.out.println("hello " + name);
-
}
-
}
-
package com.figsprite.bean;
-
-
public class Main {
-
-
public static void main(String[] args) {
-
HelloWorld helloWorld = new HelloWorld();
-
helloWorld.setName("world");
-
helloWorld.hello();
-
}
-
-
}
真的很简单,我们来说说Main中的代码
-
创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
-
为name属性赋值
helloWorld.setName("world");
-
调用hello方法
helloWorld.hello();
Spring的任务就是帮我们完成1.2两步,
通过IDEA解决spring配置文件
先创建spring的配置文件,首先右键新建èXML Configuration FileèSpring Config
创建一个applicationContext.xml文件
-
<?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.figsprite.bean.HelloWorld">
-
<property name="name" value="World"></property>
-
</bean>
-
</beans>
看看里面各个标签,首先是<bean>,很明显,Spring需要利用反射创建新对象,所以我们需要传入类的全限定名,并用id作为唯一标识,接着里面所包含的<property>标签,name属性即为setXXX所对应的字段名,value就是赋给它的值。
这样在Main中的步骤就将发生变化
-
创建Spring的Ioc容器对象
ApplicationContext代表的就是Spring中的Ioc容器接口,ClassPathApplicationContext是ApplicationContext的一个实现类,传入的是配置文件的名字
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-
从Ioc容器中获取Bean实例
这里传入的是<bean>标签的id
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
-
调用hello方法
helloWorld.hello();
好奇的小伙伴可以试着在HelloWorld.java中加点东西看看Spring是否真的帮我们创建了新对象,并且将它赋值,我们注释掉2.3步的代码,看看发生什么
-
package com.figsprite.bean;
-
-
public class HelloWorld {
-
private String name;
-
-
public HelloWorld(){
-
System.out.println("****创建对象****");
-
}
-
public void setName(String name) {
-
System.out.println("****设置name****");
-
this.name = name;
-
}
-
-
public void hello(){
-
System.out.println("hello " + name);
-
}
-
}
是它,是它,就是它,(~ ̄▽ ̄)~就是Spring干的
-