Spring第二章:Spring环境搭建

1. 导入 jar

  1.1 四个核心包一个日志包(commons-logging)

  

 导入方式为 选中包 右键add as library

2. src 下新建 applicationContext.xml

  2.1 文件名称和路径自定义.

  2.2 记住 Spring 容器 ApplicationContext,applicationContext.xml 置的信息最终存储到了 AppliationContext 容器中

  2.3 spring 配置文件是基于 schema

    2.3.1 schema 文件扩展名.xsd

    2.3.2 schema 理解成 DTD 的升级版.

      2.3.2.1 DTD 具备更好的扩展性.

    2.3.3 每次引入一个 xsd 文件是一个 namespace(xmlns)

  2.4 配置文件中只需要引入基本 schema

    2.4.1 通过<bean/> 创建对象.

    2.4.2 默认配置文件被加载时创建对象.

 1.application.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没有关系 之后ac.getBean("peo2",People.class); 的时候使用相同的名称获取就可以 class是对象的全地址-->
<bean id="peo2" class="com.bjsxt.pojo.People"></bean>
</beans>
2.People类
package com.bjsxt.pojo;

public class People {
    private int id;
    private String name;

    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + "]";
    }
}
3.测试类
package com.bjsxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;

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


        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = ac.getBean("peo2",People.class);
        System.out.println(people);
        
        

    }
}
4.执行测试类即可完成people类注册

 以上即为spring环境搭建

   

扫描二维码关注公众号,回复: 5346957 查看本文章

猜你喜欢

转载自www.cnblogs.com/reload-sun/p/10447548.html