你的位置:首页 > 软件开发 > Java > Spring学习笔记之 Spring IOC容器(二)

Spring学习笔记之 Spring IOC容器(二)

发布时间:2015-06-18 00:00:44
给MessageBean注入参数值 测试Spring自动组件扫描方式 如何控制ExampleBean实例化方式 使用注解方式重构JdbcDataSource, UserDAO, UserService 1 给Message ...

 

 

 

1 给MessageBean注入参数值

1.1 问题

Spring可以通过配置文件为bean注入多种类型的属性, MessageBean类用于演示Spring的多种类型数据的注入方式, 这些类型数据和注入方式包括:

1. 注入基本值。

2. 注入Bean对象(请参考Unit 1案例)。

3. 直接集合注入。

4. 引用方式集合注入

5. 注入Spring表达式值。

6. 注入null或空字符串。

1.2 步骤

步骤一:新建工程,导入Spring API jar

新建名为SouvcSpringIoC_02的web工程,在该工程导入5个jar包。

  

步骤四:修改applicationContext.

修改applicationContext.

  <bean id="messagebean" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="测试基本属性"></property>    <property name="pageSize" value="5"></property>    <property name="username" value="daliu_it"></property>    <property name="password" value="123456"></property>  </bean>

2. 修改applicationContext.

<?  ="http://www.w3.org/2001/  ="http://www.springframework.org/schema/context"  ="http://www.springframework.org/schema/jdbc"  ="http://www.springframework.org/schema/jee"  ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/data/jpa"  ="http://www.springframework.org/schema/util"  xsi:schemaLocation="    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">  <!-- 加载properties文件为bean    <util:properties id="jdbcProperties"    location="classpath:db.properties" /> -->  <bean id="messagebean" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="测试基本属性的注入"></property>    <property name="pageSize" value="5"></property>    <!-- 表达式注入 -->    <property name="username" value="daliu_it"></property>    <property name="password" value="123456"></property>    <property name="someList">      <list>        <value>北京</value>        <value>上海</value>        <value>广州</value>      </list>    </property>    <property name="someSet">      <set>        <value>James Gosling</value>        <value>Martin fowler</value>        <value>Larry Ellision</value>      </set>    </property>    <property name="someMap">      <map>        <entry key="1001" value="Java语言基础"></entry>        <entry key="1002" value="Java Web基础"></entry>        <entry key="1003" value="Spring使用基础"></entry>      </map>    </property>    <property name="someProps">      <props>        <prop key="username">root</prop>        <prop key="password">1234</prop>      </props>    </property>  </bean>  <!-- 定义集合Bean     <util:list id="oneList">    <value>Tom</value>    <value>Andy</value>    </util:list>    <util:set id="oneSet">    <value>James Gosling</value>    <value>Martin fowler</value>    </util:set>    <util:map id="oneMap">    <entry key="1001" value="Java语言基础"></entry>    <entry key="1002" value="Java Web基础"></entry>    </util:map>    <util:properties id="oneProps">    <prop key="username">root</prop>    <prop key="password">1234</prop>    </util:properties>      -->  <!-- 引用方式注入集合属性     <bean id="messagebean2" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="资费管理"></property>    <property name="pageSize" value="5"></property>    <property name="username" value="andy"></property>    <property name="password" value="123"></property>-->  <!-- 引用方式注入集合属性     <property name="someList" ref="oneList" />    <property name="someSet" ref="oneSet" />    <property name="someMap" ref="oneMap" />    <property name="someProps" ref="oneProps" />    </bean>--></beans>

2. 增加Test2类测试如上配置, Test2代码如下:

package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathimport com.souvc.bean.MessageBean;public class TestCase {  @Test  public void test1() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean", MessageBean.class);    System.out.println(messagebean.toString());    messagebean.execute();  }  @Test  public void test2() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean2", MessageBean.class);    bean.execute();  }}

3. 执行Test2 有如下结果所示:

moduleName=资费管理pageSize=5username=andypassword=123---List信息如下---TomAndy---Set信息如下---James GoslingMartin fowler---Map信息如下---1001=Java语言基础1002=Java Web基础---Properties信息如下---password=1234username=root

2. 修改applicationContext.

<!-- 加载properties文件为bean --> <util:properties id="jdbcProperties" location="classpath:db.properties" />  <bean id="messagebean" class="org.tarena.bean.MessageBean">  <!-- ================================================================ -->  <property name="moduleName" value="资费管理"></property> <property name="pageSize" value="5"></property> <!-- 表达式注入 --> <property name="username" value="#{jdbcProperties.user}"></property>  <property name="password">  <null /> </property>

Test1,Test2的完整代码如下:

package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathimport com.souvc.bean.MessageBean;public class TestCase {  @Test  public void test1() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean", MessageBean.class);    System.out.println(messagebean.toString());    messagebean.execute();  }  @Test  public void test2() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean2", MessageBean.class);    bean.execute();  }}

