spring-依赖注入
1、构造器注入
前面有提及过!!!
2、set方式注入
依赖注入:set注入
依赖:bean对象的创建依赖于容器
注入:bean对象的属性由容器注入
环境搭建:
配置文件beans: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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<bean id="address" class="com.haoqi.pojo.Address"/>
<bean id="student" class="com.haoqi.pojo.Student">
<!-- 普通值注入 value-->
<property name="name" value="搜索搜索"></property>
<!-- 地址注入 ref-->
<property name="address" ref="address">
</property>
<!-- 数组-->
<property name="books">
<array>
<value>你</value>
<value>听说</value>
<value>爱看书</value>
</array>
</property>
<!-- 列表-->
<property name="hobbys">
<list>
<value>萨顶顶</value>
<value>使得</value>
<value>vdv</value>
<value>萨</value>
</list>
</property>
<!-- Map-->
<property name="cards">
<map>
<entry key="id" value="445202200322214355"></entry>
<entry key="bank_id" value="213214124"></entry>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>lol</value>
<value>lpl</value>
<value>lbl</value>
</set>
</property>
<!-- 设null值 -->
<property name="wife">
<null></null>
</property>
<!-- properties-->
<property name="info">
<props>
<prop key="学号">20205080</prop>
<prop key="姓名">陈</prop>
<prop key="性别">男</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
实体类:
Student.java
package com.haoqi.pojo; import java.util.*; public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> cards; private Set<String> games; private String wife; private Properties info; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", cards=" + cards + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCards() { return cards; } public void setCards(Map<String, String> cards) { this.cards = cards; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } }
测试类
Test.java
import com.haoqi.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); } }
3、其他注入
配置文件xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <bean id="user" class="com.haoqi.pojo.User" c:age="15" c:name="太子"></bean> <bean id="user1" class="com.haoqi.pojo.User" p:age="17" p:name="第三"> </bean> </beans>
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术