Spring系列之简单的应用实例
2016-07-14 07:15
453 查看
【回顾】
在上篇博客中,我们对Spring框架做了很细致的了解。从博客开头的实例中,我们知道了,没有使用Spring框架,就需要自己在应用程序中去写实例化对象的代码。现在,我们就看看使用了Spring框架,可以给我们带来怎样的便利。
【应用】
整个项目的目录不需要改变,只需要添加对应的jar包(spring.jar/log4j.jar/commons-logging.jar)、applicationContext.xml配置文件和对应的log4j.properties文件。
Dao层代码不变,仍旧是一个addUser()方法。
在UserManagerImp层中,不再需要写实例化Dao类的代码,提供一个构造函数或Set方法,spring就能将Dao注入过来。下面代码中包含两种注入方式,分别为构造函数注入和set注入,在下篇博客中会有更详细的学习。
这样,就实现了上篇博客中一样的效果。加上了Spring框架,降低了代码之间的耦合性,将耦合性推迟到了配置文件,发生了变化只需要修改对应的配置文件即可。
下面,再来一个HelloWorld实例,进一步加深对代码的理解。
1. 新建一个类,定义一个变量,提供一个get和set方法。
4.结果
获取到msg变量的值,在控制台中打印出Hello World字样。
【总结】
在本篇博客中,通过两个简单的实例,并与没有使用Spring框架的程序进行对比,对Spring框架的使用有了进一步的认识。而Spring之所以能够帮助我们如此便利的实现代码功能,在于Spring的IOC和DI。下面也会对Spring的这个核心进行进一步的学习和总结。
在上篇博客中,我们对Spring框架做了很细致的了解。从博客开头的实例中,我们知道了,没有使用Spring框架,就需要自己在应用程序中去写实例化对象的代码。现在,我们就看看使用了Spring框架,可以给我们带来怎样的便利。
【应用】
整个项目的目录不需要改变,只需要添加对应的jar包(spring.jar/log4j.jar/commons-logging.jar)、applicationContext.xml配置文件和对应的log4j.properties文件。
Dao层代码不变,仍旧是一个addUser()方法。
在UserManagerImp层中,不再需要写实例化Dao类的代码,提供一个构造函数或Set方法,spring就能将Dao注入过来。下面代码中包含两种注入方式,分别为构造函数注入和set注入,在下篇博客中会有更详细的学习。
public class UserManagerImpl implements UserManager { private UserDao userDao; // public UserManagerImpl(UserDao userDao) { // this.userDao = userDao; // } public void addUser(String username, String password) { userDao.addUser(username, password); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }在客户端,我们也不再需要写实例化Manager的代码,解析对应的配置文件即可,其中包括BeanFactory和ApplicationContext两种接口,任意一种都行,后面的博客中也会对两者进行比较,下面是客户端代码:
public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); //ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); //UserManager userManager = new UserManagerImpl(new UserDao4MySqlImpl()); userManager.addUser("张三", "123"); } }最后,在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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userDao4Mysql" class="com.bjpowernode.spring.dao.UserDao4MySqlImpl"/> <bean id="usrDao4Oracle" class="com.bjpowernode.spring.dao.UserDao4OracleImpl"/> <bean id="userManager" class="com.bjpowernode.spring.manager.UserManagerImpl"> <!-- <constructor-arg ref="userDao4Mysql"/> --> <!-- <constructor-arg ref="usrDao4Oracle"/> --> <property name="userDao" ref="usrDao4Oracle"/> </bean> </beans>
这样,就实现了上篇博客中一样的效果。加上了Spring框架,降低了代码之间的耦合性,将耦合性推迟到了配置文件,发生了变化只需要修改对应的配置文件即可。
下面,再来一个HelloWorld实例,进一步加深对代码的理解。
1. 新建一个类,定义一个变量,提供一个get和set方法。
public class HelloWorld { //该变量用来存储字符串 public String msg=null; //获取msg的get方法 public String getMsg() { return msg; } //设定msg的set方法 public void setMsg(String msg) { this.msg = msg; } }2. 新建一个测试类,提供一个main方法。使用Spring框架,不再需要在代码中实例化HelloWorld类
public class TestHelloWorld { public static void main(String[] args) { //获取Spring配置文件 ApplicationContext actx=new ClassPathXmlApplicationContext("applicationContext.xml"); //通过bean的id获取bean HelloWorld HelloWorld=(HelloWorld)actx.getBean("HelloWorld"); System.out.println(HelloWorld.getMsg()); } }3.配置文件内容
<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 代码说明: id,用来唯一表示Bean; class,表示Bean的来源; name和value,设置字符串的值 --> <!-- 定义一个Bean --> <bean id="HelloWorld" class="com.bjpowernode.spring.manager.HelloWorld"> <!-- 将其变量msg通过依赖注入 --> <property name="msg"> <value>Hello World</value> </property> </bean> </beans>
4.结果
获取到msg变量的值,在控制台中打印出Hello World字样。
【总结】
在本篇博客中,通过两个简单的实例,并与没有使用Spring框架的程序进行对比,对Spring框架的使用有了进一步的认识。而Spring之所以能够帮助我们如此便利的实现代码功能,在于Spring的IOC和DI。下面也会对Spring的这个核心进行进一步的学习和总结。
相关文章推荐
- SpringMVC中使用Interceptor拦截器
- 如何在Java中构造多维动态数组以及多维动态数组的使用
- [置顶] Debugging PL-SQL calls from Java Session Using Eclipse and SQL Developer
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- Eclipse快捷键与使用技巧总结
- 01 java开发前小知识
- java中List、Set、Map、Properties集合的遍历方法总结
- Java中的参数传递问题图解
- Java 集合 LinkedList的ListIterator
- Java-try-with-resource
- java 集合中自定义对象的几种排序方法
- 使用Eclipse的几个必须掌握的快捷方式
- java反射:泛型
- spring aop注解配置
- J2EE基础教程(4):struts框架
- Springmvc-MyBatis集成
- spring web mvc配置
- 新人java项目开发
- Eclipse(Myeclipse)打jar包方式总结
- error LNK2001: unresolved external symbol __imp__JNI_CreateJavaVM@12 Debug/Main.exe : fatal error LN