spring_helloworld

来源:互联网 发布:淘宝html5模板 编辑:程序博客网 时间:2024/06/15 03:43

spring是什么

spring是一个开源框架
spring为简化企业级应用开发而生,使用spring可以使简单的JavaBean实现以前只有EJB才能实现的功能。
spring是一个IOC(DI)和AOP容器框架。
IOC:反转控制  DI:依赖注入

IOC:其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,
而应用了IOC之后,则是容器主动的将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源。这种行
为也被称为查找的被动形式。

DI:IOC的另一种表述方式,即组件以一些预先定义好的方式(例如:setter方法)接受来自如容器的资源注入,相对于IOC而言,
这种表述更直接。




具体描述Spring:
-轻量级:spring是非侵入性的,基于Spring开发的应用中的对象可以不依赖于Spring的API。
-依赖注入(DI--dependency injection , IOC)
-面向切面编程(AOP--aspect oriented programming)
-容器:spring是一个容器,因为它包含并且管理应用对象的生命周期。
-框架:spring实现了使用简单的组建配置组合成一个复杂的应用,在spring中可以使用XML和java注解组合这些对象。
- 一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC)。



spring模块



(声明式事务)Data Access/Integration:JDBC ORM OXM JMS Transaction ---- AOP  Aspects
Web:WebSocket  Servlet   Web  Portlet ---Instrumentation(整合)  Messaging
面向切面编程:AOP  Aspects 
Instrumentation(整合)  Messaging
Core Container(核心容器)里可配置:Beans  Core   Context   SpEL

为开发更加便捷
安装SPRING TOOL SUITE 插件
•SPRING TOOLSUITE 是一个 Eclipse 插件,利用该插件可以更方便的在Eclipse 平台上开发基于 Spring 的应用。
•安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
–Help--> Install New Software...
–Click Add...
–In dialog Add Site dialog, click Archive...
–Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip  and click Open
–Clicking OK in the Add Site dialog will bring you back to the dialog'Install'
–Select the xxx/Spring IDE that has appeared
–Click Next and then Finish
–Approve the license
Restart eclipse when that is asked
安装时只点击带spring IDE的项


搭建Spring环境
把jar包加入到工程的classpath下(放到lib下后bulid path)
spring的配置文件:一个典型的spring项目需要创建一个或多个Bean配置文件,这些配置文件用于在Spring IOC容器里配置Bean,
Bean的配置文件可以放在classpath下,也可以放在其他目录下。


建立spring项目


Helloworld.java
package com.wul.spring.beans;public class Helloworld {private String name;public String getName() {return name;}public void setName(String name) {System.out.println("setName: "+name);this.name = name;}public void hello(){System.out.println("Hello "+name);}public Helloworld(){System.out.println("HelloWorld's Constructor...");}}

applicationContext.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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置bean --><bean id="helloworld" class="com.wul.spring.beans.Helloworld"><property name="name" value="wul"></property></bean></beans>

Main.java
package com.wul.spring.beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args){//1.创建Spring的IOC容器对象(创建容器时即会将配置文件里的内容执行)//ApplicatonContext代表 Spring 的IOC容器//ClassPatnXmlApplicationContext是ApplicatonContext的实现类,该实现类从类路径下来加载配置文件ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.从IOC容器中获取Bean实例//利用id定位到IOC容器中的beanHelloworld helloworld = (Helloworld) ctx.getBean("helloworld");//利用类型返回IOC容器中的Bean,但要求IOC容器中必须只有一个该类型的bean//Helloworld helloworld = (Helloworld) ctx.getBean(Helloworld.class);System.out.println(helloworld);//3.调用hello方法helloworld.hello();}}






0 0
原创粉丝点击