
其实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 framework项目中所需要的部分jar包,在系列博客——学习笔记_轻松搞定Spring全家桶(初识篇)中所涉及的系列项目所需的jar包。。
第一天:初识Spring Cloud 1. Spring Cloud概述:了解Spring Cloud的发展背景,理解其在微服务架构中的地位和作用。 2. Eureka服务注册与发现:学习如何使用Eureka实现服务的注册与发现,理解服务注册与发现的重要性...
以下是对标题“Struts2学习笔记”中涉及的一些核心知识点的详细解释: 1. **MVC模式**:MVC模式是软件设计中的一种架构模式,它将应用逻辑分为三个部分:模型(Model)负责业务逻辑,视图(View)负责用户界面展示...
该压缩包包含--学习笔记(05):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:--系列博客中新需要的三个jar包。包括spring-aspects-5.2.7.RELEASE.jar和spring-aop-5.2.7.RELEASE.jar和...
此外,还可以借助CSDN、SegmentFault、菜鸟教程等在线资源进行深入学习,并利用Markdown、思维导图等工具整理笔记,提高学习效率。在实际开发中,要勤于动手实践,多敲代码,多写笔记,理解和串联知识点,注重框架...