目录

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要点

不同的创建方法的区别在于配置文件的配置项。


目录

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);
}
}

31.Spring-开发流程.md的更多相关文章

  1. Spring详细基本开发流程

    LOGO 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star! 搜索关注微信公众号 码出Offer 领取各种学习资料! 一.Spring概述 1.1 Web开发中的一些问 ...

  2. Spring MVC——项目的开发流程

    创建项目(IDEA下) 打开IDEA,我们开始创建一个简单的Spring MVC项目,流程如下: 这里要注意一下,我们是基于Maven开发项目,当然是要配置Maven环境的,如果大家之前从来没有配置过 ...

  3. ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结

    一:生产者代码编写: 1.配置pom.xml引入相关坐标 <dependencies> <!-- spring开发测试 --> <dependency> <g ...

  4. 学习spring2--跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用

    http://www.importnew.com/13246.html     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 ...

  5. Java程序员的日常——SpringMVC+Mybatis开发流程、推荐系统

    今天大部分时间都在写业务代码,然后算是从无到有的配置了下spring与mybatis的集成. SpringMVC+Mybatis Web开发流程 配置数据源 在applicationContext.x ...

  6. 使用Spring开发第一个HelloWorld应用

    http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...

  7. Thrift项目Server端开发流程

    Thrift项目Server端开发流程 首先,先了解工程中所有包的功能(见下图) 该图为用户中心项目的目录结构,以下依次介绍. 1.     src/main/java com.framework:该 ...

  8. 一 Struts2 开发流程

    SSH与SSM简介SSM:Spring+SpringMVC+MybatisSSH:Struts2+Hibernate+SpringStruts2:是侧重于控制层的框架Hibernate:是一个ORM( ...

  9. Flutter Plugin开发流程

    这篇文章主要介绍了Flutter Plugin开发流程,包括如何利用Android Studio开发以及发布等. 本文主要给大家介绍如何开发Flutter Plugin中Android的部分.有关Fl ...

随机推荐

  1. [置顶] 技术人血泪史:七种IT失误让你直接走人

    IT人士的真实故事:搞出大麻烦,旋即遭解雇 如今想找一份理想的IT工作并不容易,但丢掉一份工作却非常简单. 导致自己被炒鱿鱼的原因很多,无论是没能尽到保护雇主数字资产的义务.或者是滥用手中的权限以达到 ...

  2. zabbix 四张大表分区

    trends_uint.ibd history history_unit trends CREATE TABLE `trends` ( `itemid` bigint(20) unsigned NOT ...

  3. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  4. bzoj2818

    我们先穷举素数p然后令y>x 这样问题就是求这个gcd(x,y)=p  (1<=x<y=n)不难发现必须y=kp k∈N* 当y=p时,易知个数为φ(1)当y=2p 个数为φ(2), ...

  5. HDU 5501 背包问题

    需要按照B/C的值从大到小排序. #include<cstdio> #include<cstring> #include<iostream> #include< ...

  6. 【高精度】NCPC 2014 C catalansqure

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1789 题目大意: 求大卡特兰数..公式如下.输入n求Sn(n<=5000) 题目 ...

  7. java一切乱码的解释 以及源头【转】

    工作中经常遇到java编码问题,由于缺乏研究,总是无法给出确切的答案,这个周末在网上查了一些资料,在此做些汇总. 问题一:在java中读取文件时应该采用什么编码? Java读取文件的方式总体可以分为两 ...

  8. lightoj 1251 (Two_Sat)

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  9. openStack 云平台管理节点管理网口流量非常大 出现丢包严重 终端总是时常中断问题调试及当前测试较有效方案

    tuning for Data Transfer hosts connected at speeds of 1Gbps or higher <一.本次OpenStack系统调试简单过程简单记录& ...

  10. 最蛋疼的bug:读取图片缩略图(一定要在相冊查看下形成缓存)

    近期的一个连接服务端的应用.须要读取图片,一般供用户公布商品选择上传图片.初始的图片列表应该是缩略图.仅仅有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; imp ...