禁止私自转载 !!!
导读
在Java语言编程中类与类之间的关系:
依赖(dependency):是两个事物之间的语义关系,其中一个事物(独立事物)发生变化, 会影响到另一个事物(依赖事物)的语义
关联(association):是一种结构关系,它指明一个事物的对象与另一个事物的对象间的联系
泛化(generalization):是一种特殊/一般的关系。也可以看作是常说的继承关系
实现(realization):是类元之间的语义关系,其中的一个类元指定了由另一个类元保证执行的契约
ps: 通常我们都会去通过new多个对象来实现,某个类中调用多个类,从而使我们的代码耦合度变高,是的代码不容易更改,所以为的减少或者不去new新对象达到“高内聚低耦合”,所以出现了下面所用到的Spring
一、Spring框架的基本使用
1.基本概念
Spring 是一个开源框架,它是由Rod Johnson创建,是为了解决企业应用程序开发复杂性创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
ps: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
◆轻量-----从大小与开销两方面而言Spring都是轻量的。
完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。
并且Spring所需的处理开销也是微不足道的。
此外,Spring是非嵌入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。
◆控制反转-----Spring通过一种称作控制反转(IoC)的技术促进了松耦合。
当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是
这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容
器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
◆面向切面-----Spring提供了面向切面编程的丰富支持,允许通过分离应用的
业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行
内聚性的开发。
应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责
(甚至是意识)其它的系统级关注点,例如日志或事务支持。
◆容器-----Spring包含并管理应用对象的配置和生命周期,在这个意义上它
是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),
你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何
相互关联的。然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与
笨重的,难以使用。
◆框架-----Spring可以将简单的组件配置、组合成为复杂的应用。
在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提
供了很多基础功能(事务管理、持久化框架集成等等)。
所有Spring的这些特征使程序员能够编写更干净、更可管理、并且更易于测试的代码。
它们也为Spring中的各种模块提供了基础支持。
◆方便解耦,简化开发
通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring
进行控制,避免硬编码所造成的过度程序耦合。有了Spring,用户不必再为单
实例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的
应用。
◆AOP编程的支持
通过Spring提供的AOP功能,方便进行面向切面的编程,许多不容易用传
统OOP实现的功能可以通过AOP轻松应付。
◆声明式事务的支持
在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过声明
式方式灵活地进行事务的管理,提高开发效率和质量。
◆方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,在Spring里,测
试不再是昂贵的操作,而是随手可做的事情。
◆方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,相反,Spring可以降低各种框架的
使用难度,Spring提供了对各种优秀框架(如Struts,Hibernate、
Hessian、Quartz)等的直接支持。
◆降低Java EE API的使用难度
Spring对很多难用的Java EE API(如JDBC,JavaMail,远程调用等)
提供了一个薄薄的封装层,通过Spring的简易封装,这些Java EE API的使用
难度大为降低。
◆Java 源码是经典学习范例
Spring的源码设计精妙、结构清晰、匠心独用,处处体现着大师对Java设
计模式灵活运用以及对Java技术的高深造诣。Spring框架源码无疑是Java技术
的最佳实践范例。如果想在短时间内迅速提高自己的Java技术水平和应用开发水
平,学习和研究Spring源码将会使你收到意想不到的效果
2.Spring框架的基本使用案例
Bodywork.java
package com.rj.bd.beans;
/**
* @desc 车体
* @author HYZ
* @time 2021年3月11日
*/
public class Bodywork {
private String length;//车长
private String width;//车宽
private String high;//车高
private String color;//颜色
private String gateCount;//门数
@Override
public String toString() {
return "[车长=" + length + ", 车宽=" + width + ", 车高=" + high +
", 颜色=" + color
+ ", 门数=" + gateCount + "]";
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getGateCount() {
return gateCount;
}
public void setGateCount(String gateCount) {
this.gateCount = gateCount;
}
}
Engine.java
package com.rj.bd.beans;
/**
* @desc 发动机
* @author HYZ
* @time 2021年3月11日
*/
public class Engine {
private String engineNo;//型号
private String factory;//生产厂家
private String power; //最大公率:135马力
private String emission;//排量:2.0升
public Engine(String engineNo, String factory, String power, String emission) {
this.engineNo = engineNo;
this.factory = factory;
this.power = power;
this.emission = emission;
}
@Override
public String toString() {
return "{型号=" + engineNo + ", 生产厂家=" + factory +
", 最大功率=" + power + ", 排量=" + emission
+ "]";
}
public String getEngineNo() {
return engineNo;
}
public void setEngineNo(String engineNo) {
this.engineNo = engineNo;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
public String getEmission() {
return emission;
}
public void setEmission(String emission) {
this.emission = emission;
}
}
UnderPan.java
package com.rj.bd.beans;
/**
* @desc 底盘
* @author HYZ
* @time 2021年3月11日
*/
public class Underpan {
private String gearbox;//变速箱:手动,自动,无级变速CVT,双离合DSG,序列多挡变速
private String clutch;//离合器:无
private String gimbal;//万向节:无
private String driveshaft;//传动轴:无
@Override
public String toString() {
return "[变速箱=" + gearbox + ", 离合器=" + clutch +
", 万向节=" + gimbal + ", 传动轴="
+ driveshaft + "]";
}
public String getGearbox() {
return gearbox;
}
public void setGearbox(String gearbox) {
this.gearbox = gearbox;
}
public String getClutch() {
return clutch;
}
public void setClutch(String clutch) {
this.clutch = clutch;
}
public String getGimbal() {
return gimbal;
}
public void setGimbal(String gimbal) {
this.gimbal = gimbal;
}
public String getDriveshaft() {
return driveshaft;
}
public void setDriveshaft(String driveshaft) {
this.driveshaft = driveshaft;
}
}
Car.java
package com.rj.bd.beans;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @desc 一辆车
* @author HYZ
* @time 2021年3月11日
*/
public class Car {
private String name; //车名称
private String factory;//生产厂家:奔驰,宝马
private String price;//销售价格
private String type; //车级别:小型车,紧凑型,中型车,SUV,MPV
@Autowired
private Engine engine;
private Bodywork bodywork;
private Underpan underpan;
@Override
public String toString() {
return "基本信息: {车名称=" + name + ", 生产厂家=" + factory +
", 销售价格=" + price + ", 车级别=" + type + "}\n "+
"发动机信息:"+ engine +"\n" +
"车身信息:" + bodywork +"\n"+
"底盘信息:" + underpan + "\n";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Bodywork getBodywork() {
return bodywork;
}
public void setBodywork(Bodywork bodywork) {
this.bodywork = bodywork;
}
public Underpan getUnderpan() {
return underpan;
}
public void setUnderpan(Underpan underpan) {
this.underpan = underpan;
}
}
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-3.0.xsd">
<!-- ========宝马730Li 典雅版的配置信息========================================================================= -->
<bean id="car" class="com.rj.bd.beans.Car" scope="prototype" >
<property name="name">
<value>宝马730Li 典雅版</value> <!-- 注入字符串常量 -->
</property>
<property name="factory">
<value>德国柏林</value> <!-- 注入字符串常量 -->
</property>
<property name="price">
<value>89.80万</value> <!-- 注入字符串常量 -->
</property>
<property name="type">
<value>豪华车</value> <!-- 注入字符串常量 -->
</property>
<property name="engine">
<ref bean="engine"/> <!-- 注入对象 -->
</property>
<property name="bodywork">
<ref bean="bodywork"/> <!-- 注入对象 -->
</property>
<property name="underpan">
<ref bean="underpan"/> <!-- 注入对象 -->
</property>
</bean>
<bean id="bodywork" class="com.rj.bd.beans.Bodywork" scope="prototype">
<property name="length"> <!-- 设值注入 -->
<value>5212</value>
</property>
<property name="width"> <!-- 设值注入 -->
<value>1902</value>
</property>
<property name="high"> <!-- 设值注入 -->
<value>1478</value>
</property>
<property name="color"> <!-- 设值注入 -->
<value>银色</value>
</property>
<property name="gateCount"> <!-- 设值注入 -->
<value>4门5座3厢车</value>
</property>
</bean>
<bean id="engine" class="com.rj.bd.beans.Engine" >
<constructor-arg index="0"> <!-- 构造注入 -->
<value>EA113</value>
</constructor-arg>
<constructor-arg index="1"> <!-- 构造注入 -->
<value>一汽-大众汽车有限公司</value>
</constructor-arg>
<constructor-arg index="2"> <!-- 构造注入 -->
<value>125KW</value>
</constructor-arg>
<constructor-arg index="3"> <!-- 构造注入 -->
<value>2.0升</value>
</constructor-arg>
</bean>
<bean id="underpan" class="com.rj.bd.beans.Underpan" >
<property name="gearbox"> <!-- 设值注入 -->
<value>6档手自一体</value>
</property>
<property name="clutch"> <!-- 设值注入 -->
<value>-</value>
</property>
<property name="gimbal"> <!-- 设值注入 -->
<value>-</value>
</property>
<property name="driveshaft"> <!-- 设值注入 -->
<value>-</value>
</property>
</bean>
<!-- ========奥迪A6L 2011款 2.0 TFSI 基本型的配置信息========================================================================= -->
<bean id="car2" class="com.rj.bd.beans.Car" >
<property name="name" value="奥迪A6L 2011款 2.0 TFSI 基本型"/>
<property name="factory" value="德国"/>
<property name="price" value="35.50万"/>
<property name="type" value="中大型车"/>
<property name="engine">
<bean class="com.rj.bd.beans.Engine" >
<constructor-arg index="0" value="EA113"/>
<constructor-arg index="1" value="一汽-大众汽车有限公司"/>
<constructor-arg index="2" value="125KW"/>
<constructor-arg index="3" value="2.0升"/>
</bean>
</property>
<property name="bodywork">
<bean class="com.rj.bd.beans.Bodywork" >
<property name="length" value="5035"/>
<property name="width" value="1855"/>
<property name="high" value="1485"/>
<property name="color" value="黑色"/>
<property name="gateCount" value="4门5座3厢车"/>
</bean>
</property>
<property name="underpan">
<bean class="com.rj.bd.beans.Underpan">
<property name="gearbox" value="6档手动"/>
<property name="clutch" value="-"/>
<property name="gimbal" value="-"/>
<property name="driveshaft" value="-"/>
</bean>
</property>
</bean>
</beans>
Test.java
package com.rj.bd.test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.rj.bd.beans.Car;
/**
* @desc 测试类
* @author HYZ
* @time 2021年3月11日
*/
public class Test {
public static void main(String[] args) {
//找到项目中的spring.xml
ClassPathResource rs = new ClassPathResource("spring.xml");
//加载当前的xml文件且构建出一个bean工厂
XmlBeanFactory beanFactory = new XmlBeanFactory(rs);
Car car = (Car)beanFactory.getBean("car");
System.out.println(car);
}
}
测试结果
PS:通过使用spring框架使得我们在测试类Test.java类中new减少了,也体现了高内聚低耦合。
3.Spring框架的两个主要概念
1.IOC(Inversion of Control)控制反转
IOC,直白来说就是由容器控制程序之间的关系,而非传统实现中由程序代码直接操控。 这也就是所谓“控制反转”的概念所在,即:控制权由应用代码(java代码)中转到了外部容器(spring框架的spring.xml文件),而这控制权的转移,就是是所谓的反转
2.DI(Dependency Injection) 依赖注入
所谓依赖注入,即组件(可以理解是javabean)之间的依赖关系由容器(spring框架的spring.xml文件)在运行期决定,形象的来说,就是由容器(spring.xml)动态的将某种依赖关系注入到组件之中。
4.Spring框架注入的数据类型
1.创建一个User.java
//基本数据类型:且是通过设值注入的方式进行赋值(set和get省略)
public String name;
public int age;
2.在src下创建一个spring4.xml文件,将下面数据添加到spring4.xml的bean中
<property name="name"> <!--设置注入-->
<value>凌异州</value>
</property>
<property name="age">
<value>20</value>
</property>
3.创建一个测试类Test.java
package com.rj.bd.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.rj.bd.beans.User;
/**
* @desc 注入数据的类型之基本数据类型
* @author HYZ
* @time 2021年3月11日
*/
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring4.xml");
User user = (User) applicationContext.getBean(User.class);
System.out.println(user);
}
}
1.先创建一个Computer类
package com.rj.bd.beans;
/**
* @desc Computer实体类,目的是将来以bean的形式注入给User
* @author HYZ
* @time 2021年3月11日
*/
public class Computer {
public int price;
public String computerName;
@Override
public String toString() {
return "[价格=" + price + ", 电脑=" + computerName + "]";
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getComputerName() {
return computerName;
}
public void setComputerName(String computerName) {
this.computerName = computerName;
}
public Computer(int price, String computerName) {
this.price = price;
this.computerName = computerName;
}
}
2.在User.java类中引入:
//一个bean对象的注入(set和get省略了)
public Computer computer;
3.在spring4.xml中实现注入一个bean
//放入id为user的bean中
<property name="computer">
<ref bean="comp" />
</property>
//新建一个关于comp电脑的bean
<bean id="comp" class="com.rj.bd.beans.Computer" >
<constructor-arg index="0"> <!--构造注入-->
<value>6000</value>
</constructor-arg>
<constructor-arg index="1">
<value>华为</value>
</constructor-arg>
</bean>
1.在User.java类中引入:
//注入集合(set和get省略)
private Set<String> sets ;
private List<String> lists ;
private Properties properties ;
private Map<String, String> maps ;
2.将下面数据添加到spring4.xml的bean中
<property name="lists">
<list>
<value>严景致</value>
<value>聂星辰</value>
<value>楚炎</value>
</list>
</property>
<property name="sets">
<set>
<value>羽毛球</value>
<value>台球</value>
<value>乒乓球</value>
</set>
</property>
<property name="maps">
<map>
<entry key="userName" value="夏林" />
<entry key="password" value="123321"/>
</map>
</property>
<property name="properties">
<props>
<prop key="driverName">com.mysql.jdbc.Driver</prop>
<prop key="url">mysql:jdbc://localhost:8080/......</prop>
</props>
</property>
5.Spring框架注入的方式
1.设值注入:所谓的设值注入就是在容器配置的值通过setXX()方法注入(赋值)给类中的属性或者变量。 如下图:
2.构造注入:所谓的构造注入就是将容器中配置的值通过类的构造器注入(赋值)给类中的属性或者变量。 如下图: