写在前面:spring的两大特点:IOC与aop。IOC(Inverse of Control):控制反转,也可以称为依赖倒置。降低耦合。AOP:即面向切面编程。 从Spring的角度看,AOP最大的用途就在于提供了事务管理的能力。事务管理就是一个关注点,你的正事就是去访问数据库,而你不想管事务(太烦),所以,Spring在你访问数据库之前,自动帮你开启事务,当你访问数据库结束之后,自动帮你提交/回滚事务!

1、spring创建对象的模式是单例模式。简言之调用ac.getBean("mybean");,创建的是同一个对象。

2、创建自己的第一个spring案例:

  1、写一个接口:HelloBean

 public interface HelloBean {
public abstract void sayHelloo();
}

  2、随意写两个类,继承HelloBean接口,ZNhello与ENhello

public class ENhello implements HelloBean{
@Override
public void sayHelloo() {
// TODO Auto-generated method stub
System.out.println("测试学习spring,接口方式。");
}
public class ZNhello implements HelloBean{
@Override
public void sayHelloo() {
// TODO Auto-generated method stub
System.out.println("测试学习spring,接口方式。 interface");
}
}

  3、写一个类,调用接口方法:usebean

public class UseBean {
private HelloBean w;
public void show(){
System.out.println("显示hello的内容:");
w.sayHelloo();
}
//set后面的单词英语spring配置文件中的property name="hello" 中的name值一致。
public void sethello(HelloBean hello){
this.w = hello;
}
}

  4、配置spring项目的配置文件:applicationContext.xml

<!-- 创建bean,有指定的id , 对应的class -->
<bean id="usetest" class="demo2.UseBean">
<property name="hello" ref="rr"></property>
</bean>
<bean id="enhellobean" class="demo2.ENhello"> </bean>
<bean id="rr" class="demo2.ZNhello"> </bean>

  5、测试: Test.java

 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.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; public class Test { private static final String lujingname = "demo2/applicationContext.xml"; public static void main(String[] args) { // 第一种方式,applicationContext方法
//ApplicationContext ac = new ClassPathXmlApplicationContext("demo2/applicationContext.xml");
//UseBean userbean = (UseBean) ac.getBean("usetest");
//userbean.show(); // 第二种方式,BeanFactory是applicationContext的父类。
//Resource lujing_nei = new ClassPathResource(lujingname);
//BeanFactory ac = new XmlBeanFactory(lujing_nei);
//UseBean userbean = (UseBean) ac.getBean("usetest");
//userbean.show(); //第三种,spring文件直接放在硬盘中的其他位置。
ApplicationContext ac = new FileSystemXmlApplicationContext("I:/applicationContext.xml");
//Resource yy = new FileSystemResource("I:/applicationContext.xml");
//BeanFactory ac = new XmlBeanFactory(yy);
UseBean userbean = (UseBean) ac.getBean("usetest");
userbean.show(); } }

  6、执行测试,记得是执行运行JAVA测试,而不是执行运行Javaweb的测试……

Spring学习札记(一)的更多相关文章

  1. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  4. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  5. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  6. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  7. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  9. BITED-Windows8应用开发学习札记之二:Win8应用常用视图设计

    感觉自我表述能力有欠缺,技术也不够硬,所以之后的Windows8应用开发学习札记的文章就偏向于一些我认为较难的地方和重点了多有抱歉. 上节课是入门,这节课就已经开始进行视图设计了. Windows应用 ...

随机推荐

  1. python 写文本文件出现乱码

    最近工作中想完善一下监控日志,同事说客户突然说我们最近几天推送的数据只有几家,赶紧看预警,应推4700多家,实推3400多家,用户可能是看错了,但我记得当时项目验收上线时,这个来源的推送数据肯定是可以 ...

  2. python 装饰器问题及解决方式

    #统计函数运行时间 import time #装饰器 def timmer(func): def wrapper(*args,**kwargs): start_time=time.time() fun ...

  3. delphi提示“Undeclared identifier”TForm的缺少引用单元列表

    在interface uses  添加TForms;

  4. Python的类的下划线命名的区别

    首先,单下划线开头,这个常被用于模块中,在一个模块中以单下划线开头的变量和函数被默认当做内部函数,如果使用from  module  import  *导入时,这部分变量和函数不会被导入.注意,如果使 ...

  5. 安装weblogic

    附:wls1036_generic.jar 链接:https://pan.baidu.com/s/1W5g-SHeKL96yrOeDKJJoxw 提取码:vxft 注:以下安装步骤是一个前辈整理的,我 ...

  6. vue-quill-editor富文本编辑器,上传图片自定义为借口上传

    vue-quill-editor富文本编辑器,上传图片自定义为借口上传 博客地址:https://blog.csdn.net/lyj2018gyq/article/details/82585194

  7. C# 今天时间 今天结束时间

    var startTime = DateTime.UtcNow.ToString(timeFormat); ).AddSeconds(-).ToUniversalTime().ToString(tim ...

  8. Delphi 带星期几的日期格式化

    把日期按日期+星期几的格式输出 方法1:DatetoStr + DayOfWeek计算 ,这种办法灵活,但计算量大,不再祥叙. 方法2:FormatDateTime 具体代码如下://这里需要用For ...

  9. python之路-----MySql操作三

    mysql 概述 一.主要内容: 视图 create view name (select * from user where id>5); 触发器 函数 存储过程 索引 二.各模块详细说明 1. ...

  10. [Codeforces375E]Red and Black Tree

    Problem 给定一棵有边权的树.树上每个点是黑或白的.黑白点能两两交换. 求符合任意一个白点到最近黑点的距离小于等于x时,黑白点交换次数最少为多少. Solution 明显是一题树形DP.我们先跑 ...