spring开发步骤
2018-03-12 20:01
148 查看
1.新建工程,构建package包结构
2.导入spring的核心包和依赖关系包(或根据提示进行导入)
导入log4j和junit测试环境3.编写spring上下文配置文件
配置spring上下文环境时需要注意xsd的引用,spring包含了很多功能模块,不同的功能模块需要使用不同的xsd;是spring的核心工厂引入的xsd是:<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">xmlns - xml- extensive markup language - ns naming space - URI
4.把需要有spring bean工厂实例化的类交给spring进行管理(在spring配置文件中,配置一个bean关联类)
<bean id="now" class="java.util.Date"></bean>
5.编写测试程序或在业务程序中进行spring实例化对象,spring实例化对象时,可以使用spring的核心IOC/DI
spring的IOC模型有两种工作模式:BeanFactory,ApplicationContextApplicationContext在初始化spring环境时,会根据配置文件的配置进行初始化对象,调用getBean时直接返回对象
BeanFactory工厂模式在初始化spring环境时,不会进行初始化对象,调用getBean方法时才执行初始化
--在高版本中不建议使用,声明为过时了
ApplicationContext模式在初始化spring环境
String configLocationString = "config/application-context.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(configLocationString);默认的ApplicationContext环境会根据配置文件进行初始化所有的对象;
默认的会根据配置文件的配置顺序,一次执行对象的初始化(实例化);
如果对象间存在依赖关系且使用构造方法进行注入,则会先初始化所依赖的对象;
补充说明:使用set方法进行注入,也可以在bean标签上配置depends-on 配置依赖关系,也会先初始化依赖对象
1).实例化spring上下文环境
方法1:通过类路径进行加载ClassPathXmlApplicationContextString configLocation = "config/application-context.xml";context = new ClassPathXmlApplicationContext(configLocation);方法2:通过系统文件路径进行加载String path = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-context.xml";
context = new FileSystemXmlApplicationContext(path);spring加载环境时可以指定配置文件:配置可以使0~N个,当不指定名称是会查找默认的配置文件方法1:通过类路径进行加载ClassPathXmlApplicationContext --使用多个参数
String configLocation = "config/application-context.xml";
String configLocation2 = "config/application-common.xml";
//context = new ClassPathXmlApplicationContext(configLocation,configLocation);方法2:通过系统文件路径进行加载 --使用多个参数
String path = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-context.xml"; String path2 = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-common.xml"; context = new FileSystemXmlApplicationContext(path,path2);方法3:通过类路径进行加载ClassPathXmlApplicationContext --使用数组
String configs[] = new String[]{"config/application-context.xml" ,"config/application-common.xml"}; context = new ClassPathXmlApplicationContext(configs);方法4:通过系统文件路径进行加载 --使用数组
String path = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-context.xml"; String path2 = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-common.xml"; configs = new String[]{path,path2}; context = new FileSystemXmlApplicationContext(configs);
BeanFactory工厂模式在初始化spring环境
String path = "F:\\NotFoundTeam\\springdemo\\src\\config\\application-context.xml"; beanFactory = new XmlBeanFactory(new FileSystemResource(path));
2).通过spring上下文context环境得到实例化对象
Date now = (Date)context.getBean("now");spring获取实例对象的常用方法:
方法1:通过id名称进行获取
Course course = (Course)context.getBean("course");方法2:通过类型获取对象
对象1:
Student student = context.getBean(Student.class);对象2:
Student student2 = context.getBean(com.neuudu.springdemo.bean.Student.class);
3).使用实例对象进行业务操作
logger.info(now.toLocaleString()+" 学习了spring 框架!");
相关文章推荐
- 【Spring】开发步骤(四)
- Spring+MyBatis项目开发代码步骤
- Spring开发步骤
- spring使用工厂方法实例化对象的开发步骤:
- Spring-mvc开发步骤
- Spring学习之开发环境搭建的详细步骤
- struts+spring+hibernate开发步骤
- Struts + Spring +ibatis 整合开发步骤
- Struts + Spring +ibatis 整合开发步骤
- SSH框架之Spring的开发步骤、bean对象创建的细节
- Spring中AOP开发步骤
- J2EE框架(TomcatStrutsHibernateSpringAjax(dojo))开发步骤:
- Struts + Spring +ibatis 整合开发步骤
- J2EE框架(TomcatStrutsHibernateSpringAjax(dojo))开发步骤:
- Struts + Spring +ibatis 整合开发步骤
- Spring的开发步骤(7条)
- Myeclipse——Spring 从入门到精通一 Spring简单开发步骤
- Spring+Hibernate+Strust 整合开发步骤
- Hibernate+Spring整合开发步骤
- Spring开发步骤