spring-1:Spring helloWorld

来源:互联网 发布:linuxmysql源码安装 编辑:程序博客网 时间:2024/05/16 13:49

Spring简介

Spring是一个开源框架,可以极大简化企业级应用开发,Spring是一个IOC和AOP容器框架。

特点:

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


编程准备

安装Spring Tool Suit,Spring Tool Suit是基于eclipse的Spring应用集成开发环境。具体安装过程可以参考:http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html

搭建Spring开发环境:

创建一个名为Spring-1的java工程,在工程上右击->new->Folder   创建名为lib的文件夹,然后将以下jar包复制到lib目录中:


下载地址:http://pan.baidu.com/s/1gf9DQ99

将jar包 右键->build path -> add to build path

HelloWorld实例

在src目录下创建包名为:com.atguigu.spring.beans
在包下面新建两个类HelloWorld.java 和 Main.java 内容如下:
package com.atguigu.spring.beans;public class HelloWorld {private String name;public void setName(String name) {this.name = name;}public void hello() {System.out.println("hello: " + name);}}

package com.atguigu.spring.beans;public class Main {public static void main(String[] args) {//创建helloworld 的一个对象HelloWorld helloWorld = new HelloWorld();//为name属性赋值helloWorld.setName("spring");//调用方法.helloWorld.hello();}}

运行一下,可以看到打印输出  hello: spring
但是这个时候程序和Spring框架还没什么关系,可以看到在main方法中有三步,分别为:创建对象、赋值、调用方法。接下来让Spring框架来完成前两个步骤:
在src上右击->new ->other 找到Spring,下面有一个Spring Bean Configuration File

点击next,名为:applicationContext.xml
创建完成后,打开刚才生成的xml文件,发现文件中已经自动生成了一些内容,接下来添加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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置bean --><bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld"><property name="name" value="Spring"></property></bean></beans>

配置说明:
bean元素的id属性:一个代号,这个名称用于获取类的实例;
bean元素的class属性:用于生成bean的
property元素的name属性:用于对类中成员赋值,对应成员的set方法,要和set方法保持一致,比如将HelloWorld.java中的setName方法改为setName1,那么这里的name值也要改成name2;
property元素的value属性:set方法的参数。
接下来让Spring框架来完成main方法中的前两步骤:

package com.atguigu.spring.beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {/*//创建helloworld 的一个对象HelloWorld helloWorld = new HelloWorld();//为name属性赋值helloWorld.setName("spring");*///1.创建Spring的IOC容器对象ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//2.从IOC容器获取Bean实例HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");//调用方法.helloWorld.hello();}}

运行后会发现不仅输出了hello: Spring还打印了一些日志。

看看在
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
这一步发生了什么
将Main.java中的这两步注释掉:
//2.从IOC容器获取Bean实例HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");//调用方法.helloWorld.hello();

在HelloWorld.java中添加一些信息:
package com.atguigu.spring.beans;public class HelloWorld {private String name;public HelloWorld(){System.out.println("HelloWorld constructor");}public void setName2(String name) {this.name = name;System.out.println("setName:Spring");}public void hello() {System.out.println("hello: " + name);}}
这时在main方法中只运行:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
这一句,打印输出如下:
可以看到:在这一步调用了构造方法和set方法。



0 0
原创粉丝点击