ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/spring_/com-learn-spring01 **** **1. 创建一个 maven 项目** ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <scope>compile</scope> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies> ``` **2. 创建一个实体类** ```java @Data public class User { private String userName; } ``` **3. 将实体类注入到 IoC 容器中** *`resources/ApplicationContentext.xml`* ```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.xsd"> <!-- 配置User的bean --> <bean id="user" class="com.learn.spring01.pojo.User"> <property name="userName" value="张三"/> </bean> </beans> ``` **4. 调用 IoC 容器中的 bean 组件** ```java public class UserTest { private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContentext.xml"); @Test public void getUserName() { User user = context.getBean(User.class); //张三 System.out.println(user.getUserName()); } } ```