Java开发框架-spring01

  • Sping(是持久层框架)

    • 表示层:提供了与Struts等框架的整合
    • 业务逻辑层:管理事务、记录日志等
    • 持久层:整合Herbinate、jdbcTemplate等技术
  • Spring优点:

    • 非侵入式设计
    • 方便解耦、简化开发
    • 支持AOP<面向切面编程aspect oriented programming>(可以将事务、日志等代码集中处理,可以提高代码的复用性)
    • 支持声明式事务处理
    • 方便程序测试
    • 方便集合各种优秀框架
    • 降低Java EE API的使用(sping将API进行了封装)
  • Sping中libs目录中四个基础包:

    • sping-core-4.3.6.RELEASE.jar
      包含spring框架的核心工具类,spring其他组件都要用到这个包里的类
    • sping-beans-4.3.6.RELEASE.jar
      所有应用都要用到的jar包,包含访问配置文件、创建和管理Bean以及进行控制反转,或者依赖注入操作相关的类。
    • sping-context-4.3.6.RELEASE.jar
      提供了在基础IoC功能上的扩展服务,还提供了许多企业级服务的支持
    • sping-expression-4.3.6.RELEASE.jar
      定义了Spring的表达式语言
  • Spring的核心容器

    • BeanFactory
    • ApplicationContext是BeanFactory的子接口
    • 在Java项目中,会通过ClassPathXmlApplicationContext来实例化ApplicationContext容器。
    • ApplicationContext的实例化工作会交由Web服务器完成。通常会使用ContextLoaderListener来实现。
  • Spring体系结构:

    • Core Container(核心容器):
      Beans / Core / Context / SpEL(新增模块,是运行时查询和操作对象图的强大的表达式语言)
    • Data Access / Integration(数据访问 / 集成)
      Web
      WebSocket / Servlet / Web / Portlet
    • 其他模块:
      AOP / Aspects / Instrumentation / Messaging / Test
  • IOC(使用spring框架后,对象的实例不再由调用者来创建,而是由spring容器来创建;spring容器会负责控制程序之间的关系;控制权由应用代码转移到了spring容器,控制权发生了反转,这就是控制反转

  • DI(spring容器复制将被依赖对象赋值给调用者的成员变量,这相当于为调用者注入了它依赖的实例,这就是spring的依赖注入)

  • 一个实例:

    • 创建动态web项目example01
    • 切换java页面,在src目录下创建包 it.first.cn
    • 在build文件导入需要的工具.jar包
    • 创建类应用类欸EX01.java,测试类TestEX01.java,创建xml文件applicationContent.xml

下面代码:

//实现类EX01
package it.first.cn;

public class EX01 {
	private String number;
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	private String name;
	private String phone;
	@Override  //重写方法,不要忘了
	public String toString() {
		return "EX01 [number=" + number + ", name=" + name + ", phone=" + phone + "]";
	}

}

复习一下eclipse创建get/set方法:选中,右键Source,Generate get and set选择,然后创建成功

//配置文件
<?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 id="ex1" class="it.first.cn.EX01">
    <property name = "number" value = "1840110145"></property>
    <property name = "name" value = "某某某"></property>
    
    </bean>
    
    
    <bean id="ex2" class="it.first.cn.EX01">
    <property name = "number" value = "1840110145"></property>
    <property name = "name" value = "某某某"></property>
    
    </bean>
    
</beans>

//测试类
package it.first.cn;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestEX01 {
	
	public static void main(String args[]) {
		ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
		EX01 e1 = (EX01) application.getBean("ex1");
		EX01 e2 = (EX01) application.getBean("ex2");
		System.out.println(e1);
		System.out.println(e2);
	}

}

测试一下运行结果

EX01 [number=1840110145, name=某某某, phone=null]

EX01 [number=1840110145, name=某某某, phone=null]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值