zoukankan      html  css  js  c++  java
  • SSH框架入门学习之二(spring)

    Spring也是一个开源框架,我在学习Spring的时候,认为最重要的几点是:IOC(控制反转)、AOP(面向切面)和容器概念。

    详细的教程还请大家去看网上的视频,这里贴一个小Demo以供学习。

    (前提是大家把该导入的jar包都导入了)

    1、Student类和Teacher类

    public class Student {
    	private String name;
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public void sayHello() {
    		System.out.println("Hi," + name);
    	}
    }
    

    import java.util.List;
    
    
    public class Teacher {
    	private List<Student> stuList;
    	private String name;
    	
    	public List<Student> getStuList() {
    		return stuList;
    	}
    	public void setStuList(List<Student> stuList) {
    		this.stuList = stuList;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public void myStu() {
    		System.out.println(name + " :");
    		for(int i = 0; i < stuList.size(); i++)
    			System.out.println(stuList.get(i).getName());
    	}
    }
    

    2、配置文件:applicationContext.xml的配置

    <?xml version="1.0" encoding="UTF-8"?

    > <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="student_1" class="Student"> <property name="name" value="小明"> </property> </bean> <bean id="student_2" class="Student"> <property name="name" value="小凡"> </property> </bean> <bean id="teacher" class="Teacher"> <property name="name" value="崔老师"/> <property name="stuList"> <list> <ref bean= "student_1"/> <ref bean= "student_2"/> </list> </property> </bean> </beans>


    3、主类:main(直接执行就可以)

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    		
    		Student stu = (Student) ac.getBean("student_1");
    		stu.sayHello();
    		
    		Teacher tec = (Teacher) ac.getBean("teacher");
    		tec.myStu();
    	}
    }
    


  • 相关阅读:
    图像有用区域--------深搜和广搜的第一次二选一
    24Pointgame-----24点游戏
    CAP定理和BASE理论
    并查集
    七桥问题和一笔画
    组合数问题--------一种新的 搜索 思路
    最少换乘 之简化版
    吝啬的国度 ---用vector 来构图
    WGZX:javaScript 学习心得--1
    eclipse Maven -->web project
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5223269.html
Copyright © 2011-2022 走看看