王朝网络
分享
 
 
 

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

王朝学院·作者佚名  2016-05-28  
宽屏版  字体: |||超大  

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

本节主要内容: 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.jarspring-core.jarspring-context.jarspring-beans.jarspring-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方法获取这几个属性的信息,代码如下所示:

private List<String> someList;private Set<String> someSet;private Map<String,Object> someMap;private Properties someProps;

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有