java框架Spring学习--Spring开发流程
2017-08-29 15:04
435 查看
1) 源码, jar文件:spring-framework-3.2.5.RELEASE
commons-logging-1.1.3.jar 日志
spring-beans-3.2.5.RELEASE.jar bean节点
spring-context-3.2.5.RELEASE.jar spring上下文节点
spring-core-3.2.5.RELEASE.jar spring核心功能
spring-expression-3.2.5.RELEASE.jar spring表达式相关表
以上是必须引入的5个jar文件,在项目中可以用户库管理
2)核心配置文件: applicationContext.xml
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">
<!-- IOC容器的配置: 要创建的所有的对象都配置在这里
<bean id="user" class="cn.itcast.a_hello.User" init-method="init_user" destroy-method="destroy_user" scope="singleton" lazy-init="false"></bean>
-->
<bean id="user" class="com.bxh.a_hello.User"></bean>
</beans>
配置文件属性详解:
/**
* 1) 对象创建: 单例/多例
* scope="singleton", 默认值, 即 默认是单例 【service/dao/工具类】
* scope="prototype", 多例; 【Action对象】
*
* 2) 什么时候创建?
* scope="prototype" 在用到对象的时候,才创建对象。
* scope="singleton" 在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。
* 3)是否延迟创建
* lazy-init="false" 默认为false, 不延迟创建,即在启动时候就创建对象
* lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象
* (只对单例有效)
* 4) 创建对象之后,初始化/销毁
* init-method="init_user" 【对应对象的init_user方法,在对象创建爱之后执行 】
* destroy-method="destroy_user" 【在调用容器对象的destriy方法时候执行,(容器用实现类)】
*/API(接口)测试
package com.bxh.a_hello;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class App {
private ApplicationContext ac = new ClassPathXmlApplicationContext("com/bxh/a_hello/applicationContext.xml");
@Test
public void testIoC() throws Exception{
//创建对象
// User user=new User();
Resource resource=new ClassPathResource("com/bxh/a_hello/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
User user= (User) factory.getBean("user");
System.out.println(user.getId());
}
@Test
public void testIoc2() throws Exception{
User user1 = (User) ac.getBean("user");
User user2 = (User) ac.getBean("user");
System.out.println(user1.getId()+" "+user1.getName());
System.out.println(user2.getId()+" "+user2.getName());
}
}
补充实体User类代码
public class User {
private int id;
private String name;
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
commons-logging-1.1.3.jar 日志
spring-beans-3.2.5.RELEASE.jar bean节点
spring-context-3.2.5.RELEASE.jar spring上下文节点
spring-core-3.2.5.RELEASE.jar spring核心功能
spring-expression-3.2.5.RELEASE.jar spring表达式相关表
以上是必须引入的5个jar文件,在项目中可以用户库管理
2)核心配置文件: applicationContext.xml
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">
<!-- IOC容器的配置: 要创建的所有的对象都配置在这里
<bean id="user" class="cn.itcast.a_hello.User" init-method="init_user" destroy-method="destroy_user" scope="singleton" lazy-init="false"></bean>
-->
<bean id="user" class="com.bxh.a_hello.User"></bean>
</beans>
配置文件属性详解:
/**
* 1) 对象创建: 单例/多例
* scope="singleton", 默认值, 即 默认是单例 【service/dao/工具类】
* scope="prototype", 多例; 【Action对象】
*
* 2) 什么时候创建?
* scope="prototype" 在用到对象的时候,才创建对象。
* scope="singleton" 在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。
* 3)是否延迟创建
* lazy-init="false" 默认为false, 不延迟创建,即在启动时候就创建对象
* lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象
* (只对单例有效)
* 4) 创建对象之后,初始化/销毁
* init-method="init_user" 【对应对象的init_user方法,在对象创建爱之后执行 】
* destroy-method="destroy_user" 【在调用容器对象的destriy方法时候执行,(容器用实现类)】
*/API(接口)测试
package com.bxh.a_hello;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class App {
private ApplicationContext ac = new ClassPathXmlApplicationContext("com/bxh/a_hello/applicationContext.xml");
@Test
public void testIoC() throws Exception{
//创建对象
// User user=new User();
Resource resource=new ClassPathResource("com/bxh/a_hello/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
User user= (User) factory.getBean("user");
System.out.println(user.getId());
}
@Test
public void testIoc2() throws Exception{
User user1 = (User) ac.getBean("user");
User user2 = (User) ac.getBean("user");
System.out.println(user1.getId()+" "+user1.getName());
System.out.println(user2.getId()+" "+user2.getName());
}
}
补充实体User类代码
public class User {
private int id;
private String name;
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
相关文章推荐
- Java框架学习笔记——Spring开发环境搭建
- java框架Hibernate学习---hibernate开发流程
- JavaWeb开发基于Spring和mybatis框架(学习日志)
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- Java学习笔记38:通过Spring Bean 注入static变量,来设计一套适合测试,开发,生产环境的配置项
- 整合框架 javaweb开发平台ssmy_m(与代码生成) java struts2 mybatis spring maven jquery
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- 使用struts、hibernate、spring进行框架整合流程详细说明(完整版)附:开发规范
- java开发web工程有 servlet/ jsp/ spring mvc等技术框架,那其他语言呢?
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)
- java新手web开发学习笔记--基础知识和(struts,hibeinate,spring)ssh框架流程
- Java开发最优的开发框架与学习网站
- java web 前端开发框架和流程
- java开发SSM框架的搭建(SpringMVC+Spring+MyBatis)
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- 【Java】【SSH】Web 开发经典框架Spring+Struts+Hibernate官方网站下载地址
- Portal-Basic Java Web 应用开发框架:应用篇(十一) —— 整合 Spring
- JAVA 开发平台的技术和框架(四)一、JDBC Spring JDBC