初识spring
spring是什么
背景:
Rod Johnson是Spring框架的缔造者,他在2002编著的《Expert one-on-One J2EE Design and Development》一书中,对JavaEE系统架构臃肿、低效、脱离现实的种种现状提出了质疑,并积极寻求探索革新之道。以此书为指导思想,他编写了interface21框架,这是一个力图冲破JavaEE传统开发困境,从实际需求出发,着眼于轻便、灵巧,易于开发、测试和部署的轻量级开发框架。
Spring框架以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日发布了1.0正式版。同年Rod Johnson又推出了一部堪称经典的力作《Expert one-on-one J2EE Development without JEB》,该书在Java世界掀起了轩然大波,不断改变着Java开发者程序设计和开发的思维方式。在该书中,Rod Johnson根据自己多年丰富的实践经验,对EJB的各种笨重且臃肿的结构进行了逐一的分析和否定,并分别以简洁实用的方式替换之。从此,Spring框架得到快速发展。
概念:
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器开源框架。
轻量:轻量与重量是一个相对的概念,从大小与开销两方面而言Spring都是轻量的。EJB比较大,且其启动时需要消耗大量的内存、CPU等资源,所以是重量级框架;而Spring则不是,所以是轻量级框架。
容器:该容器中存放Java对象,并且管理其内部存储Java对象的生命周期;
开源:可以从Spring官网下载到Spring源代码;
框架:为解决一个开放性问题而设计的具有一定约束性的支撑结构。在此结构上可以根据具体问题扩展、安插更多的组成部分,从而更迅速和方便地构建完整的解决问题的方案。
spring的使用
第一步:在eclipse中安装spring插件
原因:通过在Eclipse IDE中安装Spring Tool Suite(STS)插件可以很方便地创建Spring、SpringMVC及Spring Boot工程。
第二步:spring使用,怎么创建java工程
如下目录:
1)先创建lib文件夹,导入如下四个jar包并add to bulid path;
2),创建spring,xml文件,配置了spring插件后有下述蓝色的选择,next,写文件名,spring文件创建成功。
第三步:配置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" default-lazy-init="default">
<bean id="obj" class="java.lang.Object"> </bean>
</beans>
package spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//初识spring
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");//创建对象,容器,并将对象装入容器。
Object object=applicationContext.getBean("obj");//此处内容与配置文件中额代码<bean id="obj" class="java.lang.Object"> </bean>中的id对齐
System.out.println(object);
applicationContext.close();
}
}
如下执行结果
bean的简单标签属性
以在applicationContext.xml中配置Student类为例:
创建学生类:
package com.zzu.vo;
public class Student {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//验证
public Student() {
super();
System.out.println(1);
}
}
<?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" default-lazy-init="default">
<bean name="student" class="com.zzu.vo.Student" lazy-init="default">
<property name="name" value="tom"></property>
</bean>
</beans>
package spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zzu.vo.Student;
public class Test {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");//创建对象,容器。
Student student=(Student) applicationContext.getBean("student");//上转型对象下转型。内容与name对齐。
System.out.println(student.getName());
}
}
1,name:与id类似,匹配getBean(“student”),
2,class:标示用Student类创建对象
3,new ClassPathXmlApplicationContext("application.xml");
代码执行解释
上述代码执行时:创建了一个容器,且给配置的类创建了对象,并将对象放到了容器,以下证明创建了对象。
在Student类中写如下代码
public class Student {
public Student() {
super();
System.out.println(1);
}
}
在application.xml种仅配置如下信息
<bean name="student" class="com.zzu.vo.Student"></bean>
执行Test中代码得到如下结果
4,<property name="name" value="tom"></property>
的作用:property标签为Student类中的属性name赋值。
public class Student {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在application.xml种仅配置如下信息
<bean name="student" class="com.zzu.vo.Student">
<property name="name" value="tom"></property> <!-- property标签为Student类中的属性name赋值 -->
</bean>
Text中代码如下
得到如下结果