spring
2016-05-22 20:42
253 查看
spring核心:ioc和aop
目前我只用到ioc,所以今天只介绍ioc
ioc:把New的对象交给spring管理,当要用这个对象时不用再new,只需要从spring中拿。。
IoC 不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合、更优良的程序。传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之间高耦合,难于测试;有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。
需要spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cqupt"></context:component-scan>
</beans>
把注释了的全部加载,名字为首字母小写。。
<bean id="userDao" class="cqupt.dao.imp.UserDaoImp"></bean>
<bean id="userService" class="cqupt.service.imp.UserServiceImp">
<property name="userDao" ref="userDao"></property>
</bean>
这种是手动加载和手动注入
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springannotation.xml");
UserService userService=(UserService) ctx.getBean("userServiceImp");
加载文件并且,从中拿到这个bean
加载bean有两种:延迟加载(用的时候在加载)和初始化就加载
程序入口是web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root3</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
监听器会加载这些context
目前我只用到ioc,所以今天只介绍ioc
ioc:把New的对象交给spring管理,当要用这个对象时不用再new,只需要从spring中拿。。
IoC 不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合、更优良的程序。传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之间高耦合,难于测试;有了IoC容器后,把创建和查找依赖对象的控制权交给了容器,由容器进行注入组合对象,所以对象与对象之间是松散耦合,这样也方便测试,利于功能复用,更重要的是使得程序的整个体系结构变得非常灵活。
需要spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cqupt"></context:component-scan>
</beans>
把注释了的全部加载,名字为首字母小写。。
<bean id="userDao" class="cqupt.dao.imp.UserDaoImp"></bean>
<bean id="userService" class="cqupt.service.imp.UserServiceImp">
<property name="userDao" ref="userDao"></property>
</bean>
这种是手动加载和手动注入
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springannotation.xml");
UserService userService=(UserService) ctx.getBean("userServiceImp");
加载文件并且,从中拿到这个bean
加载bean有两种:延迟加载(用的时候在加载)和初始化就加载
程序入口是web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root3</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
监听器会加载这些context
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 一起动手编写Android图片加载框架
- 基于.NET平台常用的框架和开源程序整理
- 列举PHP的Yii 2框架的开发优势
- .net中前台javascript与后台c#函数相互调用问题
- Windows窗体的.Net框架绘图技术实现方法
- js创建jsonArray传输至后台及后台全面解析
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码