01、Spring基础及入门
1. Spring是什么
Spring是一种开源轻量级框架,是为了解决企业应用程序开发复杂性而创建的,Spring致力于解决JavaEE的各层解决方案,而不仅仅于某一层的方案。
2. Spring的发展历程
2003年2月Spring框架正式称为一道开源项目,Spring致力于J2EE应用的各种解决方案,而不仅仅专注于某一层解决方案。可以说Spring是企业应用开发的“一站式”选择, Spring贯穿于表现层、业务层、持久层,然而Spring并不想取代那些已经有的框架,而是以高度的开放性,与这些已有的框架进行整合。
3. Spring的目标
- 让现有的技术更容易使用,
- 促进良好的编程习惯。
Spring是一个全面的解决方案,它坚持一个原则:不从新造轮子。已经有较好解决方案的领域,Spring绝不重复性实现,比如:对象持久化和OR映射,Spring只对现有的JDBC,Hibernate等技术提供支持,使之更容易使用,而不做重复的实现。Spring框架有很多特性,这些特性由7个定义良好的模块构成。
4. Spring体系结构
- Spring Core:即,Spring核心,它是框架最基础的部分,提供IOC和依赖注入特性
- Spring Context:即,Spring上下文容器,它是BeanFactory功能加强的一个子接口
- Spring Web:它提供Web应用开发的支持
- Spring MVC:它针对Web应用中MVC思想的实现
- Spring DAO:提供对JDBC抽象层,简化了JDBC编码,同时,编码更具有健壮性。
- Spring ORM:它支持用于流行的ORM框架的整合,比如:Spring + Hibernate、Spring + iBatis、Spring + JDO的整合等等。
- Spring AOP:AOP即,面向切面编程,它提供了与AOP联盟兼容的编程实现
5. Spring常用组件
6. Spring初体验
6.1 xml文件配置类
- 1.新建一个普通的maven项目,在pom文件引入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
- 2 新建个Person类
public class Person {
private String name;
private Integer age;
//getter/setter方法
//无参/全参构造方法
}
- 3 在resource下建立spring的配置文件,在里面配置bean
<?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="person" class="com.zz.cap1.bean.Person">
<property name="name" value="zhangsan"/>
<property name="age" value="27"/>
</bean>
</beans>
- 4 执行测试类
public class MainTest1 {
public static void main(String[] args) {
//把beans.xml的类加载到容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) context.getBean("person");
//从容器中获取bean
System.out.println(person);
}
}
ClassPathXmlApplicationContext:会加载类路径下的XML。结果如下:
6.2 注解配置类
- 1 新建配置类MainConfig
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
注意:@Configuration注解的配置类有如下要求:
- @Configuration不可以是final类型;
- @Configuration不可以是匿名类;
- 嵌套的configuration必须是静态类。
//配置类====配置文件
@Configuration
public class MainConfig {
//给容器中注册一个bean, 类型为返回值的类型,
//方法名默认为bean的id
//如何修改bean的id
//1,直接修改方法名
//2,@Bean("你要命名的bean名字")
@Bean
public Person person() {
return new Person("李四", 18);
}
}
- 2 新建MainTest2测试类
public class MainTest2 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
Person person = (Person) context.getBean("person");
System.out.println(person);
}
}
AnnoatationConfigApplicationContext: 注解配置来获取IOC容器。结果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)