spring 入门实战(二)一个简单的使用案例
2018-03-17 16:19
656 查看
1.0 环境准备
开发工具eclipse jdk1.8 spring4.5.3jar包 下载地址:http://repo.spring.io/release/org/springframework/spring commons-logging jar包 下载地址:http://commons.apache.org/logging/
这里注意 spring的版本要与jdk相对应 否则会有许多莫名奇妙的错误,一般来说 spring4.x 与jdk8.0的版本比较兼容
2.0 项目搭建
2.1 新建java工程 javaweb工程spring_bagin
2.1.1 创建业务类Sendmessage.java 内容如下
package com.ccut.class1; public class Sendmessage { public void say(){ System.out.println("哦!这样啊"); } }
2.1.2 我们如果不用spring 则会这么调用上面的类
package com.ccut.test; import com.ccut.class1.Message; public class Test { public static void main(String[] args) { Sendmessage sendmessage = new Sendmessage(); sendmessage.say(); } }
结果如下:
2.1.3 有了spring的时候 我们需要一个配置文件Beans.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id = "Sendmessage " class = "com.ccut.class1.Sendmessage "/> </beans>
这里的class指向你想要的调用的类,id可以理解为为这个类起的别名 唯一可以用来代表这个类 一个bean可以表示一个类
2.1.4 然后导入spring jar包和 commons-logging.jar
这里我们本次用到的仅仅是其中几个jar为了方便我们一次性全部导入,自己实际用的时候酌情选择
目录结构也如图
2.1.5 调用上面这个类 Test.java
package com.ccut.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ccut.class1.Sendmessage; public class Test { public static void main(String[] args) { // Sendmessage sendmessage = new Sendmessage(); // sendmessage.say(); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Beans.xml"); Sendmessage message = (Sendmessage)applicationContext.getBean("Sendmessage"); message.say(); } }
结果:
接下来我们看一下程序运行时spring的大致工作流程 1.0 读取配置文件 返回ApplicationContext 类型的对象(这个还有其他类型的对象会在下一章内容详细说明) 2.0 获取配置文件中的Bean 返回相应类型的对象 3.0 调用bean中的方法实现业务
虽然看起来他比原来更加复杂了,并且实际上也是更复杂了。。。。。。至于有啥好处想起来再说吧 欢迎评论留言
至此我们已经算是初步的了解和使用spring了
相关文章推荐
- Spring学习--IOC容器的概念及一个简单IOC容器使用案例
- ActiveMQ的入门案例以及整合Spring的简单使用
- 使用buffalo集成spring写的一个简单的登录子模块
- 使用Spring来创建一个简单的工作流引擎
- Spring 入门(一个简单的例子)
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(2)
- 使用Spring来创建一个简单的工作流引擎
- 使用Spring来创建一个简单的工作流引擎
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(6)
- Spring 入门(一个简单的例子)--适合初学者
- 一个使用spring的最简单的例子。
- 转[翻译]案例学习:仅使用Redis+PHP设计实现一个简单的Twitter
- 使用buffalo集成spring写的一个简单的登录子模块
- 源代码:使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(3)
- Spring 入门(一个简单的例子)
- 使用Spring来创建一个简单的工作流引擎
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(4)
- 使用Spring来创建一个简单的工作流引擎
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(5)