spring-Di依赖注入
构造器注入
前面讲过
set方式注入
1.依赖:bean对象的创建依赖于容器
2.注入:bean对象中的所有属性都是由容器来注入
环境搭建:
1.复杂类型
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}```
2.真实测试对象
```java
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> cards;
private Set<String> games;
private Properties info;
private String wife;
3.beans.xml
<!--第一种普通注入 value-->
<bean id="student" class="com.zhang.pojo.Student">
<property name="name" value="zhang"/>
</bean>
4.测试类
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
5.完善注入信息
<?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="address" class="com.zhang.pojo.Address"/>
<bean id="student" class="com.zhang.pojo.Student">
<!--第一种普通注入 value-->
<property name="name" value="zhang"/>
<!--第二种引用 ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!--map-->
<property name="cards">
<map>
<entry key="身份证" value="123456789456123"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>LoL</value>
</set>
</property>
<!--特殊注入-->
<property name="info">
<props>
<prop key="学号">2018211809</prop>
</props>
</property>
<property name="wife">
<null/>
</property>
</bean>
</beans>
其他方式
我们可以使用p命名和c命名
官方解释:
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="User" class="com.zhang.pojo.User" p:age="18" p:name="zzz"/>
<bean id="User2" class="com.zhang.pojo.User" c:age="18" c:name="ccc"/>
</beans>
测试:
@Test
public void Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
User user = context.getBean("User2", User.class);
System.out.println(user);
}
注意:
不能直接使用,要导入约束
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
bean作用域
1.单例模式:spring默认机制
2.原型模式:每次从容器中get时,都会产生一个新对象
3.其他只能在web开发中使用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!