文章目录
一、spring介绍
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
◆范围:任何Java应用
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。[百度百科]
可以概括为:spring有两大核心:spring ioc 容器(管理bean等)和 spring aop面向切面编程(可用于通知,日志等).
◆spring 可以整合其他框架,包括但不仅限于 springmvc,mybatis,hibernate等,
◆本次spring系列学习心得将介绍spring,springmvc,mybatis 三大核心框架(ssm)
◆以及一些辅助框架和工 具,例如jsp页面模板引擎,html,css,javascript,jquery等。
二、spring框架搭建(JavaSE)
本次主要在于spring的搭建与ioc的使用
(一) 下载sts 工具(eclipse的一款基于spring+原生eclipse做的强大IDE)
下载地址:(任选一个)
sts3,旧版(笔者所用)
最新版sts4,(与sts3都可以用)
下载后傻瓜式安装,最后设置一个工作区间(workplaces)。
(二) 开始新建一个JavaSE spring项目
1.新建项目
打开sts3,新建一个普通的Java项目(file->new->other…->java->java project),项目名为:Spring-Demo-1
如果跟着做,那么新建的项目视图应该是:
2.准备spring javaSE 项目所需要的jar(这里先用jar,后续会使用maven)
这里先罗列项目所需要的一些最基本的jar:
[核心]
spring-expresion.jar # 处理spring表达式
spring-context.jar # 处理spring上下文
spring-core.jar # 处理spring 核心
spring-beans.jar # 处理bean
commons-log # 处理日志
spring-aop.jar # 处理spring aop(控制反转)
aopaliance.jar # aop
aspectjweaver.jar # aop
为了 避免版本兼容性问题,请保持版本一致:
可以在maven仓库下载:maven repository
为了方便,可以下载经过我事先全部准备好的所有基本的jar,跳转下载:基本的jar包
准备好jar之后,在项目的src 下建立 一个lib文件夹,用于存放 所有lib,然后全部添加到类路径:
下面来测试一下吧:
①在src 目录下新建spring配置文件 applicationContext.xml:
new->other…->spring->spring bean configuration file>输入文件名applicationContext.xml;
然后点击namespace,勾选aop(面向切面),context(上下文对象),选完再切回到 source界面
配置文件自动生成为:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
</beans>
在这里我们新建一个实体包 org.jnr.pojo,和两个实体类 org.jnr.pojo.Dog 和 org.jnr.pojo.Master 用于测试:
Dog类:
package org.jnr.pojo;
/**
* 狗实体类
*
* @author JNR
*
*/
public class Dog {
private Integer dogId;// id
private String dogName;// 昵称
private Integer dogAge;// 年龄
private Master master;// 归属于哪个主人
// 使用builder 构造器
// 全参构造
public Dog(Integer dogId, String dogName, Integer dogAge, Master master) {
super();
this.dogId = dogId;
this.dogName = dogName;
this.dogAge = dogAge;
this.master = master;
}
// 最好加上
public Dog() {
super();
}
public Integer getDogId() {
return dogId;
}
public void setDogId(Integer dogId) {
this.dogId = dogId;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public Integer getDogAge() {
return dogAge;
}
public void setDogAge(Integer dogAge) {
this.dogAge = dogAge;
}
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
@Override
public String toString() {
return "Dog [dogId=" + dogId + ", dogName=" + dogName + ", dogAge=" + dogAge + ", master=" + master + "]";
}
}
Master 类
package org.jnr.pojo;
import java.util.List;
/**
* 主人实体类
*
* @author JNR
*
*/
public class Master {
private Integer masterId;// id
private String masterName;// 名字
private Integer masterAge;// 年龄
private List<Dog> listDogs;// 拥有的所有狗的集合
public Master(Integer masterId, String masterName, Integer masterAge, List<Dog> listDogs) {
super();
this.masterId = masterId;
this.masterName = masterName;
this.masterAge = masterAge;
this.listDogs = listDogs;
}
public Master() {
super();
// TODO Auto-generated constructor stub
}
public Integer getMasterId() {
return masterId;
}
public void setMasterId(Integer masterId) {
this.masterId = masterId;
}
public String getMasterName() {
return masterName;
}
public void setMasterName(String masterName) {
this.masterName = masterName;
}
public Integer getMasterAge() {
return masterAge;
}
public void setMasterAge(Integer masterAge) {
this.masterAge = masterAge;
}
public List<Dog> getListDogs() {
return listDogs;
}
public void setListDogs(List<Dog> listDogs) {
this.listDogs = listDogs;
}
@Override
public String toString() {
return "Master [masterId=" + masterId + ", masterName=" + masterName + ", masterAge=" + masterAge
+ ", listDogs=" + listDogs + "]";
}
}
②测试工作
实例:将实体类bean纳入spring管理:(注解形式)
首先在需要纳入ioc的实体类上添加@Component(“my_dog”) 注解,也可以@Component,即使用类名(首字符小写)作为bean名:
@Component
public class Master {
@Component("my_dog")
public class Dog {
然后在applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描带有注解的包 ,将其纳入spring ioc 管理-->
<context:component-scan base-package="org.jnr"/>
</beans>
新建一个包org.jnr.test,用于存放所有测试类,再新建一个 测试类:SpringDemo1Test:
package org.jnr.test;
import java.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo1Test {
/**
* 测试配置文件 applicationContext.xml
*/
public static void demo1() {
// 读取spring配置文件,获取Spring上下文对象context
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 打印在 spring ioc 容器中的所有对象
System.out.println("bean总数:" + context.getBeanDefinitionCount());
System.out.println("bean名字:" + Arrays.toString(context.getBeanDefinitionNames()));
}
public static void main(String[] args) {
demo1();
}
}
测试结果:
七月 22, 2020 6:05:08 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@76fb509a: startup date [Wed Jul 22 18:05:08 CST 2020]; root of context hierarchy
七月 22, 2020 6:05:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
bean总数:8
bean名字:[my_dog, master, org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.event.internalEventListenerProcessor, org.springframework.context.event.internalEventListenerFactory]
另外除了注解形式将bean纳入spring ioc,还可以直接在spring 配置文件中添加:
<bean id="随便起,如果上下文没有用到可以不设置" class="org.jnr.pojo.Dog" name="little-dog">
<property name="dogId" value="1"/>
<property name="dogName" value="little dog"/>
<property name="master" ref="ref-master"/>
</bean>
<bean id="ref-master" class="org.jnr.pojo.Master" name="xiao-ming">
<property name="masterId" value="2"/>
<property name="masterName" value="xiao ming"/>
</bean>
结果:
项目源码参考