依赖注入
常见的依赖注入方式
普通注入
<!--普通注入-->
<bean id="student" class="com.bkms.pojo.Student">
<property name="name" value="John"/>
</bean>
Bean注入
<bean id="address" class="com.bkms.pojo.Address"/>
<bean id="student" class="com.bkms.pojo.Student">
<!--普通注入,value注入-->
<property name="name" value="John"/>
<!--Bean注入,ref注入-->
<property name="address" ref="address"/>
</bean>
数组注入
<bean id="student" class="com.bkms.pojo.Student">
<!--数组注入-->
<property name="books">
<array>
<value>book01</value>
<value>book02</value>
<value>book03</value>
</array>
</property>
</bean>
list/map/set/null/prop注入
<!--list注入-->
<property name="hobbies">
<list>
<value>hobby01</value>
<value>hobby02</value>
<value>hobby03</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="01" value="tom01"/>
<entry key="02" value="tom02"/>
<entry key="03" value="tom03"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>game01</value>
<value>game02</value>
<value>game03</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="score01">01</prop>
<prop key="score02">02</prop>
<prop key="score03">03</prop>
</props>
</property>
输出结果
import com.bkms.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student.toString());
}
}
/*
Student{name='John',
address=com.bkms.pojo.Address@3a5ed7a6,
books=[book01, book02, book03],
hobbies=[hobby01, hobby02, hobby03],
card={01=tom01, 02=tom02, 03=tom03},
games=[game01, game02, game03],
wife='null',
info={score03=03, score02=02, score01=01}
}
*/
c命名空间和p命名空间
p命名空间
<?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命名空间注入,可以直接注入属性的值,p的全称是property-->
<bean id="user" class="com.bkms.pojo.User" p:name="Johnson" p:age="22"/>
</beans>
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = context.getBean("user", User.class);
System.out.println(user);//User{name='Johnson', age=22}
}
c命名空间
<!--c命名空间注入,constructor-args,构造器参数,通过构造器注入-->
<bean id="user2" class="com.bkms.pojo.User" c:age="20" c:name="Philip"/>
bean作用域
1.单例模式(Spring默认机制)
<!--每次get不会产生新的对象-->
<bean id="user2" class="com.bkms.pojo.User" c:age="20" c:name="Philip" scope="singleton"/>
2.原型模式
<!--每次get都会产生新的对象-->
<bean id="user2" class="com.bkms.pojo.User" c:age="20" c:name="Philip" scope="protptype"/>
其余的request/session/application只能在web开发中才能用到