spring搭建过程

spring搭建过程
 1.导包
 2.创建对象
  |-创建一个实体类User,有姓名和年龄两个属性,加入get()和set()方法
 3.书写配置注册对象到容器中
  |-在项目的src下创建一个applicationContext.xml(建议放在src下,配置文件名任意)
  |-引入约束文件
   |-首先在applicationContext.xml中写下<beans></beans>标签
   |-然后再eclipse的Window->Preferences->XML Catalog->Add
    |-Location中 D:\SSH框架jar包\spring\spring-framework-4.2.4.RELEASE\schema\beans\spring-beans-4.2.xsd
    |-Key type中 Schema location
    |-key中 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd(在beans后面添加/spring-beans-4.2.xsd)
   |-打开applicationContext.xml的Design
    |-右键beans->Edit Namespaces
     |-先导入一个xsi,点击ok
     |-再点击Add,选中specify New Namespace,点击Browse->选中Select XML Catalog entry,在里面选中刚才导入的
     schema中的beans,点击ok,
     Location hint:  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     Namespace Name: http://www.springframework.org/schema/beans
     Prefix:为空
  |-引入完成的结果:
   <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
     |-将User对象交给spring容器管理
       <!-- 将User对象交给spring容器管理 -->
    <bean name="user" class="com.test.bean.User"></bean>
    4.代码测试
     public class Demo {

    @Test
    public void fun() {
     //1.创建容器对象
     ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
     //2向容器要user对象
     User u = (User)ac.getBean("user");
     //3打印user对象
     System.out.println(u);
    }
   }

posted @ 2018-04-14 17:00  手心上亘古的月光  阅读(120)  评论(0)    收藏  举报