初级spring学习

spring配置文件

<?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"
    default-autowire="byName"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <!-- 配置扫描器 -->
    <context:component-scan base-package="org.lanqiao.dao">
    </context:component-scan>

    <!-- 该文件中产生的所有对象,被spring放入了一个  称之为spring ioc容器的地方 -->
    <!-- id:唯一标识符    class:指定类型   -->
    <bean id="student" class="org.lanqiao.entity.Student">
        <!-- property:该class所代表的类的属性
            name:属性名
            value:属性值
        
        <property name="stuNo" value="2"></property>
        <property name="stuName" value="ls"></property>
        <property name="stuAge" value="24"></property>
         -->
    </bean>
    
    <bean id="javaCourse" class="org.lanqiao.newinstance.JavaCourse">
    </bean>
    <!-- 
    <bean id="studengDao" class="org.lanqiao.dao.StudentDaoImpl">
    </bean>
     -->
    
    
    <bean id="htmlCourse" class="org.lanqiao.newinstance.HtmlCourse"></bean>


    <bean id="teacher" class="org.lanqiao.entity.Teacher"  >
        <!-- 处理空值 ""  null -->
        <property name="name" >  
                <value>zs</value>  
        </property>
        <!--
        <property name="name" > 
                <value type="java.lang.String">z<![CDATA[<>&]]>s</value>  
        </property>
        <property name="name" value="z&lt;s"></property>
        <bean id="teacher" class="org.lanqiao.entity.Teacher" p:age="25" p:name="ww" >
         通过set方式赋值
        
        <property name="age" value="23"></property>
         -->
         <!-- 通过构造器赋值
         <constructor-arg value="24" index="1"></constructor-arg>
         <constructor-arg value="ls" index="0"></constructor-arg>
        
         <constructor-arg value="24" name="age"></constructor-arg>
         <constructor-arg value="ls" name="name"></constructor-arg>
          
          <constructor-arg value="24" type="int" index="1" name="age"></constructor-arg>
         <constructor-arg value="ls" type="String" index="0" name="name"></constructor-arg>
          -->
          
          <constructor-arg  type="int">
              <value>33</value>
          </constructor-arg>
    </bean>
    <!-- autowire="byName" :
        Course类中有一个ref属性teacher(属性名),并且 该ioc容器中恰好有一个 bean的id也是teacher
        bean的id值=类的属性名
            <bean id="course" class="org.lanqiao.entity.Course"  autowire="byName" >
                <bean id="course" class="org.lanqiao.entity.Course"  autowire="constructor" >
     -->
    <bean id="course" class="org.lanqiao.entity.Course"  autowire="byType" >
        <property name="courseName" value="java"></property>
        <property name="courseHour" value="200"></property>
        <!-- <property name="teacher" ref="teacher"></property> -->
    <!--
    <bean id="course" class="org.lanqiao.entity.Course"  p:teacher-ref="teacher"  p:courseHour="300" p:courseName="hadoop" >
    
    
          xx.setCourseName("java") 
            courseName ->setCourseName()
            courseName ->setCourseName() 
        
        <property name="courseName" value="java"></property>
        <property name="courseHour" value="200"></property>
         将teacher对象注入 到 course对象中
                xx.setTeacher(teacher);
         
        <property name="teacher" ref="teacher"></property>

        <constructor-arg value="c" name="courseName"></constructor-arg>
        <constructor-arg value="100"></constructor-arg>
        <constructor-arg ref="teacher"></constructor-arg>
        -->
    </bean>
    
    <bean id="collectionDemo" class="org.lanqiao.entity.AllCollectionType" >
        <!-- 通过set方式赋值 
        <property name="listElement">
            <list>
                <value>足球</value>
                <value>篮球</value>
                <value>乒乓球</value>
            </list>
        </property>
        -->
        
        <constructor-arg name="listElement">
                <list>
                    <value>足球xx</value>
                    <value>篮球xx</value>
                    <value>乒乓球xxx</value>
                </list>
        </constructor-arg>
        
        
        <property name="arrayElement">
            <!-- <array>
                <value>足球1</value>
                <value>篮球1</value>
                <value>乒乓球1</value>
            </array>
             -->
             <list>
                <value>足球1</value>
                <value>篮球1</value>
                <value>乒乓球1</value>
            </list>
        </property>
        
        <property name="setElement">
            <!-- <set>
                <value>足球2</value>
                <value>篮球1</value>
                <value>乒乓球2</value>
            </set>
             -->
            <list>
                <value>足球2</value>
                <value>篮球1</value>
                <value>乒乓球2</value>
            </list>
        </property>
        <property name="mapElement">
                <map>
                        <entry>
                            <key>
                                <value>foot</value>
                            </key>
                            <value>足球3</value>
                        </entry>
                        
                        <entry>
                            <key>
                                <value>basket</value>
                            </key>
                            <value>篮球3</value>
                        </entry>
                        
                        <entry>
                            <key>
                                <value>pp3</value>
                            </key>
                            <value>乒乓球</value>
                        </entry>
                </map>
        </property>
        <property name="propsElement">
            <props>
                <prop key="foot4">足球4</prop>
                <prop key="basket4">篮球4</prop>
                <prop key="pp4">乒乓球4</prop>
            </props>
        </property>
    
    </bean>
