Spring ioc 是一个容器,能够帮我们管理所有的组件:
1.使用注解方式将组件加入ioc容器中,有如下4种注解:
@Service:业务逻辑层;
@Repository:数据库层(持久层、Dao层);
@Contorller:Controller层(Servlet层);
@Component:不属于上述三种的组件。
其实使用任何一种注解都可以将组件加入ioc容器中,分类加注解便于程序员阅读,理解程序。
2.依赖注入:@Autowired,自动赋值;
3.某个组件要使用Spring提供的所有功能,必须将该组件加入容器中(配置文件中可以将加了注解的类自动扫描进容器)
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
</beans>
4.容器实质上是一个map,用来保存所有的组件(键值对的方式)。