Spring中的IOC
依赖关系的管理:以后都交给spring来维护在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
- 把对象的创建交给spring来管理,对象创建完成后放入到SpringIOC容器中。
- Bean:是一种JAVA语言写成的可重用组件。写成Bean,类必须是具体的和公共的,并且具有无参数的构造器,即java类,通常把需要进行控制反转的类叫做Bean。
ApplicationContext的三个常用实现类
- ClassPathXmlApplicationContext:可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了。
- FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件(必须有访问权限)。
- AnnotationConfigApplicationContext:是用于读取注解创建容器的核心容器的两个接口引发的问题 。
Spring对Bean的管理细节
1、创建bean对象的三种方式
第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
①bean
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("service中的saveAccount方法执行了。。。。");
}
}
②xml文件
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" name="account"></bean>
③main中创建
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//按id装载
IService service = (IService) context.getBean("accountService");
//按类型装载
IService service1 = (IService) context.getBean(AccountServiceImpl.class);
//按name装载
IService service2 = (IService) context.getBean("account");
}
第二种方式:模拟一个工厂类(有可能没有默认的构造方法,该类在jar包中,所以不能修改)通过使用普通工厂中的方法创建对象。
①bean
public class InstanceFactory {
//使用工厂创建AccountServiceImpl的对象
public IAccountService getAccountService() {
return new AccountServiceImpl();
}
}
②xml文件
<!--目标对象的工厂类-->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<!--spring创建工厂类,并且声明这个工厂类,通过调用这个工厂类的方法,为我们创建目标对象-->
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
③main中创建
public static void main(String[] args) {
//创建spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//由spring创建对象,通过getBean()方法拿出来
IService service = (IService) context.getBean("accountService");
}
第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
①bean
public class StaticFactory {
//工厂类使用静态方法创建对象
public static IAccountService getAccountService() {
return new AccountServiceImpl();
}
}
②xml文件
<!--直接让class设置为目标对象的工厂类,并且声明工厂中的创建目标对象的静态方法-->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
③main中创建
public static void main(String[] args) {
//创建spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//由spring创建对象,通过getBean()方法拿出来
IService service = (IService) context.getBean("accountService");
}
2、bean对象的作用范围
bean标签的scope属性
作用:用于指定bean的作用范围
取值: 常用的就是单例的和多例的
- singletion:单例的(默认值)
- prototype:多例的
- request:作用于web应用的请求范围
- session:作用于web应用的会话范围
- global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
3、bean对象的而生命周期
bean对象的生命周期
单例对象
- 出生:当容器创建时,对象出生
- 活着:只要容器还在,对象一直活着
- 死亡:容器销毁,对象消亡
- 单例对象的生命周期和容器相同
多例对象
- 出生:当我们使用对象时spring为我们创建
- 活着:对象只要是在使用过程中就一直活着
- 死亡:当对象长时间不用且没有别的对象引用时,由Java的垃圾回收器回收
Spring中的DI
- 基本类型和spring
- 其他bean类型(在配置文件中或者注解配置过的bean)
- 复杂类型/集合类型
2、注入的方式:有三种
-
第一种:使用构造函数提供
-
第二种;使用set方法提供
-
第三种:使用注解提供
依赖注入的三种方式:
第一种方式:构造函数注入
1、使用的标签:constructor-arg
2、标签中的属性:
- name:用于指定注入时所调用的set方法名称
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据。它指的就是spring的IOC核心容器中出现过的bean对象
①要注入的实体类
public class AccountServiceImpl implements IAccountService {
//如果时经常变化的数据,并不适用于注入的方式
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public void saveAccount() {
System.out.println("service中的saveAccount方法执行了。。。。" + name + "," + age + "," + birthday);
}
}
在这里插入代码片
②xml文件
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="test"></constructor-arg>
<!--在xml文件中,都是字符串的,所以18也是个字符串,而Spring可以把这些转换成我们要的 Integer-->
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
这里注意:对于id为 now 的 bean, Spring读取java.util.Date全限定类名,反射创建一个对象,并且存入Spring核心容器中,我们用now可以调出此对象。所以上面注入birthday注入的时候,用now。
第二种方式:set方法注入
1、使用的标签:property
2、标签中的属性:
- name:用于指定给构造函数中指定名称的参数赋值 常用的
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据。它指的就是spring的IOC核心容器中出现过的bean对象
①要注入的实体类
public class AccountServiceImpl2 implements IAccountService {
//如果时经常变化的数据,并不适用于注入的方式
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void saveAccount() {
System.out.println("service中的saveAccount方法执行了。。。。" + name + "," + age + "," + birthday);
}
}
②xml文件
<bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2"> <property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
第三种方式:复杂类型的注入/集合类型的注入
1、使用的标签:
- 用于给List结构集合注入的标签:list, array, set
- 用于给Map结构集合注入的标签:map, props
结构相同,标签可以互换
①要注入的实体类
public class AccountServiceImpl3 implements IAccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String, String> myMap;
private Properties myprops;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMyset(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyprops(Properties myprops) {
this.myprops = myprops;
}
public void saveAccount() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myprops);
}
}
②xml文件
<bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
<property name="myStrs">
<array>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</array>
</property>
<property name="myList">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="myset">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
<property name="myprops">
<props>
<prop key="testC">ccc</prop>
<prop key="testD">ddd</prop>
</props>
</property>
</bean>