依赖注入的概念
依赖注入(Dependency Injection,DI)
- 依赖:指Bean对象的创建依赖于容器,Bean对象的依赖资源
- 注入:指Bean对象所依赖的资源,由容器来设置和装配
依赖注入的方式
- 构造器注入(上一章已经演示过)
- set方式注入
- 拓展方式注入(p命令空间、c命令空间等)
set方式注入(重点)
环境搭建:
1、复杂类型(Address.java)
public class Address{
private String address;
public String getAddress(){
return address
}
private void setAddress(String address){
this.address=address;
}
}
2、真实测试对象(Student.java)
public class Student{
private String name;
private Address address;
private String[] books;
private List<String> hobbies;//爱好
private Map<String,string> card;//卡片
private Set<String> games;
private String wife;//妻子
private Properties info;//自定义 信息
public String getStudent(){
return name;
}
public void setStudent(String name){
this.name=name;
}
public void setBooks(String[] books){
this.books=books;
}
public void setHobbies(List<String> hobbies){
this.hobbies=hobbies;
}
public void setCard(Map<String,String> card){
this.card=card;
}
public void setGames(Set<String> games){
this.games=games;
}
public void setWife(String wife){
this.wife=wife;
}
public void setInfo(Properties info){
this.info=info;
}
@override
public String toString(){
return "Student{"+
"name"+name+
",address"+address.getAddress()+
",books"+Arrays.toString(books)+
",hobbies"+honnies+
",card"+card+
",games"+games+
",wife"+wife+
"+info"+info+
"}"
}
}
3、编写Student.xml配置文件,并将其导入applicationContext.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="student" class="com.ljy.pojo.Student">
<property name="name" value="李锦阳"/>
</bean>
</beans>
4、测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
student.show();
}
}
结果:
完整的注入信息
<?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" class="com.ljy.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.ljy.pojo.Student">
<!--普通注入-->
<property name="name" value="李锦阳"/>
<!--Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--List-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>打篮球</value>
<value>旅游</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证号" value="111111222222223333"/>
<entry key="手机号" value="12345678912"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>斗地主</value>
<value>LOL</value>
</set>
</property>
<!--null-->
<property name="wife">
<null></null>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">hahahaha</prop>
<prop key="url">www.hahahh.com</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值 property-->
<bean id="user" class="com.ljy.pojo.User" p:name="李锦阳" p:age="18"/>
<!--c命名空间注入,通过构造器注入 Construct-args-->
<bean id="user2" class="com.ljy.pojo.User" c:name="阳哥" c:age="81"/>
</beans>
测试:
@Test
public void Test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("UserBeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user.toString());
}
注意:p命名空间和c命名空间不能直接使用,需要导入xml约束。
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
bean的作用域
1、单例模式
Spring的默认机制
<bean id="user" class="com.ljy.pojo.User" p:name="李锦阳" p:age="18" scope="singleton"/>
2、原型模式
每次从容器中get的时候,都会产生一个新的对象
<bean id="user" class="com.ljy.pojo.User" p:name="李锦阳" p:age="18" scope="prototype"/>
3、其余的request、session、application,这些只能在web开发中使用到!