多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 创建一个 maven 项目** ```xml <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <spring.version>6.0.11</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies> ``` **2. 创建一个实体类** ```java @Data public class User { private String username; private String password; } ``` **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="learn.spring.entity.User"> <property name="username" value="张三"/> <property name="password" value="123456"/> </bean> </beans> ``` **4. 调用 IoC 容器中的 bean 组件** ```java public class UserTest { /** 获取 IoC 容器 */ private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContentext.xml"); @Test public void getUser() { //获取IoC容器里面的User Bean User user = context.getBean(User.class); System.out.println(user); ///User(username=张三, password=123456) } } ``` **** 案例代码:https://gitee.com/flymini/codes03/tree/master/learn-spring