spring细节讨论
来源:互联网 发布:gis软件开发工程师 编辑:程序博客网 时间:2024/04/30 04:31
一 传统的方法和使用spring的方法
使用spring,没有new对象,我们把创建对象的任务交给spring框架。
二 spring的运行原理图
三 案例
1 UserService
package com.service;public class UserService { private String name; private BybService byeService; public BybService getByeService() { return byeService; } public void setByeService(BybService byeService) { this.byeService = byeService; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello(){ System.out.println("hello "+name ); byeService.sayBye(); } }
2 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:tx="http://www.springframework.org/schema/tx"; xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";><!-- 在容器文件中配置bean(service/dao/domain/action/数据源) --><!-- bean元素的作用是,当我们的spring框架加载时候,spring就会自动的创建一个bean对象,并放入内存 UserService userSerivce=new UserService(); userSerivce.setName("韩顺平"); --><bean id="userService" class="com.service.UserService"> <!-- 这里就体现出注入的概念. --> <property name="name"> <value>韩顺平</value> </property> <property name="byeService" ref="bybService"> </property></bean><bean id="bybService" class="com.service.BybService"> <!-- 这里就体现出注入的概念. --> <property name="name" value="小明"> </property></bean></beans>
3 BybService
package com.service;public class BybService { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayBye(){ System.out.println("bye"+name); }}
4 Test
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.BybService;import com.service.UserService;import com.util.ApplicaionContextUtil;public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //我们先使用传统的方法,来调用UserService的 sayHello方法// UserService userService=new UserService();// userService.setName("顺平");// userService.sayHello(); //我们现在使用spring来完成上面的任务 //1.得到spring 的applicationContext对象(容器对象) //UserService us=(UserService) ac.getBean("userService"); //us.sayHello(); ((UserService)ApplicaionContextUtil.getApplicationContext() .getBean("userService")).sayHello(); //从ac[代表applicatonContext容器]中 //BybService bybService=(BybService) ac.getBean("bybService"); //bybService.sayBye(); }}
5 ApplicaionContextUtil
package com.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;final public class ApplicaionContextUtil { private static ApplicationContext ac=null; private ApplicaionContextUtil(){ } static{ ac=new ClassPathXmlApplicationContext("applicationContext.xml"); } public static ApplicationContext getApplicationContext(){ return ac; } }
6 运行结果
hello 韩顺平
bye小明
四 案例总结
spring实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可。
把Applicationcontext做成一个单例的。
五 ioc是什么
ioc,即控制反转,inverseofcontroll,所谓控制反转就是把创建对象(bean),和维护对象(bean)的关系的权利从程序中转移到spring的容器(applicationContext.xml),而程序本身不再维护。
DI是什么?
di,依赖注入,dependency injection,实际上di和ioc是同一个概念,spring设计者认为di更准确表示spring核心技术
学习框架,最重要的就是学习各个配置.
阅读全文
0 0
- spring细节讨论
- 讨论:烦人的细节
- Memcached细节讨论
- session的细节讨论
- 讨论记录之C++细节
- 字符串合并的细节讨论
- 讨论记录之C++细节
- SqlDataAdapter细节问题的讨论
- SqlDataAdapter细节问题的讨论
- spring 细节
- Spring aop 讨论
- Spring aop 讨论
- Spring讨论班
- FBReader 的相关文章和细节讨论
- 稳定的完成端口开发细节讨论
- 稳定的完成端口开发细节讨论
- CSS模块化的几个细节讨论
- (转)关于RSA实现细节的讨论
- 分布式系统事务一致性解决方案
- 八周第一次课 2017.12.4 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍
- 字符集(ASCII、GB***、Big5、Unicode)及字符编码(UTF-*等)
- stl中map的使用
- 继承
- spring细节讨论
- Python入门到实践(9)-类的继承
- HTML5(四)
- 11,数据挖掘环境搭建-hive安装
- 期末考试 编程题#6:MyString(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- IAR for ARM系列教程(一)_新建软件工程详细过程
- elasticseach 和hbase 在海量数据存储上哪个好
- 吴恩达(Andrew Ng)深度学习工程师笔记
- Java学习笔记——并发之synchronized