天天看点

【Spring笔记】01、Spring环境搭建,STS工具,第一个Spring程序,解耦合发展史,控制反转,依赖注入

2002 Rod Johnon <Expoer One-on-one j2eedevelopment and Design>

Spring  2003  ,IOC  Aop

Spring data,spring boot,spring cloud,spring framework ,spring social 

IOC :控制反转(DI:依赖注入)

1.搭建Spring环境

下载jar

http://maven.springframework.org/release/org/springframework/spring/

spring-framework-4.3.9.RELEASE-dist.zip

开发spring至少需要使用的jar(5个+1个):

spring-aop.jar        开发AOP特性时需要的JAR

spring-beans.jar    处理Bean的jar            <bean>

spring-context.jar    处理spring上下文的jar        <context>

spring-core.jar        spring核心jar

spring-expression.jar    spring表达式 

三方提供的日志jar

commons-logging.jar    日志

2.编写配置文件

为了编写时有一些提示、自动生成一些配置信息:

方式一:增加sts插件

可以给eclipse增加 支持spring的插件:spring tool suite(https://spring.io/tools/sts/all)

下载springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite.zip,然后在Eclipse中安装:Help-Install new SoftWare.. - Add

方式二:(推荐)

    直接下载sts工具(相当于一个集合了Spring tool suite的Eclipse): https://spring.io/tools/sts/

新建:bean configuration .. - applicationContext.xml(该文件中产生的所有对象,被Spring放入一个称之为Spring IOC容器的地方)

3.开发Spring程序(IOC)

        ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml") ;

        //执行从springIOC容器中获取一个 id为student的对象

        Student student = (Student)conext.getBean("student") ;

可以发现,springioc容器 帮我们new了对象,并且给对象赋了值

【Spring笔记】01、Spring环境搭建,STS工具,第一个Spring程序,解耦合发展史,控制反转,依赖注入

4.SpringIOC发展史

Student student = new Student();

student.setXxx();

②简单工厂

③ioc (超级工厂)

【Spring笔记】01、Spring环境搭建,STS工具,第一个Spring程序,解耦合发展史,控制反转,依赖注入

总结:ioc/di ,无论要什么对象,都可以直接去springioc容器中获取,而不需要自己操作(new\setXxx())

因此之后的ioc分为2步:①先给springioc中存放对象并赋值  ②拿对象

继续阅读