一、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环境
1. eclipse安装spring tool suite
spring tool suite 是一个 eclipse 插件,利用该插件可以更方便的在 eclipse 平台上开发基于 spring 的应用。
2. 加包
把以下的jar包加入到工程的classpath:
3. spring的配置文件:一个典型的spring项目需要创建一个或多个bean配置文件,这些配置文件用于在spring ioc容器中配置bean。bean的配置文件可以放在classpath下,也可以放在其他目录下。
4. 建立spring项目,编写helloworld:
package com.atguigu.spring.beans;
public class helloworld {
private string name;
public void setname(string name) {
system.out.println("setname...");
this.name = name;
}
public void hello(){
system.out.println("hello " + name);
}
public helloworld() {
system.out.println("helloworld's construct...");
}
}
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<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 = new helloworld();
helloworld.setname("spring");
helloworld.hello();
*/
//1.创建容器
applicationcontext ctx = new classpathxmlapplicationcontext("appliactioncontext.xml");
//2.从容器中获取bean
helloworld hello = (helloworld) ctx.getbean("helloworld");
//3.调用bean的方法
hello.hello();
}
}
5.测试结果
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
更多java spring快速入门。