首页 > 其他分享 >IOC配置版代码

IOC配置版代码

时间:2023-06-01 19:57:51浏览次数:24  
标签:return int 代码 配置 System println IOC public out

一、通过get/set方法依赖注入
1、dao层

点击查看接口代码
public interface IDAO {
    public int save();
    public int remove();
    public int modify();
    public List findAll();
}
点击查看实现类代码
public class DeptDAO implements IDAO{
    @Override
    public int save() {
        System.out.println("保存了一条 dept 的数据");
        return 0;
    }

    @Override
    public int remove() {
        return 0;
    }

    @Override
    public int modify() {
        return 0;
    }

    @Override
    public List findAll() {
        List resultList = new ArrayList<>();
        resultList.add("dept01");
        resultList.add("dept02");
        resultList.add("dept03");
        return resultList;
    }
}
点击查看实现类代码
public class EmpDAO implements IDAO{
    @Override
    public int save() {
        System.out.println("保存了一条 emp 的数据");
        return 0;
    }

    @Override
    public int remove() {
        return 0;
    }

    @Override
    public int modify() {
        return 0;
    }

    @Override
    public List findAll() {
        return null;
    }
}
2、service层
点击查看代码
public class DeptService {
    private DeptDAO deptDAO;
    private EmpDAO empDAO;

    public DeptDAO getDeptDAO() {
        return deptDAO;
    }

    public void setDeptDAO(DeptDAO deptDAO) {
        this.deptDAO = deptDAO;
    }

    public EmpDAO getEmpDAO() {
        return empDAO;
    }

    public void setEmpDAO(EmpDAO empDAO) {
        this.empDAO = empDAO;
    }
    public List findAll(){
        List all = deptDAO.findAll();
        return all;
    }
    public int save(){
        deptDAO.save();
        empDAO.save();
        return 1;
    }
}
3、配置applicationContext.xml文件
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
    <!--通过get/set依赖注入-->
    <bean id="deptdao" class="com.bh.dao.DeptDAO"></bean>
    <bean id="empdao" class="com.bh.dao.EmpDAO"></bean>
    <bean id="deptservice" class="com.bh.service.DeptService">
        <property name="deptDAO" ref="deptdao"/><!--传值方式:应用传递用ref,值传递用value-->
        <property name="empDAO" ref="empdao"/>
    </bean>
    <bean class="com.bh.service.DeptService2"></bean>

    <!--通过构造器方式依赖注入-->
    <bean id="print" class="com.bh.service.PrintService">
        <constructor-arg index="0" value="5"></constructor-arg>
        <constructor-arg index="1" value="class08"></constructor-arg>
    </bean>

    <!--通过注解依赖注入-->
    <context:annotation-config></context:annotation-config>

    <!--使用spring管理.properties配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>
    <bean id="user" class="com.bh.po.User">
        <property name="userName" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>

    <!--p属性-->
    <bean id="dept" class="com.bh.po.Dept"
          p:deptno="100"
          p:dname="class009"
          p:loc="1406">
        <!--        <property name="dname" value="class009" />-->
        <!--        <property name="loc" value="1406" />-->
        <!--        <property name="deptno" value="100" />-->
    </bean>
</beans>
4、测试类
点击查看代码
public class Test {
    public static void main(String[] args) {
        System.out.println("start==========");
        //读取配置文件,并且根据配置文件初始化spring容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据配置文件的id创建实例对象
        IDAO deptdao = (IDAO) ac.getBean("deptdao");
        //deptdao.save();
        DeptService deptService = (DeptService) ac.getBean("deptservice");

       // DeptService deptService = ac.getBean(DeptService.class);//如果配置文件没有id就通过类对象创建实例对象
        List all = deptService.findAll();
        System.out.println(all);
        System.out.println("end=============");
    }
}

二、使用构造器依赖注入
1、创建一个类
点击查看代码
package com.bh.service;

public class PrintService {
    private String name;
    private int count;
//创建构造器
    public PrintService(int count,String name){
        this.count = count;
        this.name = name;
    }
    public void print(){
        for (int i = 0; i < count; i++) {
            System.out.println(name + "-" + i);

        }
    }
}

2、在配置文件applicationContext.xml中配置
点击查看代码
<!--通过构造器方式依赖注入-->
    <bean id="print" class="com.bh.service.PrintService">
        <constructor-arg index="0" value="5"></constructor-arg><!--index为第几个参数,value为值传递-->
        <constructor-arg index="1" value="class08"></constructor-arg>
    </bean>
