本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构JdbcDataSource, UserDAO, UserService
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包。
commons-logging.jar
spring-core.jar
spring-context.jar
spring-beans.jar
spring-expression.jar
网盘下载jar包 :http://yunpan.cn/cQJhPMPRZeLH7 访问密码 2bf8步骤二:新建Spring配置文件
在src目录下,新建Spring配置文件applicationContext.xml。该文件名为Spring默认的配置文件名,也可以自由定义名称。
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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"></beans>
步骤三:新建类MessageBean 添加基本值属性
新建类MessageBean,在该类中添加如下代码中的四个属性,并生成这几个属性对应的getter和setter方法;最后在execute方法获取这几个属性的信息,代码如下所示:
package com.souvc.bean;public class MessageBean { private String moduleName; private int pageSize; private String username; private String passWord = ""; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String execute() { System.out.println("moduleName=" + moduleName); System.out.println("pageSize=" + pageSize); System.out.println("username=" + username); System.out.println("password=" + password); System.out.println("---List信息如下---"); return "success"; }}
步骤四:修改applicationContext.xml文件, 增加属性基本值注入
修改applicationContext.xml文件,为MessageBean的四个属性注入基本参数值,代码如图-3所示:
<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>
步骤五:新建类Test1, 添加测试方法获取MessageBean对象测试注入结果
新建类Test1,在类中使用ApplicationContext的方式实例化Spring容器,获取MessageBean对象,并调用execute方法。
@Test public void test1() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); System.out.println(ac); MessageBean messagebean = ac.getBean("messagebean", MessageBean.class); System.out.println(messagebean.toString()); messagebean.execute(); }
步骤六:运行Test1类, 测试注入结果
运行Test1类,控制台输入结果
org.springframework.context.support.ClassPathXmlApplicationContext@65b4fad5: startup date [Wed Jun 17 11:16:13 CST 2015]; root of context hierarchycom.souvc.bean.MessageBean@5d3e754fmoduleName=测试基本属性pageSize=5username=daliu_itpassword=123456---List信息如下---
控制台输出所示的信息,说明获取到了注入的基本属性值。
步骤七:为MessageBean添加集合属性,用于注入集合测试
1. 修改类MessageBean,在该类中添加如下加粗代码中的四个属性,并生成这几个属性对应的getter和setter方法;最后在execute方法获取这几个属性的信息,代码如下所示:
package com.souvc.bean;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class MessageBean { private String moduleName; private int pageSize; private String username; private String password = ""; private List<String> someList; private Set<String> someSet; private Map<String,Object> someMap; private Properties someProps; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<String> getSomeList() { return someList; } public void setSomeList(List<String> someList) { this.someList = someList; } public Set<String> getSomeSet() { return someSet; } public void setSomeSet(Set<String> someSet) { this.someSet = someSet; } public Map<String, Object> getSomeMap() { return someMap; } public void setSomeMap(Map<String, Object> someMap) { this.someMap = someMap; } public Properties getSomeProps() { return someProps; } public void setSomeProps(Properties someProps) { this.someProps = someProps; } public String execute() { System.out.println("moduleName=" + moduleName); System.out.println("pageSize=" + pageSize); System.out.println("username=" + username); System.out.println("password=" + password); System.out.println("---List信息如下---"); for(String s : someList){ System.out.println(s); } System.out.println("---Set信息如下---"); for(String s : someSet){ System.out.println(s); } System.out.println("---Map信息如下---"); Set<String> keys = someMap.keySet(); for(String key : keys){ System.out.println(key+"=" +someMap.get(key)); } System.out.println("---Properties信息如下---"); Set<Object> keys1 = someProps.keySet(); for(Object key : keys1){ System.out.println(key+"=" +someProps.getProperty(key.toString())); } return "success"; }}
2. 修改applicationContext.xml文件,为MessageBean的四个属性注入集合参数值:
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="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>
3. 运行Test1类,控制台输出结果所示:
org.springframework.context.support.ClassPathXmlApplicationContext@42704baa: startup date [Wed Jun 17 11:30:43 CST 2015]; root of context hierarchycom.souvc.bean.MessageBean@25961581moduleName=测试基本属性的注入pageSize=5username=daliu_itpassword=123456---List信息如下---北京上海广州---Set信息如下---James GoslingMartin fowlerLarry Ellision---Map信息如下---1001=Java语言基础1002=Java Web基础1003=Spring使用基础---Properties信息如下---password=1234username=root
控制台输出的信息,说明只需要通过配置Spring就可以为Bean注入集合参数值。
步骤八:测试引用方式集合注入
1.为Spring配置文件applicationContext.xml增加如下配置, 采用引用方式注入集合对象, 代码参考如下:
<!-- 定义集合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>
2. 增加Test2类测试如上配置, Test2代码如下:
package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.souvc.bean.MessageBean;public class TestCase { @Test public void test1() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); System.out.println(ac); MessageBean messagebean = ac.getBean("messagebean", MessageBean.class); System.out.println(messagebean.toString()); messagebean.execute(); } @Test public void test2() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); MessageBean bean = 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
这个结果说明, 通过引用方式也可以为bean注入集合属性.
步骤九:利用 Spring表达式注入属性值
1.在工程的src下,新建属性文件db.properties,文件内容如下:
user=scott
2. 修改applicationContext.xml文件,注入Spring表达式值,代码所示:
<!-- 加载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>
3. 运行Test1类,控制台输出结果所示:
org.springframework.context.support.ClassPathXmlApplicationContext@68861f24: startup date [Wed Jun 17 11:41:35 CST 2015]; root of context hierarchycom.souvc.bean.MessageBean@209444d1moduleName=测试基本属性的注入pageSize=5username=daliu_itpassword=123456---List信息如下---北京上海广州---Set信息如下---James GoslingMartin fowlerLarry Ellision---Map信息如下---1001=Java语言基础1002=Java Web基础1003=Spring使用基础---Properties信息如下---password=1234username=root
控制台输出信息,说明获取到了注入的Spring表达式值。
步骤十:注入null值
1. 修改applicationContext.xml文件,将属性password注入null值,代码如图-11所示:
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="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>
2. 运行Test1类,控制台输出结果:
org.springframework.context.support.ClassPathXmlApplicationContext@2996c1b0: startup date [Wed Jun 17 11:42:47 CST 2015]; root of context hierarchycom.souvc.bean.MessageBean@195ed659moduleName=测试基本属性的注入pageSize=5username=daliu_itpassword=null---List信息如下---北京上海广州---Set信息如下---James GoslingMartin fowlerLarry Ellision---Map信息如下---1001=Java语言基础1002=Java Web基础1003=Spring使用基础---Properties信息如下---password=1234username=root
控制台输出所示的信息,说明获取到了注入的null值。
1.3 完整代码MessageBean类的完整代码如下所示:
package com.souvc.bean;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class MessageBean { private String moduleName; private int pageSize; private String username; private String password = ""; private List<String> someList; private Set<String> someSet; private Map<String, Object> someMap; private Properties someProps; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<String> getSomeList() { return someList; } public void setSomeList(List<String> someList) { this.someList = someList; } public Set<String> getSomeSet() { return someSet; } public void setSomeSet(Set<String> someSet) { this.someSet = someSet; } public Map<String, Object> getSomeMap() { return someMap; } public void setSomeMap(Map<String, Object> someMap) { this.someMap = someMap; } public Properties getSomeProps() { return someProps; } public void setSomeProps(Properties someProps) { this.someProps = someProps; } public String execute() { System.out.println("moduleName=" + moduleName); System.out.println("pageSize=" + pageSize); System.out.println("username=" + username); System.out.println("password=" + password); System.out.println("---List信息如下---"); for (String s : someList) { System.out.println(s); } System.out.println("---Set信息如下---"); for (String s : someSet) { System.out.println(s); } System.out.println("---Map信息如下---"); Set<String> keys = someMap.keySet(); for (String key : keys) { System.out.println(key + "=" + someMap.get(key)); } System.out.println("---Properties信息如下---"); Set<Object> keys1 = someProps.keySet(); for (Object key : keys1) { System.out.println(key + "=" + someProps.getProperty(key.toString())); } return "success"; }}
Test1,Test2的完整代码如下:
package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.souvc.bean.MessageBean;public class TestCase { @Test public void test1() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); System.out.println(ac); MessageBean messagebean = ac.getBean("messagebean", MessageBean.class); System.out.println(messagebean.toString()); messagebean.execute(); } @Test public void test2() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); MessageBean bean = ac.getBean("messagebean2", MessageBean.class); bean.execute(); }}
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="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 方案使用组件扫描,首先需要在XML配置中指定扫描类路径,配置代码如下:
<context:component-scan base-package="com.souvc"/>
上述配置,容器实例化时会自动扫描com.souvc 包及其子包下所有组件类。
指定扫描类路径后,并不是该路径下所有组件类都扫描到Spring容器的,只有在组件类定义前面有以下注解标记时,才会扫描到Spring容器。
@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。@Name 通用注解@Service 业务层组件注解, 用于标注业务层组件@Repository 持久层组件注解,用于标注数据访问组件,即DAO组件@Controller 控制层组件注解,用于标注控制层组件(如struts中的action)2.3 步骤
步骤一:新建类ExampleBean
新建类ExampleBean,在该类前使用通用组件注解“@Component”,表明ExampleBean为可被扫描的组件,代码如下所示:
package com.souvc.bean;import org.springframework.stereotype.Component;@Componentpublic class ExampleBean { public ExampleBean() { System.out.println("实例化ExampleBean"); } public void execute() { System.out.println("执行ExampleBean处理"); }}
步骤二: 修改applicationContext.xml
修改applicationContext.xml,使容器实例化时自动扫描org.tarena包及其子包下所有组件类
<!-- 组件扫描 --> <context:component-scan base-package="com.souvc" />
步骤三: 新建方法Test3
@Test public void test3() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); ExampleBean bean = ac.getBean("exampleBean", ExampleBean.class); bean.execute(); }
运行Test3方法,控制台输出结果:
实例化ExampleBean执行ExampleBean处理控制台输出上述结果,说明Spring会自动使用组件扫描的方式创建ExampleBean实例, 并且Test3中获取到了这个ExampleBean对象。
2.4 完整代码ExampleBean类的完整代码如下所示:
package com.souvc.bean;import org.springframework.stereotype.Component;@Componentpublic class ExampleBean { public ExampleBean() { System.out.println("实例化ExampleBean"); } public void execute() { System.out.println("执行ExampleBean处理"); }}
TestCase 类的完整代码如下所示:
package com.souvc.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.souvc.bean.ExampleBean;import com.souvc.bean.MessageBean;public class TestCase { @Test public void test1() { String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); System.out.println(ac); MessageBean messagebean = ac.getBean("messagebean", MessageBean.class); System.out.println(messagebean.toString()); messagebean.execute(); } @Test public void test2() { String c
新闻热点
疑难解答