1.spring简介
-
Spring框架是一个开源的应用程序框架,是针对bean的生命周期进行管理的轻量级容器。
-
Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与 Swing等桌面应用程序AP组合。
-
Spring不仅仅能应用于J2EE应用程序之中,也可以应用于桌面应用程序以及小应用程序之中。
-
Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。
官方文档地址:
https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/
https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/
优点:
- 开源免费
- 轻量级的非入侵式的
- 控制反转(IOC),面向切面编程(aop)
- 支持事务处理
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.22</version>
</dependency>
七大模块:
弊端:发展了太久后,配置越来越多,人称“配置地狱”
2.IOC理论推导
在我们之前的业务中,用户的需求可能会影响程序的代码,可能需要修改代码,如果程序的代码量十分大,修改一次的成本十分的昂贵!
原来的方式:
private UserMapper usermapper=new UserMapperImpl();
现在将对象的传递由new变成set动态注入
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper){
this.userMapper=userMapper;
}
原来是程序控制的,现在变成用户控制了。
3.一个spring项目的快速搭建
package com.pojo;
/**
* @author panglili
* @create 2022-07-23-21:40
*/
public class HelloSpring {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
<?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" >
<!--使用spring来创建对象,在spring中被称为bean -->
<!-- class="com.pojo.HelloSpring" 相当于在newHelloSpring
id="helloSpring" 相当于对象变量名字
name="name" 属性
value="spring" 属性值
-->
<bean id="helloSpring" class="com.pojo.HelloSpring">
<property name="name" value="spring"></property>
</bean>
</beans>
(3)测试
import com.pojo.HelloSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
/**
* @author panglili
* @create 2022-07-23-21:43
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClasspathXmlApplicationContext( "application.xml");
HelloSpring hello =(HelloSpring) context.getBean("helloSpring");
System.out.println(hello.toString());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。