• 创建Maven工程,pom.xml添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.min</groupId>
<artifactId>Spring-Study</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
</dependencies> </project>
  • 创建实体类 Student
package com.min.entity;

import lombok.Data;

@Data
public class Student {
private long id;
private String name;
private int age;
}
  • 传统的开发方式,手动 new Student
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(22);
System.out.println(student);
  • 通过 IoC 创建对象,在配置文件中添加需要管理的对象,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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean id="student" class="com.southwind.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
</bean>
</beans>
  • 从 IoC 中获取对象,通过 id 获取。
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student);

最新文章

  1. Vmware扩展磁盘如何不需重启系统
  2. 苹果下如果安装nginx,给nginx安装markdown第三方插件
  3. PKU 1007
  4. MVC模式介绍
  5. 用wordpress制作网站的总结
  6. jq hover方法用法 例子
  7. android中数据存储
  8. Android基础学习之context
  9. Android Animation动画(很详细)
  10. C++ sizeof 操作符的用法总结
  11. Matlab实现Hough变换检測图像中的直线
  12. mysql常用基础操作语法(十二)~~常用数值函数【命令行模式】
  13. 详解Nginx服务器配置
  14. 在action中进行文件下载,下载时运行不报错,可是也不下载
  15. mybatis使用枚举优化
  16. jdbctemplate 调用oracle 有返回(会话型临时表数据的)结果的存储过程
  17. 『Python CoolBook』C扩展库_其一_用法讲解
  18. hive命令的三种执行方式
  19. 系统学习NLP(二十一)--SWEM
  20. matlab中的Traing、Validation、Testing

热门文章

  1. Day13_72_类锁
  2. WinForm只运行运行一个实例(单开)
  3. k8s 运行单实例 mysql
  4. C#搞个跨平台的桌面NES游戏模拟器
  5. Weekly Contest 138
  6. Python 巡检接入钉钉机器人
  7. 一个简单的Android木马病毒的分析
  8. hdu1914 稳定婚姻问题
  9. C#-窗体移动
  10. window下批量删除指定后缀文件