1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)

来源:互联网 发布:网络应急预案演练记录 编辑:程序博客网 时间:2024/04/29 05:30

一、Spring是什么

Spring 是一个开源框架。
Spring 为简化企业级应用开发而生(主要针对EJB2来说),使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。

Spring 是一个 IOC(DI) 和 AOP 容器框架。


二、具体描述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模块



四、安装 SPRING TOOL SUITE

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



五、搭建 Spring 开发环境

1、创建maven工程

2、配置pom.xml引入spring的jar包

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>4.3.4.RELEASE</version>    </dependency></dependencies>

3、Spring 的配置文件:

一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目录下

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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <bean id="helloWorld" class="cn.wwy.spring.base.HelloWorld">        <property name="user" value="Spring"></property>    </bean></beans>

4、建立Spring项目

public class HelloWorld {private String user;public HelloWorld() {System.out.println("HelloWorld's constructor...");}public void setUser(String user) {System.out.println("setUser:" + user);this.user = user;}public HelloWorld(String user) {this.user = user;}public void hello(){System.out.println("Hello: " + user);}}
public class Main {public static void main(String[] args) {//HelloWorld helloWorld = new HelloWorld();//helloWorld.setUser("Tom");//helloWorld.hello(); //1.创建Spring的IOC容器对象ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.从IOC容器中获取bean实例HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");//3.调用hello方法helloWorld.hello();}}
5、运行结果



0 0
原创粉丝点击