</beans>
View Code

StudentDaoImpl.java

package org.lanqiao.dao;

import org.lanqiao.entity.Student;
import org.springframework.stereotype.Repository;
/*
 * 
 * <bean id="studentDao" class="org.lanqiao.dao.StudentDaoImpl">
 */
//@Component("studentDao")
@Repository
public class StudentDaoImpl {
    public void addStudent(Student student) {
        System.out.println("增加学生..");
    }
}
View Code

AllCollectionType.java

package org.lanqiao.entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class AllCollectionType {
    private List<String> listElement ;
    private String[] arrayElement ; 
    private Set<String> setElement ; 
    private Map<String,String> mapElement;
    private Properties propsElement ;
    
    
    public AllCollectionType() {
    }

    
    
    public AllCollectionType(List<String> listElement) {
        this.listElement = listElement;
    }


    public List<String> getListElement() {
        return listElement;
    }


    public void setListElement(List<String> listElement) {
        this.listElement = listElement;
    }


    public String[] getArrayElement() {
        return arrayElement;
    }


    public void setArrayElement(String[] arrayElement) {
        this.arrayElement = arrayElement;
    }


    public Set<String> getSetElement() {
        return setElement;
    }


    public void setSetElement(Set<String> setElement) {
        this.setElement = setElement;
    }


    public Map<String, String> getMapElement() {
        return mapElement;
    }


    public void setMapElement(Map<String, String> mapElement) {
        this.mapElement = mapElement;
    }


    public Properties getPropsElement() {
        return propsElement;
    }


    public void setPropsElement(Properties propsElement) {
        this.propsElement = propsElement;
    }


    @Override
    public String toString() {
        //["aa","bb","cc"]
        String strContent = "" ;
        for(String str :arrayElement) {
            strContent += str +"," ;//aa,bb,cc,
        }
        
        return "list:"+this.listElement+"\nset:"+this.setElement+"\nmap:"+this.mapElement+"\nprop:"+this.propsElement +"\narray:"+strContent;
    }
    
    
}
View Code

Course.java

package org.lanqiao.entity;

public class Course {
    private String courseName;
    private int courseHour ;
    private Teacher teacher ;//�ڿ���ʦ  ��������Teacher
    public Course() {
    }
    
    public Course(Teacher teacher) {
        this.teacher = teacher;
    }
    
    public Course(String courseName, int courseHour, Teacher teacher) {
        this.courseName = courseName;
        this.courseHour = courseHour;
        this.teacher = teacher;
    }
    
