Spring基础

Spring基础

Spring概述

Spring框架,可以解决对象创建以及对象之间依赖关系的一种框架。 且可以和其他框架一起使用;Spring与Struts, Spring与hibernate (起到整合(粘合)作用的一个框架)

  • Spring提供了一站式解决方案:
  1. Spring Core spring的核心功能: IOC容器, 解决对象创建及依赖关系
  2. Spring Web Spring对web模块的支持。
    • 可以与struts整合,让struts的action创建交给spring
    • spring mvc模式
  3. Spring DAO Spring 对jdbc操作的支持 【JdbcTemplate模板工具类】
  4. Spring ORM spring对orm的支持:
    • 既可以与hibernate整合,【session】
    • 也可以使用spring的对hibernate操作的封装
  5. Spring AOP 切面编程
  6. SpringEE spring 对javaEE其他模块的支持

开发步骤

spring各个版本中:

在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】 如2.5版本;

在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】 (如果要用依赖包,需要单独下载!)

  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文件,在项目中可以用户库管理!

  1. 核心配置文件: applicationContext.xml
    Spring配置文件:applicationContext.xml / bean.xml

约束参考: spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html

<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. API
public class App {

	// 1. 通过工厂类得到IOC容器创建的对象
	@Test
	public void testIOC() throws Exception {
		// 创建对象
		// User user = new User();
		
		// 现在,把对象的创建交给spring的IOC容器
		Resource resource = new ClassPathResource("cn/csx/a_hello/applicationContext.xml");
		// 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + applicationContext.xml
		BeanFactory factory = new XmlBeanFactory(resource);
		// 得到容器创建的对象
		User user = (User) factory.getBean("user");
		
		System.out.println(user.getId());
		
	}
	
	//2. (方便)直接得到IOC容器对象 
	@Test
	public void testAc() throws Exception {
		// 得到IOC容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/csx/a_hello/applicationContext.xml");
		// 从容器中获取bean
		User user = (User) ac.getBean("user");
		
		System.out.println(user);
	}
}
  1. bean对象创建的细节
/**
	 * 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方法时候执行,(容器用实现类)】
	 */
	@Test
	public void testIOC() throws Exception {
		// 得到IOC容器对象  【用实现类,因为要调用销毁的方法】
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("cn/csx/a_hello/applicationContext.xml");
		System.out.println("-----容器创建-----");
		
		// 从容器中获取bean
		User user1 = (User) ac.getBean("user");
		User user2 = (User) ac.getBean("user");
		
		System.out.println(user1);
		System.out.println(user2);
		
		// 销毁容器对象 
		ac.destroy();
	}

实例HelloWorld程序

  • Dao层
// 一、Dao接口
public interface UserDao {
	void printInfo();
}

// 二、Dao实现类
public class UserDaoImpl implements UserDao {
	private String jdbcUrl;
	private String driverClass;
	private String username;
	private String password;

	public void printInfo() {
		System.out.println("jdbcUrl = " + jdbcUrl);
		System.out.println("driverClass = " + driverClass);
		System.out.println("username = " + username);
		System.out.println("password = " + password);
	}

	// getter与setter略
}
  • Service层:
public class UserServiceImpl {
	private UserDao userDao;

	public UserDao getUserDao() {
		System.out.println("UserServiceImpl.getUserDao()");
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		System.out.println("UserServiceImpl.setUserDao()");
		this.userDao = userDao;
	}
}
  • Spring的配置文件
<?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:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<!-- 配置Dao并设置他的属性 -->
	<bean id="userDao" class="cn.csx.b_springhelloworld.UserDaoImpl">
		<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>

	<!-- 配置Service并设置他的属性 -->
	<bean id="userService" class="cn.csx.b_springhelloworld.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>

</beans>
  • 测试代码(主程序)
public class HelloWorld {
	@Test
	public void testBeanFactory() {
		Resource resource = new ClassPathResource(
"cn/csx/b_springhelloworld/applicationContext.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);

		UserServiceImpl userServiceImpl = (UserServiceImpl) beanFactory.getBean(
"userService");
		System.out.println(userServiceImpl); // 可以得到UserServiceImpl
		System.out.println(userServiceImpl.getUserDao()); // 可以得到被注入的UserDao
	
		userServiceImpl.getUserDao().printInfo(); // 可以显示Dao中被注入的信息
		System.out.println("-- end --");
	}
}

以上是使用Spring的ObjectFactory,还可以使用他的子类ApplicationContext获取Bean,后者有更多的功能,在实际应用时都是使用后者:

@Test
public void testApplicationContext() {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"cn/itcast/b_springhelloworld/applicationContext.xml");
	
	UserServiceImpl userServiceImpl = (UserServiceImpl) applicationContext.getBean(
"userService");
	System.out.println(userServiceImpl); // 可以得到UserServiceImpl
	System.out.println(userServiceImpl.getUserDao()); // 可以得到被注入的UserDao
	
	userServiceImpl.getUserDao().printInfo(); // 可以显示Dao中被注入的信息
	System.out.println("-- end --");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值