在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。 而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。
spring推荐面向接口编程
package cn.nevo.service;
public interface HelloService {
public void sayHello();
}
package cn.nevo.service;
public interface HelloService {
public void sayHello();
}
package cn.nevo.service.impl;
import cn.nevo.service.HelloService;
public class HelloServiceImpl implements HelloService {
private String message;
//当用set注入时,一个空的构造方法是必须的
public HelloServiceImpl() {}
public HelloServiceImpl(String message) {
this.message = message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println("hello " + message);
}
}
package cn.nevo.service.impl;
import cn.nevo.service.HelloService;
public class HelloServiceImpl implements HelloService {
private String message;
//当用set注入时,一个空的构造方法是必须的
public HelloServiceImpl() {}
public HelloServiceImpl(String message) {
this.message = message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println("hello " + message);
}
}
spring配置文件bean.xml(在这里决定如何配置bean及多个bean之间的依赖关系)
<?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-2.5.xsd">
<bean id="helloService" class="cn.nevo.service.impl.HelloServiceImpl">
<!-- 采用此种注入方式,容器实例化配置的Bean:HelloServiceImpl的时候会调用
它的setMessage方法完成赋值 -->
<property name="message">
<value>world!</value>
</property>
<!--
采用此种注入方式,容器会使用带有一个参数的构造器
实例化配置的Bean:HelloServiceImpl从而完成赋值
<constructor-arg>
<value>world!</value>
</constructor-arg>
-->
</bean>
</beans>
<?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-2.5.xsd">
<bean id="helloService" class="cn.nevo.service.impl.HelloServiceImpl">
<!-- 采用此种注入方式,容器实例化配置的Bean:HelloServiceImpl的时候会调用
它的setMessage方法完成赋值 -->
<property name="message">
<value>world!</value>
</property>
<!--
采用此种注入方式,容器会使用带有一个参数的构造器
实例化配置的Bean:HelloServiceImpl从而完成赋值
<constructor-arg>
<value>world!</value>
</constructor-arg>
-->
</bean>
</beans>
通常我们会看到拆分的元数据配置文件,这种做法是值得推荐的,它使得各个模块之间的开发分工更明确,互不影响,如:
<beans>
<import resource="model1.xml"/>
<import resource="model2.xml"/>
<import resource="model3.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
测试类HelloServiceTest.java
package cn.nevo.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.nevo.service.impl.HelloServiceImpl;
public class HelloServiceTest {
@Test
public void testHelloService() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
HelloService hs = (HelloServiceImpl)ctx.getBean("helloService");
hs.sayHello();
}
}
package cn.nevo.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.nevo.service.impl.HelloServiceImpl;
public class HelloServiceTest {
@Test
public void testHelloService() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
HelloService hs = (HelloServiceImpl)ctx.getBean("helloService");
hs.sayHello();
}
}
Spring IoC容器通过读取spring配置文件实例化,如下面的例子:
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"});// an ApplicationContext is also a BeanFactory (via inheritance)
BeanFactory factory = context;
org.springframework.beans.factory.BeanFactory 是Spring IoC容器的实际代表者,IoC容器负责容纳此前所描述的bean,并对bean进行管理。在Spring中,BeanFactory是IoC容器的核心接口。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。Spring为我们提供了许多易用的BeanFactory实现。
org.springframework.context包的核心是ApplicationContext接口。它由BeanFactory接口派生而来,因而提供了BeanFactory所有的功能。并且提供了一些更加通用的实现。BeanFactory就是spring容器,负责如何创建,配置和管理我们的Bean,采用工厂模式实现Ioc,将系统配置和依赖关系从具体业务代码中独立出来。
Spring IoC容器将读取配置元数据,并通过它对应用中各个对象进行实例化、配置以及组装。
Beanfactory接口结构图:
本示例项目结构图: