导包:spring-webmvc
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
</dependencies>
pojo:
package com.kuang.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 + '\'' +
'}';
}
}
spring核心文件bean.xml:
<?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-3.0.xsd">
<!--spring 创建对象称为beans,Bean
id=对象名
class=new 对象
property:属性设置-->
<bean id="word" class="com.kuang.pojo.Hello">
<property name="str" value="spring属性值"/>
</bean>
</beans>
测试类:
import com.kuang.pojo.Hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
/**
作者
时间
类的描述
***/
public static void main(String[] args) {
// 获取spring上下文
BeanFactory context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello =(Hello) context.getBean("word");
System.out.println(hello.toString());
}
}
输出:
1、在spring创建对象,在spring中被称为bean
2、java创建对象:
对象 对象名=new 对象();
3、spring中创建对象:
Bean
id=对象名
class=new 对象
property:属性设置
所谓的Ioc就是spring来创建对象、管理、装配