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实例
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
<?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>
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");这一步发生了什么
//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");这一句,打印输出如下:
- spring-1:Spring helloWorld
- 1、Spring之HelloWorld
- spring(1) helloworld
- java spring(1) HelloWorld
- Spring-1 helloworld
- Spring helloworld
- spring helloworld
- spring helloworld
- Spring -- HelloWorld
- spring helloworld
- Spring HelloWorld
- Spring--HelloWorld
- spring HelloWorld!
- spring-HelloWorld
- spring helloworld
- Spring-HelloWorld
- spring HelloWorld
- Spring(1)--初识Spring之HelloWorld
- 数据库索引的实现原理—SQL Server
- Android完美解决输入框EditText隐藏密码打勾显示密码问题
- linux C 网络编程基础
- 91. Decode Ways
- 新的开始
- spring-1:Spring helloWorld
- springMVC入门--传递参数的方式
- 逆向工程之表达式优化识别(4)-除法(下)
- iOS 日常工作之常用宏定义大全
- 前端框架
- 7月5号,一个新的开始,
- JQ添加移除css样式
- POJ3258 River Hopscotch
- sqoop命令举例