今天复习了Spring的知识,在这里总结一下Spring获取对象的三种情况:
Spring是通过IOC(控制反转)进行的,那么IOC是什么呢?简单来说IOC就是将管理对象的权力交给Spring。之前我们创建对象需要自己手动去new一个对象,现在对象的生命周期由Spring来管理,我们需要对象的时候直接从Spring那里去获取一个对象即可。
下面便是Spring获取对象的三种情况。
1、直接在配置文件applicationContext.xml中配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop