spring框架

来源:互联网 发布:linux cp 文件夹覆盖 编辑:程序博客网 时间:2024/06/03 09:17

Spring 是什么?

Spring 是一个开源框架.
Spring 为简化企业级应用开发而生. 使用 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 是一个 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


把以下 jar 包加入到工程的 classpath 下:


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


代码实现:

第一阶段: HelloWorld

package com.atguigu.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name){
System.out.println("setName: "+name);
this.name=name;
}
public void hello(){
System.out.println("hello:"+name);
}
HelloWorld(){
System.out.println("HelloWorld 的构造方法");
}
}


第二个阶段:

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) {
/*
这些交给spring来完成
//创建helloworld
HelloWorld helloWorld=new HelloWorld();
//为name属性
helloWorld.setName("atguigu");
*/

//1、创建spring 的ioc容器对象
ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContent.xml");

//2、从ioc容器中获得ioc容器对象
//HelloWorld helloWorld=(HelloWorld) ctx.getBean("helloworld");

//调用hello方法
//helloWorld.hello();
}
}


第三阶段:配置文件ApplicationContent.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="helloworld" class="com.atguigu.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
</beans>



注意:

1、配置文件应该写在src下,不要写错文职了,否则就会报配置文件不存在的错误。


2、ApplicationContext ctx=
new ClassPathXmlApplicationContext("applicationContext.xml");

执行之后,


HelloWorld 的构造函数
setName...









0 0