书小宅之java-使用Spring框架必备的步骤和基本用法(一)

Spring框架的概念

主要发明者:Rod Johnson,推崇轮子理论:不重复发明轮子,复用代码。
Spring框架宗旨:让原有的技术使用起来更加方便。
Spring是一个轻量级的java一站式开源框架,轻量级指不依赖于容器也能运行。
Spring框架是一个以IoC(Inverse of Control 反转控制)和AOP(Aspect Oriented Programming 面向切面编程)为内核的容器框架。

下载spring插件

在这里插入图片描述
下载Spring插件
操作步骤:打开eclipse->Help->Install New Software->add->local->在电脑上找到上述下载的插件的位置->选中所有spring IDE->next
在这里插入图片描述
如若出现The operation cannot be completed. See the details.的错误,其解决办法就是换成比较稳定的Eclipse IDE for Java EE Developers,我重新下载4.5.1版本,再重新安装所需要的插件即可成功。

接下来一直按提示点击next->I accept the terms of the license agreements->finish

验证:在Window->preferences的左侧列表有如下的spring节点则spring安装成功:
在这里插入图片描述

下载spring framework框架

Spring框架的核心类库Core模块的jar包:spring-beans、spring-core、spring-context、spring-expression
支持日志输出的jar包:commons-logging 、 log4j

spring官网下载spring framework框架 :Spring官网
在这里插入图片描述
Spring历史版本下载地址
解压下载好的压缩包,libs目录下的jar包可以复制到所需项目中进行使用。

下载spring依赖的日志包commons-logging

commons-logging下载
下载解压commons-logging-1.2-bin.tar.gz

jar包的使用

file->new->java project新建一个java项目firstSpring,新建的简单java项目没有lib文件夹,在firstSpring根目录下新建一个lib文件夹,并且将上述spring-framework和commons-logging文件中所需的jar包复制到该lib目录下。此时lib目录下的jar包效果如下:
在这里插入图片描述
在lib文件夹上右击->build path->Configure Build Path
在这里插入图片描述
点击apply,可以看到lib下jar包的状态改变了。
在这里插入图片描述
新建配置文件xml。

Spring 框架 runtime

在这里插入图片描述
1、容器(Container):Spring当作一个一个大容器
2、新版本ApplicationContext(升级版)接口是BeanFactory(老版本)的子接口。
3、从Spring3开始Spring框架的功能被拆分成多个jar包;Spring2及之前的是一整个jar包。

  • test:spring提供测试功能
  • Core Container:核心容器,Spring启动的最基本的条件
    • Beans:Spring负责 创建 类对象并 管理 对象
    • Core:核心类
    • Context:上下文参数,获取外部资源或管理器注解等
    • SpEI:expression.jar
  • AOP:实现aop功能需要依赖
  • Aspects:切面AOP依赖的包
  • Data Access/Integration:spring封装数据访问层等相关内容
    • JDBC:Spring 对JDBC封装后的代码
    • ORM:封装了持久层框架的代码。例如Hibernate
    • transactions:对应spring-tx.jar,声明式事务使用
  • WEB:需要spring完成web相关功能是需要。例如,由tomcat配置文件时需要有spring-web包

Spring几大核心功能

  • IoC/DI
    1、控制反转/依赖注入(inversion of Control)
    2、spring帮助完成程序员主动new实例化对象的工作
    3、控制指控制类的对象
    4、反转指交给spring负责
    5、IoC最大的功能:解耦:接触了程序员和对象管理之间的耦合。

  • AOP:面向切片编程

  • 声明式事务

Spring 环境搭建

1、导入jar包:四个核心包和一个日志包(commons-logging)
2、在src下新建applicationContext.xml。文件名称和路径自定义,applicationContext.xml配置的信息最终存储到applicationContext容器中。
3、spring配置文件是基于schema,相当于是DTD的升级版,比DTD具备更好的扩展性。schema文件扩展名.xsd,
DTD(文档类型定义):定义xml文档的合法构建模块。使用一系列合法元素定义文档结构。
每次引入一个xsd文件是一个namaspace(xmlns)

配置文件只需要引入基本的schema包;
通过创建对象;
默认配置文件被加载时创建对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd>
	<!--id表示获取到的对象标识
		class说明创建的对象属于哪个类-->
	<bean id="peo" class="com.example.People">
</beans> 

编写测试方法:
getBean(“标签id值”,返回值类型);如果没有第二个参数,默认是Object。
getBeanDefinitionNames():spring容器中目前所管理的所有对象。

public class Test{
	public static void main(String[] args){
		//People pro = new People();
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people =ac.getBean("peo",People.class);
		System.out.println(people);
		
		String[] names=ac.getBeanDefinitionNames();
		for(String string:nams){
			System.out.println(string);
		}
	}
}

结果:
People[id=0,name=null]
peo

spring创建对象的三种方式

  • 构造方法
    • 无参构造:默认情况
    • 有参构造:需要明确配置
    • 1、需要在类中提供有参构造方法
    • 2、在applicationContext.xml中设置条件调用哪个构造方法创建对象
    • 3、如果设定的条件匹配多个构造方法则执行最后一个构造方法
    • index:参数索引,从0开始
    • name:参数名
    • type:类型(区分基本数据类型和封装类,如int和Integer)
<bean id="peo" class="com.example.People">
	<!--ref引用另一个bean     value基于数据类型或string等-->
	<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
	<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
</bean>
public People(String name,int id){
	super();
	this.id=is;
	this.name=name;
	System.out.println("执行有参构造函数1");
}
public People(int id,String name){
	super();
	this.id=is;
	this.name=name;
	System.out.println("执行有参构造函数2");
}

不使用spring框架(工厂创建对象及实例工厂测试代码):

public class PeopleFactory{
	public People createPeople(String witch){
		switch(witch){
			case "A"
				return new A();
			case "B"
				return new A();
			default:
				return null;
		}
	}
}
PeopleFactory factory = new PeopleFactory();
People people = factory.newInstance();
  • 实例工厂
    • 工厂设计模式:帮助创建类对象,一个工厂可以生产多个对象。
    • 实例工厂:需要先创建工厂,才能生产对象

必须要有一个实例工厂

public class PeopleFactory{
	public People newInstance(){
		return new People(1,"测试");
	}
}

在applicationContext.xml中配置工厂对象和需要创建的对象

<bean id="factory" class="com.example.PeopleFactory"></bean>
<bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean>
  • 静态工厂
  • 不需要创建工厂,快速创建对象

编写一个静态工厂(在方法上添加static)

public class PeopleFactory{
	public static People newInstance(){
		return new People(1,"测试");
	}
}

在applicationContext.xml中配置工厂对象和需要创建的对象

<bean id="peo2" class="com.example.PeopleFactory" factory-method="newInstance"></bean>

给Bean的属性赋值(注入)

  • 通过构造方法设值
  • 设置注入
    如果属性是基本数据类型或String:String name;
<bean id="factory" class="com.example.PeopleFactory">
	<property name="id" value="456"></property>
	<property name="name" value="张三"></property>
</bean>

<bean id="factory" class="com.example.PeopleFactory">
	<property name="id">
		<value>456</value>
	</property>
	<property name="name">
		<value>张三</value>
	</property>
</bean>

set方法:private Set <String> sets;

<bean id="factory" class="com.example.PeopleFactory">
	<property name="sets">
		<set>
			<value>1</value>
			<value>2</value>
			<value>3</value>
		</set>
	</property>
</bean>

List方法:private List <String> list;

<bean id="factory" class="com.example.PeopleFactory">
	<property name="list">
		<list>
			<value>1</value>
			<value>2</value>
			<value>3</value>
		</list>
	</property>
</bean>

如果list只有一个值

<bean id="factory" class="com.example.PeopleFactory">
	<property name="list" value="1,2,3">
	<!--只算一个数据-->
	</property>
</bean>

Map方法:private Map<String,String> map;

<bean id="factory" class="com.example.PeopleFactory">
	<property name="map">
		<map>
			<entry key="a" value="b"></entry>
			<entry key="c" value="d"></entry>
		</map>
	</property>
</bean>

如果是Properties类型

<bean id="factory" class="com.example.PeopleFactory">
	<property name="demo">
		<props>
			<prop key="key1">value1</prop>
			<prop key="key2">value2</prop>
		</props>
	</property>
</bean>

DI(Dependency Injection依赖注入)

1、DI同IoC:当一个类(A)需要依赖另一个类对象(B)时,把另一个对象赋值给,把B赋值给A的过程叫做依赖注入【另一个类的实例化和注入由Spring来完成】

<bean id="peo" class="com.example.People">
	<property name="desk" ref="deskTest"></property>
</bean>
<bean id="deskTest" class="com.example.Desk">
	<property name="id" value="1"></property>
	<property name="price" value="12.5"></property>
</bean>

使用Spring来简化MyBatis

1、导入mybatis所有jar包和spring基本包、spring-jdbc,pring-tx,pring-aop,spring整合myabtis的包:
在这里插入图片描述
1、编写spring配置文件
在applicationContext.xml中:

<!-- 数据源封装类,数据源:获取数据库连接,要导入JDBC的jar包-->
<!-- 通过dataSource可以连接上数据库对象,返回DeriverManagerDataSource类型-->
<bean id="dataSource" class="org.springframework.jdbc.datasourse.DeriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
	<property name="username" value="root"></property>
	<property name="password" value="123456"></property>
	<!--创建SqlSessionFactory对象-->
	<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--数据库连接信息来源于dataSourse-->
		<property name="dataSourse" ref="dataSourse"></property>
	</bean>
	<!--扫描相当于MyBatis.xml中mapper下的package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfiguer">
		<!--要扫描哪个包-->
		<property name="basePackage" value="com.bjsxt.mapper"></property>
		<!--和factory产生关系-->
		<property name="sqlSessionFactory" ref="factory"></property>
	</bean>
	<!--想要给一个属性赋值的前提是该属性所在的类杯spring管理-->
	<!--由spring管理service实现类-->
	<bean id="airportService" class="com.example.AirportServiceImpl">
		<property name="airportMapper" ref="airportMapper"></property>
	</bean>
</bean>

代替了MyaBtis.xml的如下功能:

<dataSourse type="POOLED">
	<property name="driver" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
	<property name="username" value="root"/>
	<property name="password" value="123456"/>
</dataSourse>

网上单独下载mybatis-spring-1.2.3的jar包;
3、正常编写 pigo
编写mapper包下时必须使用接口绑定方案或注解方案(必须有接口)
正常编写Service接口和Service实现类。需要在Service实现类中声明Mapper接口对象,并生成get/set方法
4、spring无法管理servlet
在Test.java中加载全局配置文件:

public class Test{
	public statuc void main(String[] args)throws IOException,SQLException{
		InputStream is = Resources.getResourceAsStream("myabtis");
		//使用工厂设计模式
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
		//生产SqlSession
		SqlSession session = factory.openSession();
		Connection conn = null;
		conn.prepareCall("");
		List<Flower> list =session.selectList("a.b.selAll");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值