31.Spring-开发流程.md
[toc]
1.简单开发流程
1.1引用类库
基本类库:
1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
</beans>
1.3创建范例的对象类:
package per.liyue.spring.development_process;
/**
* Created by liyue on 2016/11/2.
*/
public class Car
{
private String carId;
private String carMfr;
public String getCarId()
{
return carId;
}
public void setCarId(String carId)
{
this.carId = carId;
}
public String getCarMfr()
{
return carMfr;
}
public void setCarMfr(String carMfr)
{
this.carMfr = carMfr;
}
}
1.4配置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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!--IOC容器,将所有需要spring创建的对象都配置到这里
id:别名
class:类对象位置
-->
<bean id="car" class="per.liyue.spring.development_process.Car"></bean>
</beans>
1.5在需要的地方使用IOC容器创建对象:
package per.liyue.spring.development_process;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by liyue on 2016/11/2.
*/
public class App
{
/*
没有框架的创建对象方式
*/
@Test
public void OldProcss()
{
Car myCar = new Car();
System.out.println("这是就方式创建的对象类:" + myCar);
}
/*
使用spring的创建方式:
*/
@Test
public void CreateProcess()
{
//得到IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/spring/development_process/applicationContext.xml");
//从IOC容器中获得对象
Car car = (Car) applicationContext.getBean("car");
System.out.println("IOC创建对象:" + car);
}
}
2.Bean创建的注意要点
2.1创建方式:
javabean创建可以分为两种:单例和多例,在配置文件中使用关键字scope控制
单例
- 对应的值为:singleton,这是默认值,默认每次对象都是单例创建的,适用于工具类,在程序中创建一次。
- 懒加载,使用关键字lazy-init控制 ,只对单例模式生效
多例
- 对应值为:prototype,在多例的模式下,以懒加载方式生成对象。
2.2初始化和销毁
可以用关键字设置初始化函数和销毁函数
- 初始化:init-method
- 销毁:destory-method
2.3实例
2.3.1xml文件配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!--IOC容器,将所有需要spring创建的对象都配置到这里
id:别名
class:类对象位置
scopr:singleton对应单例模式,prototype对应多例模式
lazy-init:延迟加载,对单例生效,默认为关闭
init-method:自定义的类的初始化函数
destroy-method:自定义的类释放函数
-->
<bean id="car" class="per.liyue.spring.development_process.Car" scope="singleton" lazy-init="true"></bean>
<bean id="carPrototype"
class="per.liyue.spring.development_process.CarPrototype"
scope="prototype"
init-method="InitFun"
destroy-method="DestoryFun"></bean>
</beans>
2.3.2对象类
package per.liyue.spring.development_process;
/**
* Created by liyue on 2016/11/2.
*/
public class Car
{
public Car()
{
System.out.println("创建Car对象");
}
private String carId;
private String carMfr;
public String getCarId()
{
return carId;
}
public void setCarId(String carId)
{
this.carId = carId;
}
public String getCarMfr()
{
return carMfr;
}
public void setCarMfr(String carMfr)
{
this.carMfr = carMfr;
}
}
package per.liyue.spring.development_process;
/**
* Created by liyue on 2016/11/2.
* 这个类和Car类没有区别,只是方便配置文件区分
*/
public class CarPrototype
{
public CarPrototype()
{
System.out.println("创建CarPrototype对象");
}
private String carId;
private String carName;
public String getCarId()
{
return carId;
}
public void setCarId(String carId)
{
this.carId = carId;
}
public String getCarName()
{
return carName;
}
public void setCarName(String carName)
{
this.carName = carName;
}
public void InitFun()
{
System.out.println("这是CarProrotype的初始化函数");
}
public void DestoryFun()
{
System.out.println("这是CarPrototype的释放函数");
}
}
2.3.3app
package per.liyue.spring.development_process;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by liyue on 2016/11/2.
*/
public class App
{
@Test
public void MainOut()
{
ConfigBeanPrint();
}
// /*
// 没有框架的创建对象方式
// */
// public void OldProcss()
// {
// Car myCar = new Car();
// System.out.println("这是就方式创建的对象类:" + myCar);
// }
//
// /*
// 使用spring的创建方式:
//
// */
// public void CreateProcess()
// {
// //得到IOC容器对象
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/spring/development_process/applicationContext.xml");
// //从IOC容器中获得对象
// Car car = (Car) applicationContext.getBean("car");
// System.out.println("spring IOC创建对象:" + car);
// }
public void ConfigBeanPrint()
{
//得到IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/spring/development_process/applicationContext.xml");
//单例对象
Car car1 = (Car) applicationContext.getBean("car");
Car car2 = (Car) applicationContext.getBean("car");
System.out.println("单例对象获取1:" + car1);
System.out.println("单例对象获取2:" + car2);
//多例对象
CarPrototype cp1 = (CarPrototype) applicationContext.getBean("carPrototype");
CarPrototype cp2 = (CarPrototype) applicationContext.getBean("carPrototype");
System.out.println("多例对象获取1:" + cp1);
System.out.println("多例对象获取2:" + cp2);
}
}
创建Car对象
单例对象获取1:per.liyue.spring.development_process.Car@7791a895
单例对象获取2:per.liyue.spring.development_process.Car@7791a895
创建CarPrototype对象
这是CarProrotype的初始化函数
创建CarPrototype对象
这是CarProrotype的初始化函数
多例对象获取1:per.liyue.spring.development_process.CarPrototype@6325a3ee
多例对象获取2:per.liyue.spring.development_process.CarPrototype@1d16f93d
3.IOC对象的创建方式
3.1分类
使用IOC创建对象,可以分为三类
- 无参的对象
- 有参的对象
- 通过工厂方法创建
- 工厂类的非静态创建
- 工厂类的静态创建
3.2要点
不同的创建方法的区别在于配置文件的配置项。
[toc]
3.3
3.3.1类对象
package per.liyue.springlearing.process;
/**
* Created by admin-1 on 2016/11/2.
*/
public class Car {
private String carId;
public String getCarId() {
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public String getCarMfr() {
return carMfr;
}
public void setCarMfr(String carMfr) {
this.carMfr = carMfr;
}
private String carMfr;
/*
无参的构造
*/
public Car()
{
System.out.println("构造了一个无参的类对象");
}
@Override
public String toString()
{
return "Car{" +
"carId='" + carId + '\'' +
", carMfr='" + carMfr + '\'' +
'}';
}
/*
有参的构造
*/
public Car(String carId, String carMfr) {
this.carId = carId;
this.carMfr = carMfr;
System.out.println("构造了一个有参数的类对象");
}
}
package per.liyue.springlearing.process;
/**
* Created by admin-1 on 2016/11/2.
* 此类是一个类工厂的例子
*/
public class ObjectFactory {
/*
工厂类的非静态方法
*/
public void getInstance()
{
System.out.println("调用工厂类的非静态方法");
return new Car("陕A0002", "BMW");
}
/*
工厂类的静态方法
*/
public static void getStaticInstance()
{
System.out.println("调用工厂类的静态方法");
return new Car("陕A003", "L");
}
}
3.3.2配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!--无参的构造:
-->
<bean id="carNoPara"
class="per.liyue.springlearing.process.Car"
lazy-init="default">
</bean>
<!--有参的构造:
-->
<!---->
<bean id="strName" class="java.lang.String">
<constructor-arg value="奔驰"></constructor-arg>
</bean>
<bean id="carPara"
class="per.liyue.springlearing.process.Car"
lazy-init="default">
<constructor-arg index="0" value="陕A0001" type="java.lang.String"></constructor-arg>
<constructor-arg index="1" ref="strName"></constructor-arg>
</bean>
<!--工厂类动态加载:
1.获取到工厂类
id:工厂类的id
class:工厂类的类引用位置
2.通过工厂类获取对象
id:对象的id
factory-bean:工厂类的id
factory-method:需要调用的动态创建方法
-->
<bean id="carFactory" class="per.liyue.springlearing.bean_create.ObjectFactory"></bean>
<bean id="carByFac" factory-bean="carFactory" factory-method="getInstance"></bean>
<!--工厂类静态加载:
id:工厂类的id
class:工厂类的类引用位置
factory-method:需要调用的静态创建方法
这里不需要配置工厂类
-->
<bean id="carByFacStatic" class="per.liyue.springlearing.bean_create.ObjectFactory" factory-method="getStaticInstance"></bean>
</beans>
3.3.3app
package per.liyue.springlearing.process;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by liyue on 2016/11/10.
*/
public class App
{
@Test
public void Main()
{
//获取IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/springlearing/bean_create/bean.xml");
//获取到对象
Car carFac = applicationContext.getBean("carByFac", Car.class);
System.out.println("工厂类动态创建对象:" + carFac);
Car carFacStatic = applicationContext.getBean("carByFacStatic", Car.class);
System.out.println("工厂类静态创建对象:" + carFacStatic);
}
}