6.1、构造器注入
详见四、IOC创建对象
Spring-IOC创建对象的方式_xmosang的博客-优快云博客
6.2、Set方式注入【重点】
-
依赖注入
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中的所有属性,由容器来注入
-
【环境搭建】
-
pojo:
package com.mosang.pojo;
import lombok.Data;
import java.util.*;
@Data
public class Student {
{
private String name;
private Address address;
private String[] book;
private List<String> hobby;
private Map<String,Integer> card;
private Set<String> games;
private String girlfriend;
private Properties properties;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", book=" + Arrays.toString(book) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
", girlfriend='" + girlfriend + '\'' +
", properties=" + properties +
'}';
}
}
@Data
public class Address {
private String name;
private int id;
}
-
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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Address" class="com.mosang.pojo.Address">
<property name="name" value="山东"/>
<property name="id" value="111"/>
</bean>
<bean id="Student" class="com.mosang.pojo.Student">
<!--常规注入-->
<property name="name" value="小明"/>
<!--bean注入-->
<property name="address" ref="Address"/>
<!--数组注入-->
<property name="book">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobby">
<list>
<value>写病毒</value>
<value>读书</value>
<value>玩游戏</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="1236548978"/>
<entry key="学号" value="2018556565"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>CSGO</value>
<value>CF</value>
</set>
</property>
<!--null值注入-->
<property name="girlfriend">
<null/>
</property>
<!--Properties注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
-
环境测试
import com.mosang.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("Student");
System.out.println(student.toString());
}
}
-
结果
/* * Student{name='小明', * address=Address(name=山东, id=111), * book=[西游记, 红楼梦, 水浒传, 三国演义], * hobby=[写病毒, 读书, 玩游戏], * card={身份证=1236548978, 学号=2018556565}, * games=[LOL, CSGO, CF], * girlfriend='null', * properties={password=123456, username=root}} * */
6.3、其他方式(命名空间)
1. p-namespace: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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--xmlns:p="http://www.springframework.org/schema/p"-->
<!--p命名空间注入,可直接注入属性的值:properties(需要无参构造方法)-->
<bean id="user" class="com.mosang.pojo.user" p:name="小明" p:age="18"/>
</beans>
2. c-namespace: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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--xmlns:c="http://www.springframework.org/schema/c"-->
<!--c命名空间注入,通过构造器注入:constructor-args
必须要有有参的构造方法!
-->
<bean id="user2" class="com.mosang.pojo.user" c:name="小明" c:age="18"/>
</beans>
注意
p-namespace和c-namespace不能直接使用,需要导入xml约束
<bean
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
>
</bean>
测试:
@Test
public void ttt(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
user user = context.getBean("user", user.class);
user user1 = context.getBean("user", user.class);
System.out.println(user==user1);
}
6.4、bean的作用域
1. 单例模式(Spring默认)
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
每次从容器中get,产生的对象都是同一个
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
user user = context.getBean("user", user.class);
user user1 = context.getBean("user", user.class);
System.out.println(user==user1);
/*result:
* true
* */
2. 原型模式:
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
每次从容器中get的时候,都会产生一个新的对象
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
user user = context.getBean("user", user.class);
user user1 = context.getBean("user", user.class);
System.out.println(user==user1);
/*result:
* false
* */
3. 其他模式:request、session、application只会在web开发中使用到