Spring学习笔记(1)——初识Spring

其实Spring就是面向Bean的编程(BOP,Bean Oriented Programming),Bean在Spring中才是真正的主角。
Bean在Spring中作用就像Object对OOP的意义一样,没有对象的概念就像没有面向对象编程,Spring中没有Bean也就没有Spring存在的意义。就像一次演出舞台都准备好了但是却没有演员一样。为什么要Bean这种角色Bean或者为何在Spring如此重要,这由Spring框架的设计目标决定,Spring为何如此流行,我们用Spring的原因是什么,想想你会发现原来Spring解决了一个非常关键的问题他可以让你把对象之间的依赖关系转而用配置文件来管理,也就是他的依赖注入机制。而这个注入关系在一个叫Ioc容器中管理,那Ioc容器中有又是什么就是被Bean包裹的对象。Spring正是通过把对象包装在 Bean中而达到对这些对象管理以及一些列额外操作的目的。
它这种设计策略完全类似于Java实现OOP的设计理念,当然了Java本身的设计要比Spring复杂太多太多,但是都是构建一个数据结构,然后根据这个数据结构设计他的生存环境,并让它在这个环境中按照一定的规律在不停的运动,在它们的不停运动中设计一系列与环境或者与其他个体完成信息交换。这样想来回过头想想我们用到的其他框架都是大慨类似的设计理念。
核心组件如何协同工作
前面说Bean是Spring中关键因素,那Context和Core又有何作用呢?前面吧Bean比作一场演出中的演员的话,那Context就是这场演出的舞台背景,而Core应该就是演出的道具了。只有他们在一起才能具备能演出一场好戏的最基本的条件。当然有最基本的条件还不能使这场演出脱颖而出,还要他表演的节目足够的精彩,这些节目就是Spring能提供的特色功能了。
aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
<?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-3.0.xsd">
<bean id="userDao" class="com.boya.spring.ioc.UserDao" />
<bean id="exampleBean" class="com.boya.spring.ioc.ExampleBean">
<property name="name" value="boya" />
<property name="userDao" ref="userDao" />
</bean>
</beans>
public class UserDao {
public String getName() {
return "boya";
}
}
public class ExampleBean {
private String name;
private UserDao userDao;
public void print(){
System.out.println("Name is :"+name);
}
public void userPrint(){
System.out.println("User name is :"+userDao.getName());
}
//省略getter、setter方法
}
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();
Name is :boya
User name is :boya
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.boya.spring.ioc" />
</beans>
@Repositorypublic class UserDao {
public String getName() {
return "boya";
}
}
@Service
public class ExampleBean {
@Resource
@Value("boya")
private String name;
@Resource
private UserDao userDao;
public void print(){
System.out.println("Name is :"+name);
}
public void userPrint(){
System.out.println("User name is :"+userDao.getName());
}
}
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();
Name is :boya
User name is :boya
Spring学习笔记(1)——初识Spring的更多相关文章
- 【Spring学习笔记-0】Spring开发所需要的核心jar包
spring开发所需要的核心jar 1. libs目录下的核心jar包: 2. common-logging-xxx.jar 来自为知笔记(Wiz) 附件列表
- 【Spring学习笔记-MVC-2】spring导出Excel
说明: 1.结合Spring MVC实现Excel导出功能: 2. 在MVC配置文件中配置Excel视图解析器: 需要的jar包 以poi开头的jar包都是必须的 web.xml <?xml v ...
- 【Spring学习笔记-5】Spring中的抽象bean以及bean继承
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- 【Spring学习笔记-MVC-15】Spring MVC之异常处理
作者:ssslinppp 1. 描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理 ...
- 【Spring学习笔记-MVC-14】Spring MVC对静态资源的访问
作者:ssslinppp 参考链接: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html http://www.cnb ...
- 【Spring学习笔记-MVC-13】Spring MVC之文件上传
作者:ssslinppp 1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...
- 【Spring学习笔记-MVC-12】Spring MVC视图解析器之ResourceBundleViewResolver
场景 当我们设计程序界面的时候,中国人希望界面是中文,而美国人希望界面是英文. 我们当然希望后台代码不需改变,系统能够通过配置文件配置,来自己觉得是显示中文界面还是英文界面. 这是,Spring mv ...
- 【Spring学习笔记-MVC-10】Spring MVC之数据校验
作者:ssslinppp 1.准备 这里我们采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先 ...
- Spring学习笔记—最小化Spring XML配置
自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系. 自动 ...
随机推荐
- UVA 12446 How Many... in 3D! ( 递推 + 树状数组 )
C. How Many... in 3D! Time Limit: 1000ms Memory Limit: 131072KB 64-bit integer IO format: %lld ...
- Javascript基础五(BOM和DOM)
1.BOM概念 什么是BOM? BOM是Browser Object Model的缩写,简称浏览器对象模型.这个对象就是window BOM提供了独立于内容而与浏览器窗 ...
- β版本apk下载地址及源代码github地址
β版本下载地址 源代码下载地址:https://github.com/U-Help/Version-1.0 安装包下载地址:百度网盘:(密码q3sy)https://pan.baidu.com/s ...
- websocket(python)
1.websocket-server https://github.com/google/pywebsocket git clone https://github.com/google/pywebso ...
- 如何在YouTube上下载视频
1,首先要有科学上网的工具 2,详细说明:https://en.savefrom.net/1-how-to-download-youtube-video/
- 力扣—set matrix zeroes (矩阵置零) python实现
题目描述: 中文: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 英文: Given a m x n matrix, if an eleme ...
- Requests使用
Requests 简介 Requests库 requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到.可以说,**Requests 完全满足如今网络的需求 ...
- Springboot项目静态资源配置
springboot项目的静态资源配置网上有好多,说的也很详细 我今天出错是自定义了一个filter,在shiro里配置的/**,自定义filter 所以一直报302
- SCP-bzoj-3309
项目编号:bzoj-3309 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 以下用\((x, y)\)表示\(gcd(x, y)\). \[ ans = \sum _ {i = 1} ^ { ...
- 2017 NOIp 初赛体验
很菜...我还是太蒟蒻了. d 老师太强了... 应该能有七十几分 初赛稳了 Update: 五十几分...