Spring4 IOC详解
上一章对Spring做一个快速入门的教程,其中只是简单的提到了IOC的特性。本章便对Spring的IOC进行一个详解。主要从三个方面开始:基于xml文件的Bean配置,基于注解的Bean配置和IOC容器Bean的生命周期。
基于xml文件的Bean配置
首先是applicationContext.xml文件,这可是核心文件。
配置一个bean,需要一个id去唯一标识它,用class指定Bean对象的路径,作用域默认是单例。
通过prototype进行属性赋值,name是属性名,value是值,也可以用ref引用对象,用list,map设置集合。
用SpEL表达式语言赋值,用p命名空间简化赋值,用继承和依赖简化代码。
]]>
欢迎阅读
ITDragon
的博客!
实体类Entity和SpELEntity
import java.util.List;
import java.util.Map;
public class Entity {
private int intValue;
private float floatValue;
private String stringValue;
private SpELEntity spELEntity;
private Map mapValue;
private List
public class SpELEntity {
private int intSpel;
private float floatSpel;
private boolean bSpel;
private String stringSpel;
public int getIntSpel() {
return intSpel;
}
public void setIntSpel(int intSpel) {
this.intSpel = intSpel;
}
public float getFloatSpel() {
return floatSpel;
}
public void setFloatSpel(float floatSpel) {
this.floatSpel = floatSpel;
}
public boolean isbSpel() {
return bSpel;
}
public void setbSpel(boolean bSpel) {
this.bSpel = bSpel;
}
public String getStringSpel() {
return stringSpel;
}
public void setStringSpel(String stringSpel) {
this.stringSpel = stringSpel;
}
@Override
public String toString() {
return "SpELEntity [intSpel=" + intSpel + ", floatSpel=" + floatSpel
+ ", bSpel=" + bSpel + ", stringSpel=" + stringSpel + "]";
}
}
测试Main方法。首先要创建一个容器,然后通过bean的id获取到实例,这样就可以开始相关的操作。其中entity,entity2,entity3对应三块不同的知识点。
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/**
* ClassPathXmlApplicationContext:从 类路径下加载配置文件,建议用该方法
* FileSystemXmlApplicationContext: 从文件系统中加载配置文件
*/
// 1. 创建 Spring 的 IOC 容器
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* entity 属性注入,构造器注入,对象引用,集合,SpEL,(重点)
* entity2 自动装配和P命名空间
* entity3 继承和依赖,作用域
*/
// 2. 从 IOC 容器中获取 bean 的实例
Entity entity = (Entity) ctx.getBean("entity");
// 3. 使用 bean
System.out.println(entity.toString());
// System.out.println(ctx.getBean("entity") == ctx.getBean("entity")); 使用 prototype 打印的是false
ctx.close();
}
}
Entity [intValue=1, floatValue=1.1, stringValue=, spELEntity=SpELEntity [intSpel=1, floatSpel=1.2, bSpel=true, stringSpel=Spring4基础教程], mapValue={one=1, two=2}, listValue=[欢迎阅读, ITDragon, 的博客!]]
基于注解的Bean配置
相对于xml的配置,注解的方式显得异常简单。主要分两个步骤
第一步:在applicationContext.xml文件设置扫描包的范围
第二步:在对象上用注解。这四几个注解的作用一样,只是为了结构清晰,取的名字不同罢了。
使用方法很简单:直接在类上加注解即可。无参数的情况,bean的id默认是小写字母开头的类名。也可以指定参数@Commponent("指定参数"),那bean的id就是指定参数。
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
public interface AnnoRepository {
public void hello();
}
import org.springframework.stereotype.Repository;
@Repository
public class AnnoRepositoryImp implements AnnoRepository{
@Override
public void hello() {
System.out.println("AnnoRepository : hello!");
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AnnoService {
@Autowired
private AnnoRepository annoRepository;
public void hello() {
System.out.println("AnnoService : hello!");
annoRepository.hello();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class AnnoController {
@Autowired
private AnnoService annoService;
public void execut() {
System.out.println("AnnoController : hello !");
annoService.hello();
}
}
这里还有一个注解Autowired, context:component-scan 元素会自动组件装配被Autowired修饰的对象。
测试类:虽然applicationContext.xml中没有annoController的bean配置,但我们有注解!
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
AnnoController annoController = (AnnoController) ctx.getBean("annoController");
annoController.execut();
ctx.close();
}
}
AnnoController : hello !
AnnoService : hello!
AnnoRepository : hello!
有没有觉得基于注解的bean配置比基于xml的bean配置简单很多。
IOC容器Bean的生命周期
- step1 实例化,通过构造器创建 Bean 实例
- step2 赋值,为 Bean 的属性设置值
- step3 init-method,调用 Bean 的初始化方法(init-method)
- step4 destroy-method,当容器关闭时, 调用 Bean 的销毁方法(destroy-method)
以上代码都是笔者亲测可用的,不要嫌麻烦,麻烦是学不好的,如果有什么问题和建议可以留言,我会及时处理。http://blog.csdn.net/qq_19558705/article/details/49994191