applicationContext.

<?  ="http://www.w3.org/2001/  ="http://www.springframework.org/schema/context"  ="http://www.springframework.org/schema/jdbc"  ="http://www.springframework.org/schema/jee"  ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/data/jpa"  ="http://www.springframework.org/schema/util"  xsi:schemaLocation="    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">  <!-- 加载properties文件为bean-->  <util:properties id="jdbcProperties"    location="classpath:db.properties" />  <bean id="messagebean" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="测试基本属性的注入"></property>    <property name="pageSize" value="5"></property>    <!-- 表达式注入       <property name="username" value="daliu_it"></property>-->    <property name="username" value="#{jdbcProperties.user}"></property>    <property name="password" >    <null/>    </property>    <property name="someList">      <list>        <value>北京</value>        <value>上海</value>        <value>广州</value>      </list>    </property>    <property name="someSet">      <set>        <value>James Gosling</value>        <value>Martin fowler</value>        <value>Larry Ellision</value>      </set>    </property>    <property name="someMap">      <map>        <entry key="1001" value="Java语言基础"></entry>        <entry key="1002" value="Java Web基础"></entry>        <entry key="1003" value="Spring使用基础"></entry>      </map>    </property>    <property name="someProps">      <props>        <prop key="username">root</prop>        <prop key="password">1234</prop>      </props>    </property>  </bean>  <!-- 定义集合Bean -->  <util:list id="oneList">    <value>Tom</value>    <value>Andy</value>  </util:list>  <util:set id="oneSet">    <value>James Gosling</value>    <value>Martin fowler</value>  </util:set>  <util:map id="oneMap">    <entry key="1001" value="Java语言基础"></entry>    <entry key="1002" value="Java Web基础"></entry>  </util:map>  <util:properties id="oneProps">    <prop key="username">root</prop>    <prop key="password">1234</prop>  </util:properties>  <!-- 引用方式注入集合属性 -->  <bean id="messagebean2" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="资费管理"></property>    <property name="pageSize" value="5"></property>    <property name="username" value="andy"></property>    <property name="password" value="123"></property>    <!-- 引用方式注入集合属性 -->    <property name="someList" ref="oneList" />    <property name="someSet" ref="oneSet" />    <property name="someMap" ref="oneMap" />    <property name="someProps" ref="oneProps" />  </bean></beans>

源码如下:http://yunpan.cn/cQVU3sLcMzXGC  访问密码 daf3

 

2 测试Spring自动组件扫描方式

2.1 问题

如何使用组件扫描的方式和"注解标记"配合获取容器中的ExampleBean对象。

2.2 方案

使用组件扫描,首先需要在

 

  <context:component-scan          base-package="com.souvc"/> 

步骤二: 修改applicationContext.

修改applicationContext. 

TestCase 类的完整代码如下所示:

package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathimport com.souvc.bean.ExampleBean;import com.souvc.bean.MessageBean;public class TestCase {  @Test  public void test1() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean", MessageBean.class);    System.out.println(messagebean.toString());    messagebean.execute();  }  @Test  public void test2() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean2", MessageBean.class);    bean.execute();  }  @Test  public void test3() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("exampleBean", ExampleBean.class);    bean.execute();  }}

applicationContext.

<?  ="http://www.w3.org/2001/  ="http://www.springframework.org/schema/context"  ="http://www.springframework.org/schema/jdbc"  ="http://www.springframework.org/schema/jee"  ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/data/jpa"  ="http://www.springframework.org/schema/util"  xsi:schemaLocation="    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">  <!-- 加载properties文件为bean-->  <util:properties id="jdbcProperties"    location="classpath:db.properties" />  <bean id="messagebean" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="测试基本属性的注入"></property>    <property name="pageSize" value="5"></property>    <!-- 表达式注入       <property name="username" value="daliu_it"></property>-->    <property name="username" value="#{jdbcProperties.user}"></property>    <property name="password">      <null />    </property>    <property name="someList">      <list>        <value>北京</value>        <value>上海</value>        <value>广州</value>      </list>    </property>    <property name="someSet">      <set>        <value>James Gosling</value>        <value>Martin fowler</value>        <value>Larry Ellision</value>      </set>    </property>    <property name="someMap">      <map>        <entry key="1001" value="Java语言基础"></entry>        <entry key="1002" value="Java Web基础"></entry>        <entry key="1003" value="Spring使用基础"></entry>      </map>    </property>    <property name="someProps">      <props>        <prop key="username">root</prop>        <prop key="password">1234</prop>      </props>    </property>  </bean>  <!-- 定义集合Bean -->  <util:list id="oneList">    <value>Tom</value>    <value>Andy</value>  </util:list>  <util:set id="oneSet">    <value>James Gosling</value>    <value>Martin fowler</value>  </util:set>  <util:map id="oneMap">    <entry key="1001" value="Java语言基础"></entry>    <entry key="1002" value="Java Web基础"></entry>  </util:map>  <util:properties id="oneProps">    <prop key="username">root</prop>    <prop key="password">1234</prop>  </util:properties>  <!-- 引用方式注入集合属性 -->  <bean id="messagebean2" class="com.souvc.bean.MessageBean">    <property name="moduleName" value="资费管理"></property>    <property name="pageSize" value="5"></property>    <property name="username" value="andy"></property>    <property name="password" value="123"></property>    <!-- 引用方式注入集合属性 -->    <property name="someList" ref="oneList" />    <property name="someSet" ref="oneSet" />    <property name="someMap" ref="oneMap" />    <property name="someProps" ref="oneProps" />  </bean>  <!-- 组件扫描 -->  <context:component-scan base-package="com.souvc" /></beans>

 

3 如何控制ExampleBean实例化方式

3.1 问题

使用组件扫描的方式,重构如何控制ExampleBean实例化为单例或非单例模式。

3.2 方案

1. 通常受Spring管理的组件,默认的作用域是"singleton"。如果需要其他的作用域可以使用@Scope注解,只要在注解中提供作用域的名称即可,代码如下:

  @Component  @Scope("singleton")  public class ExampleBean {    ...    }

2. @PostConstruct和@PreDestroy注解标记分别用于指定初始化和销毁回调方法,代码如下:

  public class ExampleBean {      @PostConstruct      public void init() {        //初始化回调方法     }           @PreDestroy      public void destroy() {         //销毁回调方法      }   }

通过前面的讲解,了解到默认情况下Spring容器是通过单例模式创建Bean对象的。通过本案例的运行结果,可以看出使用原型方式,调用了2次ExampleBean类的构造方法,说明创建了2个ExampleBean对象。

4. 如果想要使用单例模式创建对象,也可以使用注解的方式进行显示标注,修改ExampleBean类的代码所示:

package com.souvc.bean;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component//@Scope("prototype")@Scope("singleton")public class ExampleBean {  public ExampleBean() {    System.out.println("实例化ExampleBean");  }//  public void execute() {//    System.out.println("执行ExampleBean处理");//  }////  @PostConstruct//  public void init() {//    System.out.println("初始化ExampleBean对象");//  }////  @PreDestroy//  public void destroy() {//    System.out.println("销毁ExampleBean对象");//  }}
 

上述运行结果可以看得出,两个ExampleBean对象,通过比较操作符“ ==”进行比较的输出结果为true,说明Spring容器是通过单例模式创建Bean对象的。

步骤二:Bean对象的初始化和销毁

1. 修改ExampleBean类,加入方法init和方法destroy,并使用注解“@PostConstruct”和注解“@PreDestroy”指定init为初始化的回调方法、destroy为销毁的回调方法,代码如下所示:

package com.souvc.bean;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component//@Scope("prototype")@Scope("singleton")public class ExampleBean {  public ExampleBean() {    System.out.println("实例化ExampleBean");  }  public void execute() {    System.out.println("执行ExampleBean处理");  }  @PostConstruct  public void init() {    System.out.println("初始化ExampleBean对象");  }  @PreDestroy  public void destroy() {    System.out.println("销毁ExampleBean对象");  }}

2.运行Test4类,控制台输出结果所示:

实例化ExampleBean初始化ExampleBean对象true2015-6-17 14:26:05 org.springframework.context.support.AbstractApplicationContext doClose信息: Closing org.springframework.context.support.ClassPath17 14:26:04 CST 2015]; root of context hierarchy2015-6-17 14:26:05 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@36f0b7f8: defining beans [jdbcProperties,messagebean,oneList,oneSet,oneMap,oneProps,messagebean2,exampleBean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy销毁ExampleBean对象

Test4类的完整代码如下所示:

package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathimport com.souvc.bean.ExampleBean;import com.souvc.bean.MessageBean;public class TestCase {  @Test  public void test1() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean", MessageBean.class);    System.out.println(messagebean.toString());    messagebean.execute();  }  @Test  public void test2() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("messagebean2", MessageBean.class);    bean.execute();  }  @Test  public void test3() {    String conf = "applicationContext.;    ApplicationContext ac = new ClassPath= ac.getBean("exampleBean", ExampleBean.class);    //bean.execute();  }  @Test  public void test4() {    String conf = "applicationContext.;    AbstractApplicationContext ac = new ClassPath= ac.getBean("exampleBean", ExampleBean.class);    ExampleBean bean2 = ac.getBean("exampleBean", ExampleBean.class);    System.out.println(bean1 == bean2);    ac.close();  }}

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Spring学习笔记之 Spring IOC容器(二)

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录