4

spring | 注入原理「简易版本」

 1 year ago
source link: https://benpaodewoniu.github.io/2023/01/03/spring29/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

spring | 注入原理「简易版本」

2023-01-03javaspring基础

3

例子来源于 spring | 什么是注入

  • spring5 项目
  • java 1.8
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>

基本原理还是使用反射。

<?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">
<bean id="person" class="com.rhino.Person">
<property name="id"> <!--name 指向属性名字-->
<value>10</value>
</property>
<property name="name">
<value>小明</value>
</property>
</bean>
</beans>

当 spring 检测到 bean 标签的时候,进行反射创建

  • <bean id="person" class="com.rhino.Person">
    • 等于 Person p = new Person()
  • <property name="name"> <value>小明</value> </property>
    • 等于 p.setName("小明")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK