您的位置:首页 > 编程语言 > Java开发

[原创]java WEB学习笔记96:Spring学习---Spring简介及HelloWord

2016-10-10 23:27 489 查看

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.spring简介

  1)Spring 是一个开源框架.

  2)Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.

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

  4)轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API  

  5)依赖注入(DI --- dependency injection、IOC)

  6)面向切面编程(AOP --- aspect oriented programming)

  7)容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期

  8)框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象

  9)一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC

2.sping的模块

  


3.安装spring tool 插件

        


4.spring 版本的helloword

  HelloWord 

package com.jason.spring.beans;

public class HelloWord {

private String name;

public void setName(String name) {
this.name = name;
}

public void hello(){
System.out.println("hello:" + name);
}

}


Main.java

package com.jason.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

/*
* 传统方式
//1.创建Helloword 对象
HelloWord helloWord = new HelloWord();

//2.为name 赋值
helloWord.setName("jason");

//调用hello 方法
helloWord.hello();
*/

//创建对象和为对象的属性赋值,可以由spring来负责
//1.创建Spring 的IOC 容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");

//2.从IOC 容器中获取bean实例

HelloWord helloWord = (HelloWord) applicationContext.getBean("helloWord");

//调用hello()方法
helloWord.hello();

}

}


ApplicatoinContext.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.xsd"> 
<!-- 配置bean -->

<bean id="helloWord" class="com.jason.spring.beans.HelloWord">
<property name="name" value="Spring"></property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