    public String getCourseName() {
        return courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public int getCourseHour() {
        return courseHour;
    }
    public void setCourseHour(int courseHour) {
        this.courseHour = courseHour;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    
    public void showInfo() {
        System.out.println(this.courseName+","+this.courseHour+","+this.teacher.getName());
    }
    
}
View Code

Student.java

package org.lanqiao.entity;

import org.lanqiao.factory.CourseFactory;
import org.lanqiao.newinstance.HtmlCourse;
import org.lanqiao.newinstance.ICourse;
import org.lanqiao.newinstance.JavaCourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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 this.stuNo+","+this.stuName+","+this.stuAge;
    }
    
    
    
    
    public void learn(String name) {//ѧϰ�κογ�  java
        //1.���Լ���д�ļ򵥹����л�ȡ �γ�
//        ICourse course = CourseFactory.getCourse(name) ;
        //course���� ���name�õ�����Ӧ�Ŀγ�
        
        
        //ֱ�Ӵ�ioc�����л�ȡ
        //2.��SPringIOC�ṩ�ij��������л�ȡ �γ̣�֮ǰ���ù�bean��
        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
        ICourse course = (ICourse)conext.getBean(name);
        
        course.learn();
        
        
        
    }
    
    //ѧϰJava�γ�
    public void learnJava() {
        ICourse course = new JavaCourse();
        course.learn(); 
    }
    
    
    //ѧϰHtml�γ�
    public void learnHtml() {
    
        ICourse course = new  HtmlCourse() ;
        course.learn(); 
    }
}
View Code

Teacher.java

package org.lanqiao.entity;

public class Teacher {
    private String name;
    private int age ;
    
    public Teacher() {
    }
    
    
    
    public Teacher(int age) {
        this.age = age;
    }
    
    public Teacher(String name) {
        this.name = name;
    }
    
    /*
    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }
    */
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    } 
    
}
View Code

CourseFactory.java

package org.lanqiao.factory;

import org.lanqiao.newinstance.ICourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//�γ̹���
public class CourseFactory {
    //������ֻ�ȡ�γ�
//    public static ICourse getCourse(String name) {
//        //��ȡioc����
//        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
//        if(name.equals("java")) {
//            return  (ICourse)conext.getBean("javaCourse");//new->��ioc��ȡ
//        }else {
//            return (ICourse)conext.getBean("htmlCourse");
//        }
//    }
    
}
View Code

ICourse.java

package org.lanqiao.newinstance;
//�γ�
public interface ICourse {
    void learn();//ѧϰ...
}
View Code

JavaCourse.java

package org.lanqiao.newinstance;

public class JavaCourse implements ICourse{

    @Override
    public void learn() {
        System.out.println("ѧϰJava...");
    }
    
}
View Code

Test.java

package org.lanqiao.test;

import org.lanqiao.entity.AllCollectionType;
import org.lanqiao.entity.Course;
import org.lanqiao.entity.Student;
import org.lanqiao.factory.CourseFactory;
import org.lanqiao.newinstance.ICourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    public static void springIoc() {
//        Student student = new Student();
//        student.setStuNo(1);
//        student.setStuName("zs");
//        student.setStuAge(23);
//        System.out.println(student);
        
        //Spring�����Ķ���conext
        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
        //ִ�д�springIOC�����л�ȡһ�� idΪstudent�Ķ���
        Student student = (Student)conext.getBean("student") ;
        System.out.println(student);
        //1.new
        //2.�������Եĸ���
        
    }
    
    public static void learnCourse() {
        Student student = new Student();
        student.learnHtml();
        student.learnJava(); 
    }
    
    public static void collectionDemo() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
        AllCollectionType type = (AllCollectionType)context.getBean("collectionDemo") ;
        System.out.println(type);
    }
    
    
    public static void testDI() {
        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
        Course course = (Course)conext.getBean("course") ;
        course.showInfo();
    }
    
    public static void learnCourseWithFactory() {
        Student student = new Student();
        student.learn("html");        
        
    }
    
    public static void learnCourseWithIoC() {
        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
        //��IOC��ȡѧ�����
        Student student = (Student)conext.getBean("student") ;
        student.learn("javaCourse");
    }
    
    public static void main(String[] args) {

//        learnCourse();
//        learnCourseWithFactory();
//        learnCourseWithIoC();
        testDI();
//        collectionDemo();
        
        
        
    }
}
View Code

 

HtmlCourse.java

package org.lanqiao.newinstance;

public class HtmlCourse  implements ICourse{

    @Override
    public void learn() {
        System.out.println("ѧϰHtml....");
    }

}
View Code
posted @ 2019-04-18 17:01  小小超plus  阅读(208)  评论(0编辑  收藏  举报