【Spring】学习笔记05-DI注入
1.构造器注入
前面已经说过了
2.通过Set方式注入
依赖注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性由容器来注入!
环境搭建
pojo实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private Properties info; private String wife; }
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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address_bean" class="com.wang.pojo.Address"> <constructor-arg index="0" value="北京市海淀区"/> </bean> <bean id="student" class="com.wang.pojo.Student"> <!-- 普通值注入,value--> <property name="name" value="王广元"/> <!-- Bean注入,ref--> <property name="address" ref="address_bean"/> <!-- 数组注入--> <property name="books"> <array> <value>你的名字</value> <value>我的天才女友</value> <value>万历十五年</value> </array> </property> <!-- list注入--> <property name="hobbys"> <list> <value>jungle</value> <value>yoga</value> <value>music</value> </list> </property> <!-- map注入--> <property name="card"> <map> <entry key="学生卡" value="200"></entry> <entry key="洗澡卡" value="20"></entry> <entry key="剪发卡" value="200"></entry> </map> </property> <!-- set注入--> <property name="games"> <set> <value>LOL</value> <value>Dota</value> </set> </property> <!-- null值注入--> <property name="wife"> <null></null> </property> <!-- Properties注入--> <property name="info"> <props> <prop key="学号">1120201390</prop> <prop key="大学">大连海事大学</prop> <prop key="专业">电子信息</prop> </props> </property> </bean> </beans>
单元测试
@Test
public void test01(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student student = (Student)context.getBean("student");
System.out.println(student.toString());
}
测试结果
Student{name='王广元', address=Address(address=北京市海淀区), books=[你的名字, 我的天才女友, 万历十五年], hobbys=[jungle, yoga, music], card={学生卡=200, 洗澡卡=20, 剪发卡=200}, games=[LOL, Dota], info={学号=1120201390, 专业=电子信息, 大学=大连海事大学}, wife='null'}
进程已结束,退出代码为 0
3.其他拓展方式注入
pojo类
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private int age; }
p命名空间注入
XML Shortcut with the p-namespace
The p-namespace lets you use the bean
element’s attributes (instead of nested <property/>
elements) to describe your property values collaborating beans, or both.
Spring supports extensible configuration formats with namespaces, which are based on an XML Schema definition. The beans
configuration format discussed in this chapter is
defined in an XML Schema document. However, the p-namespace is not defined in an XSD file and exists only in the core of Spring.
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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p命名空间注入,可以直接注入属性--> <bean id="user_p" class="com.wang.pojo.User" p:age="28" p:name="user_p"> </bean> </beans>
注意:要使用p命名空间必须添加xmlns命名约束
xmlns:p="http://www.springframework.org/schema/p"
单元测试:
@Test public void test02(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); User user_p = context.getBean("user_p", User.class); System.out.println(user_p.toString()); }
c命名空间注入
XML Shortcut with the c-namespace
Similar to the XML Shortcut with the p-namespace, the c-namespace, introduced in Spring 3.1, allows inlined attributes for configuring the constructor arguments rather then nested constructor-arg
elements.
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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p命名空间注入,可以直接注入属性--> <bean id="user_c" class="com.wang.pojo.User" c:name="user_c" c:age="18"> </bean> <bean id="user_c1" class="com.wang.pojo.User" c:_0="user_c_0" c:_1="18"/> </beans>
注意:此时通过c命名空间注入属性,我们既可以通过实体类的属性(c:name),也可以通过实体类的属性下标(c:_0)进行注入
同时必须得导入xmlns约束
xmlns:c="http://www.springframework.org/schema/c"
单元测试
@Test public void test03(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("UserBean_c_namespace.xml"); User user_c = context.getBean("user_c", User.class); System.out.println(user_c.toString()); User user_c1 = context.getBean("user_c1", User.class); System.out.println(user_c1.toString()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现