发表于: 2017-03-08 21:21:15

2 1367


今天完成的事情: 

使用maven构建spring的小程序,理解IOC机制。
明天计划的事情:

继续学习spring
遇到的问题:

更换了一个maven的镜像,终于没有问题了。。

收获:

利用maven构建spring程序步骤:

1、在pom.xml中添加依赖

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>4.2.1.RELEASE</version>

  </dependency>

2、在src/main/resources中配置bean

<?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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

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.xsd">

//设置bean的id和class

<bean id="screwDriver" class="screwDriver.ScrewDriver"></bean>       

</beans>

3、利用IOC容器获取对象。

       IOC机制可以分为以下三个阶段:

3.1、初始化容器 ApplicationContext= new ClassPathXmlApplicationContext("application-context.xml")

3.2、获取对象   ScrewDriver sd=(ScrewDriver)context.getBean("screwDriver");

3.3、使用对象    sd.use();

结果:

screwDriver.ScrewDriver类:

测试main程序:

结果:


返回列表 返回列表
评论

    分享到