zoukankan      html  css  js  c++  java
  • spring 依赖注入 set和构造方法

    基于spring4.x版本;3.x版本,不能构造方法注入;

    参考:http://c.biancheng.net/view/4244.html

    package com.ligy.service;
    
    import com.ligy.dao.StudentDao;
    import com.ligy.dao.TeacherDao;
    import com.ligy.dao.TeacherDaoImpl;
    
    public class TeacherServiceImpl implements ITeacherService {
    
        private TeacherDao teacherDao;
    
    //    public void setTeacherDao(TeacherDao teacherDao) {
    //        this.teacherDao = teacherDao;
    //    }
    //
    //    public TeacherServiceImpl() {
    //
    //    }
    
        public TeacherServiceImpl(TeacherDao teacherDao) {
            this.teacherDao = teacherDao;
        }
    
        @Override
        public void add() {
            this.teacherDao.add();
            System.out.println("this is in StudentServiceImpl do1");
        }
    }
    <?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: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">
        <!-- 由 Spring容器创建该类的实例对象 -->
        <bean id="studentDao" class="com.ligy.dao.StudentDaoImpl"/>
        <bean id="teacherDao" class="com.ligy.dao.TeacherDaoImpl"/>
        <bean id="StudentService" class="com.ligy.service.StudentServiceImpl">
            <property name="studentDao" ref="studentDao"></property>
        </bean>
    
        <bean id="TeacherService" class="com.ligy.service.TeacherServiceImpl">
            <constructor-arg ref="teacherDao"/>
            <!--<property name="teacherDao" ref="teacherDao"></property>-->
        </bean>
    
    </beans>

    天生我材必有用,千金散尽还复来
  • 相关阅读:
    理想解法
    IEEExtreme 2021
    day_1-python前期学习准备篇
    电梯模拟C++
    java线程_01——————————HelloWorld例子
    Unknown tag (c:forEach) 未知的标签
    自动生成Junit单元测试的插件 CodeProAnalytix
    Log4j笔记----01
    Springboot学习笔记_helloWorld篇
    支持开源,崇尚技术,追求真理,充实人生
  • 原文地址:https://www.cnblogs.com/ligenyun/p/14746327.html
Copyright © 2011-2022 走看看