spring快速入门

①   spring是什么?

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

hibernate是orm框架(对象和关系映射框架),处于持久层

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

在spring中有一个非常重要的概念,bean的概念很大

bean(是Java中的任何一种对象,Javabean/servie/action/数据源/dao,ioc(控制反转inverse of control),di(dependency injection)依赖注入 )

画一个层次框架图:

pojo(Java简单对象)

快速入门

开发一个spring项目

1、引入spring的开发包(最小配置spring.jar该包把常用的jar都包括了、还要一个写日志的包common-logging.jar)

2、创建spring的一个核心文件applicationContext.xml,[hibernate有核心hibernate.cfg.xml,struts核心文件struts-config.xml],该文件一般放在src目录下

xml中规范文件2.5以前用的是dtd,2.5以后用的是->xsd schmea文件,用来约束文件中的元素,子元素以及出现的顺序。

applicationContext.xml中引入xsd文件:可以从给出的案例中拷贝一份。

3、配置bean

4、在Test.java中使用

项目目录结构:

applicationContext.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: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元素的作用是,当我们的spring框架加载的时候,spring就会去看这里面有没有bean,如果有这个bean spring就会自动创建这个bean对象,并且将其装在到内存里面去 -->
<!--
类似于:Userservice userService = new UserService();
id号是和对象的变量名对应的,如果id号为litao,则这句话的含义为
UserService litao = new UserService();
property为属性值,name为注入这个属性,等价于
userService.setName("小明");set方法必须写否则注入不进去 -->
<bean id="userService" class="com.service.UserService">
<!-- 这里体现了容器的特点,配置bean并注入属性,体现出注入的概念 -->
<property name="name">
<value>小明</value>
</property>
</bean>
</beans>

UserService.java

package com.service;

public class UserService {

	public String name;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void sayHello(){
System.out.println("hello " + name);
} }

Test.java

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.UserService; public class Test { public static void main(String[] args){
//我们先使用传统方法,来调用UserService的sayHello方法
UserService userService = new UserService();
userService.setName("小明");
userService.sayHello(); //我现在来使用spring来完成上面的任务
//1.得到spring的applicationContext对象(容器对象)
//通过类路径加载文件,这个对象就对象applicationContext.xml文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
us.sayHello();
}
}

Spring框架学习之第1节的更多相关文章

  1. Spring框架学习之第2节

    传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...

  2. Spring框架学习之第9节

    aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...

  3. Spring框架学习之第8节

    <bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...

  4. Spring框架学习之第3节

    model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...

  5. Spring框架学习之第7节

    配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...

  6. Spring框架学习之第6节

    bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ①   实例化(当我们的程序加载 ...

  7. Spring框架学习之第5节

    request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...

  8. Spring框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...

  9. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

随机推荐

  1. Python MYSQL - tiny ETL tool - 文件操作和数据库操作

    import os import MySQLdb Con= MySQLdb.connect(host=',db='test') #链接数据库 cur=Con.cursor() os.chdir(&qu ...

  2. Careercup - Microsoft面试题 - 5684901156225024

    2014-05-10 23:45 题目链接 原题: Arrange the numbers in an array in alternating order. For example if the a ...

  3. hibernate--联合主键(了解+,掌握-)

    如果一个表有多个主键(= =一般比较少) 8.4. 组件作为联合标识符(Components as composite identifiers) 先定义一个类OrderLineId (实现接口,imp ...

  4. 设计模式之建造者模式(Builder)

    建造者模式原理:建造模式主要是用于产生对象的各个组成部分,而抽象工厂模式则用于产生一系列对象,建造者模式而且要求这些对象的组成部分有序. 代码如下: #include <iostream> ...

  5. Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏

    #include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...

  6. Coding4Fun.Phone.Controls的使用

    Coding4Fun.Phone.Controls的使用: windows phone的应用一直有一个特色,那就是方块(磁贴).之前的应用中,我一直都XXXX 来实现,原来其实一直有一个更加好的方法, ...

  7. nginx 杂记

    接触nginx一段时间,有些自己的心得,偶尔在网上会看到一些细小的知识点,总结于此 nginx是以多进程的方式来工作的.nginx在启动后,会有一个master进程和多个worker进程. maste ...

  8. 【面试题042】翻转单词顺序VS左旋转字符串

    [面试题042]翻转单词顺序VS左旋转字符串 题目一:     输入一个英文句子,反转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理.     例如输入字符串“I a ...

  9. (转)c语言随机数srandom( )

    转自:http://zhidao.baidu.com/question/334364810.html调用随机数函数 rand()() 的时候, 实际得到的这个随机数并不是绝对随机的,它是以一个初始值, ...

  10. 调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...