• Spring之控制反转、依赖注入


    一、控制反转:将创建对象、属性值的方式进行了反转,从new、setxxx()变为从SpringIOC中getbean();

    二、依赖注入:将属性值注入给属性,将属性注入给bean,将bean注入给了SpringIOC;

    创建接口类,ICourse.java:

    package org.ruangong.newinstance;
    
    public interface ICourse {
    	void learn();//学习
    }
    

      创建实现类,HtmlCourse:

    package org.ruangong.newinstance;
    
    public class HtmlCourse implements ICourse{
    	
    	@Override
    	public void learn() {
    		// TODO Auto-generated method stub
    		System.out.println("学习html中......");
    	}
    }
    

      创建实现类,JavaCourse:

    package org.ruangong.newinstance;
    
    public class JavaCourse implements ICourse{
    	
    	@Override
    	public void learn() {
    		// TODO Auto-generated method stub
    		System.out.println("学习JAVA......");
    	}
    }
    

      实体类:

    package org.ruangong.entity;
    
    import org.ruangong.factory.CourseFactory;
    import org.ruangong.newinstance.HtmlCourse;
    import org.ruangong.newinstance.ICourse;
    import org.ruangong.newinstance.JavaCourse;
    
    public class Student {
    	private int stuNo;
    	private String stuName;
    	private int stuAge;
    	public int getStuNo() {
    		return stuNo;
    	}
    	public void setStuNo(int stuNo) {
    		this.stuNo = stuNo;
    	}
    	public String getStuName() {
    		return stuName;
    	}
    	public void setStuName(String stuName) {
    		this.stuName = stuName;
    	}
    	public int getStuAge() {
    		return stuAge;
    	}
    	public void setStuAge(int stuAge) {
    		this.stuAge = stuAge;
    	}
    	@Override
    	public String toString() {
    		return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuAge=" + stuAge + "]";
    	}
    	
    	
    	
    	public void learnJavaWithIoc(){
    		ICourse course = new JavaCourse();
    		course.learn();
    	}
    	
    	public void learnHtmlWithIoc(){
    		ICourse course = new HtmlCourse();
    		course.learn();
    	}
    	
    	public void learnAnyone(String name){
    		ICourse course = CourseFactory.getCourse(name);
    		course.learn();
    	}
    }
    

      工厂类:

    package org.ruangong.factory;
    
    import org.ruangong.newinstance.HtmlCourse;
    import org.ruangong.newinstance.ICourse;
    import org.ruangong.newinstance.JavaCourse;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class CourseFactory {
    	
    	public static ICourse getCourse(String name){
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		if(name.equals("java")){
    			return (ICourse) context.getBean("JavaCourse");
    		}else{
    			return (ICourse) context.getBean("HtmlCourse");
    		}
    	}
    }
    

      测试类:

    public static void learnCourseWithFactory(){
    Student student = new Student();
    student.learnAnyone("java");
    }

    ApplicationContext.xml文件;

    <?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="student" class="org.ruangong.entity.Student">
    		<property name="stuNo" value="20173681"></property>
    		<property name="stuName" value="靳晨晨"></property>
    		<property name="stuAge" value="21"></property>
    	</bean>
    
    	<bean id="JavaCourse" class="org.ruangong.newinstance.JavaCourse"></bean>
    	<bean id="HtmlCourse" class="org.ruangong.newinstance.HtmlCourse"></bean>
    	
    </beans>
    

      

  • 相关阅读:
    win7下 iis配置 不能添加默认文档的 解决方案
    经典SQL语句大全
    Sql 行转列问题总结
    jQuery获取Select选择的Text和 Value(转)
    如何让虚拟目录里面的webconfig不继承网站的设置
    SQL2008 用户'sa'登录失败(错误18456)图文解决方法
    ASP.NET实现公历转农历的简单方法
    你的DNN站点慢了么?
    SQLServer2005删除log文件和清空日志的方案
    由于未能创建 Microsoft Visual C# 2008 编译器,因此未能打开项目 "xxx"
  • 原文地址:https://www.cnblogs.com/jccjcc/p/13976495.html
Copyright © 2020-2023  润新知