spring学习-1.使用Maven构建spring项目

1.新建一个Maven项目

项目的结构图

2.配置pom.xml,引入项目需要的依赖,这里引入了spring,junit



    4.0.0

    edu.njxz.demo
    springTest
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            5.0.6.RELEASE
        
        
            junit
            junit
            4.12
        
    


3.新建一个People类

package edu.njxz.demo.bean;

public class People {
    private String name;  //姓名
    private int age;      //年龄

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4.创建spring的beans.xml,通过spring的配置文件来完成对象的注入


  
  
      
          
          
      
  

5.进行测试

package edu.njxz.demo.test;

import edu.njxz.demo.bean.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    public static void main(String[] args) {

        //把beans.xml的类加载到容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");

        //从容器中获取bean
        People people= (People) applicationContext.getBean("people");
        System.out.println(people.getName()+" "+people.getAge());
    }
}

6.控制台的打印结果

你可能感兴趣的:(spring)