spring是什么?
- spring是一个开源的免费的框架
- spring是一个轻量级的,非入侵式的框架
- 控制反转,面向切面编程
- 支持事务的处理,对框架整合的支持
总结一句话:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!
1、新建maven工程
2、导入spring依赖的jar包
<!--spring-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
3、写一个实体类
public class Hello {
private String str;
public void setStr(String str) {
this.str = str;
}
public String getStr() {
return str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
4、spring配置文件
<?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="hello" class="com.liu.pojo.Hello">
<property name="str" value="Spring春天到了"/>
</bean>
</beans>
id:相当于新建一个实体类对象
class:对应的实体类
properties:给实体类对象的属性赋值
5、编写测试类
public class Mytest {
public static void main(String[] args) {
//获取spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.getStr());
}
}