其实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.jarcommons-logging-1.1.1.jarspring-aop-3.2.0.RELEASE.jarspring-beans-3.2.0.RELEASE.jarspring-context-3.2.0.RELEASE.jarspring-core-3.2.0.RELEASE.jarspring-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;<span class="keyword" style="font-weight:bold;">public</span> <span class="keyword" style="font-weight:bold;">void</span> print(){ System.<span class="keyword" style="font-weight:bold;">out</span>.println(<span class="string" style="color:rgb(221,17,68);">"Name is :"</span>+name); } <span class="keyword" style="font-weight:bold;">public</span> <span class="keyword" style="font-weight:bold;">void</span> userPrint(){ System.<span class="keyword" style="font-weight:bold;">out</span>.println(<span class="string" style="color:rgb(221,17,68);">"User name is :"</span>+userDao.getName()); } <span class="comment" style="color:rgb(153,153,136);font-style:italic;">//省略getter、setter方法</span>
}
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
ExampleBean exampleBean = context.getBean(“exampleBean”, ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();
Name is :boyaUser 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>
-
@Repository
-
public
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 :boyaUser name is :boya
转自:https://blog.csdn.net/u011225629/article/details/45417519