`
tian602564031
  • 浏览: 22956 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring 入门

 
阅读更多
package com.founder.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.founder.service.UserService;

public class Test {
	
	public static void main(String[] arg){
		
		/**
		 * 传统的方法,调用UserService 的 sayHello()方法
		 */
		
		/*UserService userService = new UserService();
		userService.setName("Aloys");
		userService.sayHello();*/
		
		/**
		 * 接下来,我们使用Spring的框架
		 * 
		 * 1.引入Spring的开发包
		 * 2.添加到build path路径
		 * 3.创建applicationContext.xml
		 * 4.配置bean
		 * 5.得到spring容器对象
		 */
		ApplicationContext appContex =  new ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml"); //通过类路径来加载配置文件
		
		UserService userService = (UserService) appContex.getBean("userService");
		userService.sayHello();
	}
}

 Spring的快速入门案例:

1、Spring是什么?

 Struts是web 框架(jsp/action/actionform)

hibernate是orm框架,处于持久层

spring 是容器框架,用于配置bean,并维护bean之间的关系的框架

☞快速入门案例:

   开发一个spring项目

1.引入Spring 最小配置开发包(去官网下载spring-framework框架spring.jar和commons-logging.jar)

2.加入到build path

3.创建一个spring的核心文件applicationContext.xml

4.配置bean

<?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:aop="http://www.springframework.org/schema/aop"
		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.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
		<!-- 在容器中配置bean(service/Dao/domain/action/数据源) -->
		
		<bean id="userService" class="com.founder.service.UserService">
			<!-- spring 框架加载的时候,spring就会自动创建一个bean对象,并放入内存,管理起来. -->
			<!-- UserService userService =  new UserService(); 
				userService.setName("Aloys");
			-->
			<property name="name" value="Aloys" />
			<property name="byeService" ref="byeService" />
		</bean>
		
		<bean id="byeService" class="com.founder.service.ByeService">
			<property name="name" value="骑着上帝去环游"></property>
		</bean>

</beans>

 

☞ ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml") 我们Spring容器对象被创建. 同时Spring中配置的Bean 就会被创建(内存[HashMap或HashTable])

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics