实验1:通过IOC容器创建对象,并为属性赋值★

====================================

写一个helloworld

以前是自己new对象,现在所有的对象交给容器创建;给容器中注册组件

以后框架编写流程:

1)导包

要导的jar包,要使用这个部分的完整功能,这些jar都需要导入

1.核心容器(IOC):beans/core/context/expression,spring运行时依赖一个日志包没有就报错commons-logging

2.AOP+Aspects(面向切面编程模块):aop/aspect

3.单元测试模块:test

4.数据访问/Spring数据库访问模块:jdbc/orm(Object Relation Mapping)/oxm/jms(集成)/tx(事务)                                                     

5.web:Spring开发web应用的模块;:websocket(新技术)/web和原生的web相关(servlet)/webmvc,开发web项目的(web)/webmvc-portlet(开发web应用的组件集成)

2)写配置文件

spring的配置文件中,集合了spring的ioc容器管理的所有组件(会员清单)

在src下创建一个Spring Bean Configuration File(Spring的bean配置文件)

小白自学spring/从一个基本的helloworld开始

具体代码

<?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="001" class="asdasd.Person">

<property name="name" value="001"></property></bean>

<bean id="002" class="asdasd.Person">

<property name="name" value="002"></property></bean>

</beans>

写一个类

package asdasd;

public class Person {

private String name;

public Person() {

       super();

       System.out.println("person创建了...");

}

@Override

public String toString() {

       return "Person [name=" + name + "]";

}

public String getName() {

       return name;

}

public void setName(String name) {

       this.name = name;

}

}

3)测试

具体代码

package asdasd;

import static org.junit.Assert.*;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

/*

* 存在的几个问题:

* 1) 、src,源码包开始的路径,称为类路径的开始;

*         所有源码包里面的东西都会被合并放在类路径里面

* Java项目类路径:/bin/        

* web项目类路径:/WEB-INF/classes/

* 2)、导包commons-logging别忘了

* 3)先导包在创建配置文件(Java bean config那个)

* 4)Spring的容器接管了的类上有s标志

* 几个细节:

* 1)、ApplicationContext ()

* 2) 、给容器中注册一个组件;我们也从容器中按照id拿到了这件组件的对象?

*         组件的创建工作,是容器完成

* Person对象是什么时候创建好了呢?

* 容器中对象(所有的对象)的创建在容器创建完成的时候就已经创建了

* 3)同一个组件在ioc容器中是单实例的(就是不管引用多少回都是引的同一个)

* 4)容器中如果没有这个组件,获取组件会报错

* 5) ioc容器在创建这个组件对象时,会利用setter方法为javaBean的属性进行赋值

* 6)javaBean的属性名是由什么决定的?getter/setter方法是属性名;set去掉后面那个是名,然后getter/setter方法自动生成

* */

public class Testioc {

       @Test

       public void test() {

             //ApplicationContext:代表ioc容器

             //ClassPathXmlApplicationContext:当前应用的xml配置文件在

             //根据spring的配置文件得到ioc容器对象

             ApplicationContext ioc =new ClassPathXmlApplicationContext("iiiioc.xml");//创建ioc容器

       //容器帮我们创建好对象了;

             Person bean = (Person) ioc.getBean("002");

             System.out.println(bean);

       }

}

总结

new ClassPathXmlApplicationContext("iiiioc.xml);ioc容器的配置文件在类路径(src)下,所以是直接在src下找,如果放在src下的某个文件夹里,应该com/atguigu/bean/ioc.xml

new FileSystemXmlApplicationContext("F://ioc.xml");ioc容器的配置文件在磁盘路径下;小白自学spring/从一个基本的helloworld开始

 

相关文章: