三大关键点:
Bean: 在IOC容器中被Ioc容器创建或管理的对象的统称
Ioc容器: 就是spring提供的一个容器 用来 创建初始化存放管理Bean的
DI注入: 在容器中根据bean之间的依赖关系 进行关系绑定的过程
案列:
就上图而言 :当有业务层注入service时 对service调用 但是service的调用依赖于dao层,这时spring也同时管理dao和service 就会进行dao和service的依赖关系 进行关系绑定 让业务层调用service时也调用了dao
最终目标:
案例:
1.导坐标 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency>
2.写配置 在xml中
<!--注册bean--> <!--bean为注册bean的标签 参数class 为注册bean的类路径 id为 这个bean的唯一标识--> <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
3.调用
//获取Ioc容器 为配置文件 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取根据beanId获取bean对象 UserService userService = context.getBean("userService", UserService.class); //执行方法 userService.save();