持续更新
----------------------------------------------------
自己在网络上找了一些学习spring框架的视频,先看b站最新的尚硅谷视频,传送门:https://www.bilibili.com/video/BV1b7411p79Y?from=search&seid=13548384235867309699
------------------------------------------------------
day01 框架基础知识
知名的6大框架:struts1、struts2、hibernate、spring、springMVC、mybatis
分类
mvc框架: struts1(封装servlet)、struts2(封装过滤器 filter?)、springmvc
持久层框架:hibernate(自动化)、mybatis(半自动)
整合型框架:spring
-------------------------------------------------
spring模块四核心:Beans、Core、context、spel(spring express language)
- beans:bean管理
- Context:上下文(体现在xml配置)
- Expression Language:spring EL表达式
-
Core:spring框架基石核心支持
一个依赖:logging日志依赖
使用idea创建maven项目导入spring框架必须的四核心jar包
1.file-new-project选择maven
设置groupid和artifactid一路next
2.打开pom.xml文件,配置spring依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springframeGroup</groupId>
<artifactId>springframeArtifact</artifactId>
<version>1.0-SNAPSHOT</version>
<!--使用pom引入spring四核心 来源https://blog.youkuaiyun.com/qicui2835/article/details/80792951-->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
reimport一下pom文件,等待maven从中央库下载到本地库
3.自定义一个实体类
4.在src-main-resource目录下新建applicationContext文件(默认这么写,当然可以自定义),设置新建实体类
<?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">
<!--配置我自定义的Four类-->
<bean class="com.study.april.Four" id="fourId">
<!-- 通过property标签的name value 为对象属性赋值-->
<property name="aBoolean" value="true"></property>
<property name="myBoolean" value="true"></property>
</bean>
</beans>
5.新建测试类,使用spring容器生成自定义类实例
package com.study.april;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBySpring {
// 通过spring框架来获取类的实例对象
// 1.初始化applicationcontext容器
// 2.通过容器实例的getBean方法创建对象实例
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");// 对应 src\main\resources\applicationContext.xml
// tips:ClassPathXmlApplicationContext的文件一定要写全,.xml要带上,不然报错
Four myFour = (Four) applicationContext.getBean("fourId");
// 可以在applicationContext.xml文件中配置对象属性
System.out.println(String.format("class Four:[%s]",myFour));
/**
* 启动结果为
* 四月 04, 2020 5:10:46 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
* 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Sat Apr 04 17:10:46 CST 2020]; root of context hierarchy
* 四月 04, 2020 5:10:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
* 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
*/
}
}
完成基于bean容器创建实体类的方法,不再像以前那样new 对象,由spring管理对象
总结:老师讲的很细,认真听还是不难的,朋友说过一句话:只要用心学,天下没有学不会的东西。
千里之行始于足下,加油