zoukankan      html  css  js  c++  java
  • java_spring_依赖注入(构造器)

    依赖注入对象可以 手工装配(建议) 和 自动装配


    package com.PersonDaoBean.test;
    
    public interface PersonDao {
    
    	public abstract void add();
    
    }



    package com.PersonDaoBean.test;
    
    public class PersonDaoBean implements PersonDao {
    	
    
    	@Override
    	public void add(){
    		System.out.println("PersonDaoBean执行。。。。。。。。。。。。。。。。。");
    	}
    }
    



    package com.dao.bean.www;
    
    public interface PersonServiceDao {
    
    	public abstract void save();
    
    }



    package com.bean.www;
    
    
    import com.PersonDaoBean.test.PersonDao;
    import com.dao.bean.www.PersonServiceDao;
    
    public class PersonServiceBean implements PersonServiceDao {
    
    	private PersonDao personDao;
    	private String name;
    	
    	public PersonServiceBean(PersonDao personDao, String name) {
    		this.personDao = personDao;
    		this.name = name;
    	}
    
    
    
    	public void save() {
    		personDao.add();
    		System.out.println(name);
    	}
    
    }
    


    //配置方法

    <?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.0.xsd">
    
    
    	<bean id="personDao" class="com.PersonDaoBean.test.PersonDaoBean"></bean>
    	<bean id="personService" class="com.bean.www.PersonServiceBean">
    		<constructor-arg index="0" type="com.PersonDaoBean.test.PersonDao" ref="personDao"></constructor-arg>
    		<constructor-arg index="1" value="valueString"></constructor-arg>
    	</bean>
    
    </beans>



    //Test Class

    package com.itcast.www;
    
    import static org.junit.Assert.*;
    
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.dao.bean.www.PersonServiceDao;
    
    public class TestCaseDemo {
    
    	@BeforeClass
    	public static void setUpBeforeClass() throws Exception {
    	}
    
    	@Test
    	public void instanceSpring() {
    
    		ApplicationContext ctx = new ClassPathXmlApplicationContext(
    				"applicationContext.xml");
    
    		PersonServiceDao personService = (PersonServiceDao) ctx
    				.getBean("personService");
    		
    		personService.save();
    
    
    	}
    
    }
    























































































































  • 相关阅读:
    Spring 中的重试机制,简单、实用!
    Docker 常用命令,还有谁不会?
    Java 对象不使用时为什么要赋值为 null?
    为什么 Nginx 比 Apache 更牛叉?
    你还在用命令看日志?快用 Kibana 吧,一张图片胜过千万行日志!
    golang如何体现面向对象思想
    golang 三⾊标记+GC混合写屏障机制
    Golang中逃逸现象-变量何时 栈何时堆
    golang调度器原理与GMP模型设计思想
    golang 程序内存分析方法
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720410.html
Copyright © 2011-2022 走看看