Spring Framework------>version4.3.5.RELAESE----->应用实例------->测试spring framework的IOC功能
step1,确保你的project中已经包含了spring的beans/context/core/expression、三方logging模块,
-
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>bupt.jinmensuyin</groupId> <artifactId>TestSpring</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>TestSpring Maven Webapp</name> <url>http://maven.apache.org</url> <dependencyManagement> <dependencies> <!-- BOM:bill of materials,其实就是一个列表,该列表中描述了spring framework各个模块及其唯一的版本号, 引入BOM之后,下面再引入spring framework的各个module时,就可以省略<version>标签,maven会自动查询BOM来 得知应该引入该module的哪个版本 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>4.3.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring framework的spring-context模块提供依赖注入功能 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <exclusions> <!-- exclusion:排除 这里的配置将commons-logging这个三方日志库文件从spring-core模块中排除出去, 这样一来spring framework的所有模块都没有了logging插件 (为什么Exclude the dependency from the spring-core module就等于从整个spring framework 的所有module中都排除了commons-logging? 答:因为the spring-core module is the only module that explicitly显式地 depends on commons-logging,spring framework的其他module都是基于spring-core的, 其他module通过spring-core获得commons-logging提供的功能)) --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 排除了spring framework中本身带有的commons-logging之后,要继承其他的三方log插件来管理日志 You need to supply 4 dependencies: the bridge, the SLF4J API, the binding to Log4J, and the Log4J implementation itself.--> <!-- 在spring framework和SLF4J之间架起桥梁,使得logging calls from within Spring will be translated into logging calls to the SLF4J API, --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.5.8</version> </dependency> <!-- the SLF4J API, --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <!-- 在SLF4J和Log4J之间架起桥梁 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> </dependencies> <build> <finalName>TestSpring</finalName> </build> <repositories> <!-- 添加了如下配置之后,就不从maven中央仓库下载相关jar包,而是从如下url下载相关jar包 到maven本地仓库 --> <repository> <id>io.spring.repo.maven.release</id> <url>http://repo.spring.io/release/</url> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> </project>
-
step2,创建bean类(这三个文件放在“工程名/src/test/java”文件夹下)
-
-
package java; public interface Person { public String sayHello(String name); public String sayGoodBey(String name); }
package java; public class Chinese implements Person { @Override public String sayHello(String name) { // TODO Auto-generated method stub return "你好 "+name; } @Override public String sayGoodBey(String name) { // TODO Auto-generated method stub return "再见 "+name; } }
package java; public class American implements Person { @Override public String sayHello(String name) { // TODO Auto-generated method stub return "hello "+name; } @Override public String sayGoodBey(String name) { // TODO Auto-generated method stub return "GoodBey "+name; } }
-
step3,使用spring的IOC容器管理bean
-
- 本例中使用的是xml-based configuration
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="american" class="java.American"/> <bean name="chinese" class="java.Chinese"/> </beans>
该配置文件存放路径——工程名/src/test/resources/SpringMVC/ctx-cache.xml
- 本例中使用的是xml-based configuration
step4,初始化IOC容器,并且获取其中定义的bean,并做出后续操作
-
- 工程名/src/test/java/TestIoC.java
package java; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class TestIoC { public static void main(String[] args){ ApplicationContext ctx=new FileSystemXmlApplicationContext("src/resources/SpringXML/ctx-cache.xml"); Person p=(Person) ctx.getBean("american"); System.out.println(p.sayHello("lxrm")); p=(Person) ctx.getBean("chinese"); System.out.println(p.sayHello("陈")); } }
运行上述程序,应该输出结果(现在有点bug)
- 工程名/src/test/java/TestIoC.java
学习的过程中总会得到一些心得体会,认真地将它们记录下来并分享给每一个愿意花费时间去阅读它们的人,然后意外地收获某个读者的评论,从而激发出新的感想,是一件十分令人欢快的事。如果你也在研习这方面的知识,欢迎加入到我们的队伍中来,和我们一起进步吧(^_^)
posted on 2017-03-03 14:39 LXRM-JavaWeb、ML 阅读(134) 评论(0) 编辑 收藏 举报