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,用来保存所有的组件(键值对的方式)。
Spring IoC 容器详解
本文详细介绍了Spring框架中的IoC容器概念,包括如何通过@Service、@Repository、@Controller和@Component注解将不同层级的组件加入容器,以及依赖注入的实现方式。同时,文章还解释了配置文件中如何自动扫描并加载这些组件。
1933

被折叠的 条评论
为什么被折叠?



