spring架构图
核心容器
AOP:面向切面编程
Bean的含义是可重复使用的Java组件。所谓组件就是一个由可以自行进行内部管理的一个或几个类所组成、外界不了解其内部信息和运行方式的群体。使用它的对象只能通过接口来操作。
IOC通过配置文件管理service和Dao
通过接口获得Ioc容器获取Bean
具体实现:
1.新建maven项目
jdk17,maven3.8.6
2.导入以下依赖
<!-- 引入spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.3</version>
</dependency>
<!-- 引入Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- 引入LomBok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
3.新建实体类User
4.新建XML文件
<?xml version="1.0" encoding="UTF-8"?><bean id="user" class="entity.User">
<property name="username" value="张三" />
<property name="password" value="123456" />
<property name="age" value="18" />
<property name="phoneNumber" value="12368965432" />
</bean>
class=“实体类对应的包名” id是Bean唯一标识,
5.使用
创建Test启动类
// 通过Xml加载获取Ioc容器对象。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“XML Configuration File.xml”);
// 通过Ioc容器获取Bean对象
User user = applicationContext.getBean(“user”, User.class);
// 打印对象内容,查看内容是否对的上。[User(username=张三, password=123456, age=18, phoneNumber=12368965432)]
System.out.println(user);