3、测试类
点击查看代码
package com.bh.test;

import com.bh.po.User;
import com.bh.service.DeptService2;
import com.bh.service.PrintService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {
    public static void main(String[] args) {
        System.out.println("start============");
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过构造器依赖注入
        PrintService print = (PrintService) ac.getBean("print");
        print.print();


        System.out.println("end  ===============");

    }
}

4、结果
点击查看代码
class08-0
class08-1
class08-2
class08-3
class08-4

标签:return,int,代码,配置,System,println,IOC,public,out
From: https://www.cnblogs.com/liangkuan/p/17450023.html

相关文章

  • pycharm环境配置
    CUDA10.1的安装_哔哩哔哩_bilibili新建解释器:新建项目,然后点击文件-->设置-->项目-->python解释器,即下面的解释器配置页面。a.通过Virtualenv新建解释器。(1)点击添加本地解释器,打开添加python解释器界面,配置本地解释器常用的有两种,即本次介绍的Virtualenv方式,还有Conda方式。V......
  • 代码随想录算法训练营第二十二天|235. 二叉搜索树的最近公共祖先,701. 二叉搜索树中的
    [参考链接]235.二叉搜索树的最近公共祖先[注意]1.因为是有序树,所以如果中间节点是q和p的公共祖先,那么中间节点的数组一定是在[p,q]区间的。即中节点>p&&中节点<q或者中节点>q&&中节点<p。2.那么只要从上到下去遍历,遇到cur节点是数值在[p,q]区间中则一......
  • 36 KVM管理设备-配置虚拟串口
    36KVM管理设备-配置虚拟串口36.1概述在虚拟化环境下,由于管理和业务的需求,虚拟机与宿主机需要互相通信。但在云管理系统复杂的网络架构下,运行在管理平面的服务与运行在业务平面的虚拟机之间,不能简单的进行三层网络互相通信,导致服务部署和信息收集不够快速。因此需要提供虚拟串口......
  • Autosar OS IOC
    OS为IOC通信分配的所有数据都应使用内存分配关键字机制进行包装#defineOS_<IE>_START_SEC_<sadm>#include"Os_MemMap.h"<IOCbuffers>#defineOS_<IE>_STOP_SEC_<sadm>#include"Os_MemMap.h".//<IE>为shortname。<sadm>......
  • 根据Servie接口 生成 Controller 代码-因业务需要简单应付勿喷
    附上垃圾代码(勿喷,只不过为了应付工作需求,百十来个service都要创建对应的controller的需求,复制实在吃不消,说明一下就是简单的字符串替换操作)ApplicationControllerimportjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOExcepti......
  • springboot多模块打包配置问题
    工程案例结构: -baidu//聚合过程   -baidu_web     //子模块web工程   -baidu_service//子模块   -baidu_config//子模块配置工程  注意事项(配置步骤):1.baidu聚......
  • SpringBoot配置Bean是否生效
    @Configuration@ConditionalOnProperty(prefix="xxl.job",name="enable",havingValue="true",matchIfMissing=true)publicclassXxlJobConfig{//...}上述可直接读取配置文件中的,xxl.job.enable=true@ConditionalOnProperty(prefi......
  • Swagger 分组配置
     pom文件依赖<properties><springfox-swagger2.version>2.9.2</springfox-swagger2.version><springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version><knife4j-spring-boot-starter.version>2.0.2&l......
  • IDEA 中 .gitignore 文件中 配置 忽略上传的文件
    target/pom.xml.tagpom.xml.releaseBackuppom.xml.versionsBackuppom.xml.nextrelease.propertiesdependency-reduced-pom.xmlbuildNumber.properties.mvn/timing.properties.mvn/wrapper/maven-wrapper.jar//下面的需要手动添加**/mvnw**/mvnw.cmd**/.mvn.idea**......
  • springIOC
    一,spring学习1,但凡学习java框架,就2步①找到对应的jar包(依赖)spring-corespring-contextspring-beans②配置相应的配置文件applicationContext.xml2,写一个或者多个的类,交给spring管理DeptServiceIDAODeptDAOEmpDAO3,测试我们的工程是否正确二、IOC控制反转・高......