本文章用于总结自己学习知识,有不足或错误之处清谅解。 bean.xml 文件的读取方式: ClassPathXmlApplicationContext: 它是只能加载类路径下的配置文件 推荐 1.加载配置文件: ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 2.根据bean的id获取对象 ICustomerService cs1 = (ICustomerService) ac.getBean("customerService"); FileSystemXmlApplicationContext: 它是可以加载磁盘任意位置的配置文件
Bean创建的两种规则: BeanFactory: 提供的是一种延迟加载思想来创建bean对象。即:bean对象什么时候用,什么时候创建
ApplicationContext: 提供的是一种立即加载思想来创建bean对象。即:只要一解析完配置文件,就创建bean对象
1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 2.根据bean的id获取对象 ICustomerService cs1 = (ICustomerService) ac.getBean("customerService"); cs1.saveCustomer();
Bean的作用范围: 它是可以通过配置的方式来调整作用范围 配置的属性:bean标签的scope属性 取值: singleton :单例的(默认值) prototype :多例的(当我们让spring接管struts2的action创建时,action必须配置此值) request :作用范围是一次请求,和当前请求的转发 session :作用范围是一次会话 globalsession :作用范围是一次全局会话 Bean的生命周期: 设计bean标签的两个属性: init-method destroy-method 单例: 出生:容器创建,对象出生 活着:只要容器在,对象就在 死亡:容器销毁,对象消亡 多例: 出生:每次使用时,创建对象 活着:只要对象在使用中,就一直活着 死亡:当对象长时间不使用,并且也没有别的对象引用时,由java的垃圾回收器回收
Bean的三种创建方式: 第一种方式:调用默认无参构造函数创建 常用 默认情况下,如果没有默认无参构造函数,则创建失败,会报异常 bean.xml中配置: <bean id="customerService" class="spring.service.impl.CustomerServiceImpl" scope="singleton" init-method="init" destroy-method="destory"></bean> CustomerServiceImpl.java中配置: public CustomerServiceImpl(){ System.out.println("bean对象创建了"); } public void init(){ System.out.println("对象初始化"); } public void destory(){ System.out.println("对象消亡"); }
第二种方式:使用静态工厂中的方法创建对象 需要使用bean标签的factory-method属性,指定静态工厂中创建对象的方法 配置使用静态工厂创建bean对象 使用class中的factory-method创建对象,用id来提取 bean.xml中配置: <bean id="staticCustomerService" class="spring.factory.staticFactory" factory-method="getCustomerService"></bean>
staticFactory.java中配置: public static ICustomerService getCustomerService(){ return new CustomerServiceImpl(); }
第三种方式:使用示例工厂中的方法创建.
需要使用bean标签的factory-method属性,指定示例工厂中创建对象的方法 配置使用实例工厂创建bean对象
bean.xml中配置: <bean id="instanceFactory" class="spring.factory.instanceFactory"></bean> <bean id="instanceCustomerService" class="spring.factory.instanceFactory" factory-method="getCustomerService"></bean> instanceFactory.java中配置: public static ICustomerService getCustomerService(){ return new CustomerServiceImpl(); }
Spring的依赖注入方式: 注入的方式有三种: 第一种:使用构造函数注入 第二种:使用set方法注入 第三种:使用注解注入 使用构造函数注入:
涉及的标签:constructor-arg 标签的属性: type: 指定参数的类型 index: 指定参数的索引位置,从0开始 name: 指定参数的名称 (常用) ========上面三个属性是指定给哪个参数赋值的,下面两个属性是指定赋什么值的======== value: 指定基本数据类型或String类型的数据 ref: 指定其他bean类型数据 标签出现的位置:bean标签内部 bean.xml中的配置:
<bean id="customerService" class="spring.service.impl.CustomerServiceImpl"> <constructor-arg name="driver" value="com.mysql.jdbc.Driver"></constructor-arg> <constructor-arg name="port" value="3306"></constructor-arg> <constructor-arg name="today" ref="now"></constructor-arg> </bean> 由于上文的配置使用了ref,所以需要在bean.xml中配置ref所用的类型: <bean id="now" class="java.util.Date"></bean>
使用set方法注入:
涉及的标签:property 标签的属性: name: 指定参数的set方法名称 value: 指定基本数据类型或String类型的数据 ref: 指定其他bean类型数据 标签出现的位置:bean标签内部 注意:使用set方法注入需要注入的目标拥有set方法,否则配置会出现错误。(spring注入错误可以先检查这里) bean.xml中文件配置:
<bean id="customerService" class="spring.service.impl.CustomerServiceImpl"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="port" value="3307"></property> <property name="today" ref="now"></property> </bean> <bean id="now" class="java.util.Date"></bean> CustomerServiceImpl.java中需要有:
public void setDriver(String driver) { this.driver = driver; } public void setPort(Integer port) { this.port = port; } public void setToday(Date today) { this.today = today; }
使用注解注入:在day_02中介绍 数据类型的注入: 注入的数据类型有3类: 第一类:基本类型和String类型 第二类:其他bean类型(必须是在spring的配置文件中出现过的bean) 第三类:复杂类型(集合类型)
<bean id="customerService" class="spring.service.impl.CustomerServiceImpl"> <property name="driver" value="com.mysql.jdbc.Driver"></property> <property name="port" value="3307"></property> <property name="today" ref="now"></property> </bean> <bean id="now" class="java.util.Date"></bean> 如上例中:value则是属于第一类中的String类型,ref中则是第二类,由于ref是其他bean类型,所以需要在下面配置ref中now这个bean类型。
第三类:复杂类型的注入 结构相同,标签可以互换 如:map和properties可以互换 list,array,set可以两两交换
<bean id="customerService" class="spring.service.impl.CustomerServiceImpl"> <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="testD" value="DDD"></entry> <entry key="testE"> <value>EEE</value> </entry> <!--上面两种表示方法都可以--> </map> </property> <property name="myProps"> <props> <prop key="testF">FFF</prop> <prop key="testG">GGG</prop> </props> </property> </bean> 交换标签后 <property name="myStrs"> <list> <value>AAA</value> <value>BBB</value> <value>CCC</value> </list> </property> <property name="myList"> <array> <value>AAA</value> <value>BBB</value> <value>CCC</value> </array> </property> <property name="myMap"> <map> <props> <prop key="testF">FFF</prop> <prop key="testG">GGG</prop> </props> </map> </property> <property name="myProps"> <entry key="testD" value="DDD"></entry> <entry key="testE"> <value>EEE</value> </entry> </property>