依赖注入IOC

简介: 依赖注入IOC

简介:由spring容器进行对象的创建和依赖注入,使用时取出来

反转:由spring容器进行对象的创建和依赖注入称为反转,将控制权从程序员手中夺走,交给spring容器,称为反转

<bean id="stu" class = "com.bjpowernode.pojo.Student">
//spring容器负责对象创建
<property name="name" value="张三">
//spring容器依赖注入值
</bean>

由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">
    <!--创建对象-->
    <bean id = "stu" class="org.example.Student"></bean>
</beans>
package org.example;

public class Student {
    private String name;
    private int age;
    public Student(){
        System.out.println("student类的无参构造方法");
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

测试类

package org.example.text;

import org.example.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myText {
    @Test
    public void textStudent(){
        Student student = new Student();
    }
    @Test
    public void testStudentSpring(){
        //由spring容器创建对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student) ac.getBean("stu");
        System.out.println(stu);
    }
}

给创建的对象赋值

  1. 使用setter注入

    1. 简单类型注入使用value属性
    <bean id = "stu" class="org.example.Student">
        <property name="name" value="张三"></property>
        <property name="age" value="88"></property>
    </bean>
    1. 引用类型主人椅使用ref属性
    <bean id = "stu" class="org.example.pojo2.Student">
        <property name="name" value="lily"></property>
        <property name="age" value="12"></property>
        <property name="school" ref="school"></property>
    </bean>
    1. 注意:使用setter注入必须提供无参的构造方法,必须提供setXXX()方法。
      三层架构
<?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">
    <!--创建对象-->
    <!--创建数据访问层对象-->
    <bean id = "uMapper" class = "org.example.dao.UsersMapperImpl">
    </bean>
    <!--创建业务逻辑层的对象-->
    <bean id = "uService" class="org.example.service.impl.UsersServiceImpl">
        <property name="usersMapper" ref="uMapper"></property>
    </bean>
    <!--创建界面层对象-->
    <bean id = "uController" class="org.example.controller.UsersController">
        <property name="usersService" ref="uService"></property>
    </bean>
</beans>
  1. 使用构造方法注入

    1. 使用构造方法的参数名称进行注入值
      ==使用标签==
    <!--创建school对象-->
    <bean id="school" class="org.example.pojo3.School">
        <constructor-arg name="name" value="清华大学" ></constructor-arg>
        <constructor-arg name="address" value="海淀区" ></constructor-arg>
    </bean>
    @Test
        public void textSchool(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("r3/applicationContext.xml");
            School school = (School) ac.getBean("school");
            System.out.println(school.toString());
        }
    1. 使用构造方法的下标进行注入值
    <!--创建学生对象,使用构造方法下标注入值-->
    <bean id = "stu" class="org.example.pojo3.Student">
        <constructor-arg index="0" value="tony"></constructor-arg>
        <constructor-arg index="1" value="23"></constructor-arg>
        <constructor-arg index="2" ref="school"></constructor-arg>
    </bean>
    1. 使用默认的构造方法的参数的顺序注入值
      参考2,不使用index,按顺序注入

创建对象的注解

@Component:可以创建任意对象,创建的对面默认名称是类名的驼峰命名法,也可以指定【@Component("")】

<!--添加包扫描-->
    <context:component-scan base-package="org.example.s01"></context:component-scan>
ApplicationContext ac = new ClassPathXmlApplicationContext("s01/applicationContext.xml");
Student stu = (Student) ac.getBean("student");

@Controller:专门用来创建控制器的对象(Servlet),这种对象可以接收用户的请求,可以返回处理结果给客户端

@service:专门用来创建业务逻辑层的对象,负责向下访问数据访问层,处理完毕后的结果返回给界面层

@Repository:专门用来创建数据访问层的对象,负责数据库中的增删改查所以操作

相关文章
|
10月前
|
人工智能 安全 API
AI智能体的开发流程
2026年AI智能体开发已迈入工业级:强调确定性、可观测性与多机协作。涵盖五大阶段——需求解构(明边界、定范式)、架构设计(引擎选型+MCP工具集成+Graph-RAG记忆)、SOP编排(CoT+反思循环)、安全围栏(过滤/权限/熔断)及自动化评估(AgentBench+全链路追踪)。工程化能力决定成败。
|
人工智能 自然语言处理 安全
大模型备案材料—《安全评估报告》撰写指南
本文详解大模型备案中的关键材料——《安全评估报告》的撰写要点,涵盖报告框架、必备内容、注意事项及基础信息,助你高效通过备案。
|
缓存 Windows
win11更新出问题了安装错误 - 0x80070002,问题解决
Win11更新报错0x80070002?常见原因包括系统文件缺失、更新缓存损坏。可通过运行Windows更新疑难解答、清除SoftwareDistribution缓存、使用SFC和DISM命令修复系统文件、手动下载更新或使用微软官方工具(如MediaCreationTool)解决。还可尝试DirectX修复工具等第三方方案,全面排除问题,确保系统顺利更新。
2113 0
|
人工智能 JSON 供应链
5C提示词工程框架:让AI成为你的贴心助手
通过清晰度、上下文、命令、链式、持续优化五个维度,教你如何与AI进行高效对话。从小白到专家,一篇文章搞定所有提示词技巧!
1094 1
|
8月前
|
SQL 存储 架构师
指标平台能否真正替代 DWS/ADS?轻数仓转型的风险与对策
轻数仓转型的核心是用 NoETL 语义编织构建的统一语义层,替代烟囱式开发的物理宽表层,从根本上解决口径不一的问题。
|
Linux
百度搜索:蓝易云【Linux系统的 /dev/null 是什么?有什么隐藏的作用?】
综上所述,`/dev/null`在Linux系统中起到丢弃输入并模拟黑洞的作用。通过将输出重定向到 `/dev/null`,可以隐藏输出、屏蔽错误信息或作为空输出的占位符。它是一种非常有用的工具,特别是在自动化脚本和命令行操作中。
629 2
|
消息中间件 设计模式 监控
中间件事件总线(Event Bus)
【6月更文挑战第19天】
798 8
|
机器学习/深度学习 运维 算法
利用机器学习进行异常检测的技术实践
【5月更文挑战第16天】本文探讨了利用机器学习进行异常检测的技术实践,强调了在大数据时代异常检测的重要性。机器学习通过无监督、有监督和半监督学习方法自动识别异常,常见算法包括KNN、LOF、K-means和GMM等。异常检测流程包括数据准备、特征工程、选择算法、训练模型、评估优化及部署。机器学习为异常检测提供了灵活性和准确性,但需结合具体问题选择合适方法。
1039 2
|
存储 算法 C语言
【C语言】二分查找算法
【C语言】二分查找算法
425 0
|
JavaScript Linux Shell
NODEJS版本管理工具
NODEJS版本管理工具