您现在的位置是:主页 > news > 重庆微信网站开发公司/推广方案范例

重庆微信网站开发公司/推广方案范例

admin2025/5/7 12:57:07news

简介重庆微信网站开发公司,推广方案范例,如何制作班级网站,做网站客户要求多很烦1.控制反转IOC 控制反转,IOC,全称(Inversion of Control),将创建对象的权力交给容器,我们只需要告诉容器你需要创建哪些对象就可以了,容器会一直持有这个对象,管理对象的声明周期及其依赖关系,…

重庆微信网站开发公司,推广方案范例,如何制作班级网站,做网站客户要求多很烦1.控制反转IOC 控制反转,IOC,全称(Inversion of Control),将创建对象的权力交给容器,我们只需要告诉容器你需要创建哪些对象就可以了,容器会一直持有这个对象,管理对象的声明周期及其依赖关系,…

1.控制反转IOC

控制反转,IOC,全称(Inversion of Control),将创建对象的权力交给容器,我们只需要告诉容器你需要创建哪些对象就可以了,容器会一直持有这个对象,管理对象的声明周期及其依赖关系,我们可以通过getBean()获取对象。这样,控制权由应用代码转移到了spring容器,控制权发生了反转,这就是控制反转。

       1.自己创建

  // 自己手动创建对象Student student = new Student();

        2.容器创建

spring容器获取对象步骤:

        bean.xml 声明要创建的对象

        使用BeanFactory 的子类 去加载 bean.xml 配置 ,按照配置文件创建 bean对象,并且放置在容器中

        getBean(Student.class);  使用getBean 根据类型获取   

2. 依赖注入(DI)

依赖注入,DI,全程(Dependency Injection)。

从Spring容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,这相当于为调用者注入了它依赖的实例,这就是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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 声明一个 id 为studentDao的 实例 --><bean id="studentDao" class="com.wgz.spring.dao.impl.StudentDaoImpl"></bean><!-- 声明一个 id 为studentService的 实例,并将id为 studentDao的实例注入给StudentServiceImpl--><bean id="studentService" class="com.wgz.spring.service.StudentServiceImpl"><property name="studentDao" ref="studentDao"></property></bean>
</beans>
public class Test2 {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application.xml");StudentService studentService =   applicationContext.getBean(StudentService.class);Student student =  studentService.findStudentById(10);System.out.println("student:"+student);}
}