文章目录
spring构建核心容器
文章内容是根据最近看的一些教程和文章所做的笔记,非常感谢大家的无私奉献。
ApplicationContext和BeanFactory的区别:
- ApplicationContext:单例对象适用。在构建核心容器时,创建对象采取的策略是立即加载的方式。只要读取完配置文件,就马上创建配置文件中配置的对象。
- BeanFactory:多例对象适用。在构建核心容器时,创建对象采取的策略是延迟加载。什么时候根据getBean获取对象,什么时候才真正的创建对象。
ApplicationContext的三个常用实现类: - ClassPathXmlApplicationContext:可以加载类类路径下的配置文件,要求配置文件必须在类路径下。
- FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件,但是需要有访问权限。
- AnnotationConfigApplicationContext:可以用于读取注解创建容器。
Spring对bean的管理
创建bean的方式
业务层代码:
public class AccountService{
public AccountService(){
}
}
工厂类代码:
public class Factory {
//普通方法
public AccountService getAccountService(){
return new AccountService();
}
//静态方法
public static AccountService getAccountService2(){
return new AccountService();
}
}
默认构造函数
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数,若没有默认构造函数,则对象并无法通过此方式创建。
<bean id="accountService" class="cn.wtt.service.AccountService"></bean>
工厂类的普通方法
有时候无法修改某些类的构造函数,通过使用某个类中的方法创建对象,并存入容器。
<bean id="factory" class="cn.wtt.factory.Factory"></bean>
<bean id="accountService" factory-bean="factory" factory-method="getAccountService"></bean>
工厂类的静态方法
通过使用某个类中的静态方法来创建对象,并存入容器。
<bean id="accountService" class="cn.wtt.factory.Factory" factory-method="getAccountService2"></bean>
bean对象的作用范围
scope属性用于指定作用范围.
取值:
- singleton:单例
- prototype:多例
- request:作用于web应用的请求
- session:作用于web的会话范围
- global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
bean对象的生命周期
单例对象,singleton。当容器创建时,对象创建。与容器的生命周期一样。
多例对象,prototype。当需要用到对象时,容器创建。当对象长时间不用且没有被引用时,会被垃圾回收机制处理。
Spring的Dependency Injection
代码:
public class AccountService2 {
private String name;
private Integer age;
private Date birthday;
//构造函数
public AccountService2(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = 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 String toString() {
return "AccountService2{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
测试代码:
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
AccountService as=(AccountService)ac.getBean("accountService2");
System.out.println(as.toString());
}
}
构造函数注入
标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性:
- type:用于指定要注入的数据的数据类型,该数据类型是构造函数中某个参数的类型
- index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,从0开始
- name:用于指定给构造函数中指定名称的参数赋值
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据,它指的是在spring中IOC核心容器出现过的bean
<bean id="accountService2" class="cn.wtt.service.AccountService2">
<constructor-arg name="name" value="lhw"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="date"></constructor-arg>
</bean>
<bean id="date" class="java.util.Date"></bean>
结果如下:
Set方法注入
property:bean标签的内部
- name:用于指定给构造函数中指定名称的参数赋值
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据,它指的是在spring中IOC核心容器出现过的bean
<bean id="accountService2" class="cn.wtt.service.AccountService2">
<property name="name" value="wtt"></property>
<property name="age" value="18"></property>
<property name="birthday" ref="date"></property>
</bean>
接口注入
接口注入还没有了解。
Spring依赖注入数据类型
- 基本类型和String
- 其他bean类型(在配置文件中配置过的bean)
- 集合类型
前两种类型已经用过了,主要是第三种集合类型的注入。
代码:
public class AccountService3{
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 show() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}
测试代码:
public class Client {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
AccountService as=(AccountService)ac.getBean("accountService3");
as.show();
}
}
复杂类型注入方式:
<bean id="accountService3" class="cn.wtt.service.impl.AccountServiceImpl2">
<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>
结果如下:
总结
用于给List结构集合注入的标签: list array set
用于给Map结构注入的标签: map props
结构相同,标签可以互换。