导包
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>5.3.13</version> |
| </dependency> |
编写实体类
| public class Hello{ |
| private String name; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| @Override |
| public String toString() { |
| return "Hello{" + |
| "name='" + name + '\'' + |
| '}'; |
| } |
| } |
注意一定要有set方法,Spring容器需要set方法
编写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 |
| https://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <bean id="hello" class="com.Google.pojo.Hello"> |
| <property name="name" value="hello,Spring"/> |
| </bean> |
| |
| </beans> |
这个配置文件,官网上有,建议去官网拷贝
实现
| public class Mytest { |
| |
| public static void main(String[] args) { |
| |
| ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml"); |
| |
| Object hello = context.getBean("hello"); |
| System.out.println(hello.toString()); |
| } |
| |
| } |
从开始到现在,我们都没有new过对象,因为对象都被Spring容器给托管。 Spring IoC 容器,负责实例化、配置和组装 bean。
运行结果
| Hello{name='hello,Spring'} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律