Spring
是一个开源的免费的框架(容器)
是一个轻量级的,非入侵式的框架
控制反转(IOC),面向切面编程(AOP)
支持事务的处理,对框架整合的支持
Spring环境搭建
1、创建一个maven项目
2、在pom.xml导入包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
3、创建一个名为spring-01-ioc1的mvn项目
依赖注入:本身使用set方法注入
控制反转思想:由主动创建对象变为被动接受对象,从本质上解决了程序员不用去管理对象的创建了,可以更加专注在业务的实现上。
第一个Spring项目
HelloSpring
创建新项目:spring-02-HelloSpring
<?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">
</beans>
在resources下新建文件beans.xml
编写dao
package com.hua.pojo;
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
编写beans.xml
<!--使用Spring来创建对象,在Sprig这些都成为Bena-->
<bean id="hello" class="com.hua.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
编写测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Hello hello=(Hello)context.getBean("hello");
System.out.println(hello.toString());
}
}
打印结果