写在前面: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. linux批量配置ip

    获取使用的网卡接口 ip a 2.查看系统版本 cat /etc/redhat-release 3.执行配置脚本 wget  http://d.sshby.com/biaozhun.tar&& ...

  2. stm32WB55xx 外设资源

    1.FLASH(闪存) 2.Radio System(无线系统:兼容BLE5.0与IEEE802.15.4标准,由2.4GHz射频前端.BLE和IEEE802.15.4物理层控制器组成,无线低功耗协议 ...

  3. oracle 将一个表复制到另外一个表里 .

    复制一个表到另一个表.视图.临时表 博客分类: oracle Oracle数据结构软件测试SQL 创建一个表new_table和old_table表结构一样(没有old_table的记录) creat ...

  4. MPI编程——分块矩阵乘法(cannon算法)

    https://blog.csdn.net/a429367172/article/details/88933877

  5. AOP之配置文件实现

    看了http://www.cnblogs.com/xrq730/p/4919025.html这篇文章之后自己跟着做了一遍,有需要可以去看原文,比我写的更详细. AOP AOP(Aspect Orien ...

  6. vue中$refs的使用

    vue中$refs获取组件或元素: 获取的元素就相当于是一个原生获取的元素,可以进行操作 this.$refs.ele.style.color = 'red

  7. 如何用python将一个时间序列转化成有监督学习

    机器学习可以被用于时间序列预测. 在机器学习能使用之前,时间序列预测需要被重新转化成有监督学习.将一个序列组合成成对的输入输出序列. 在这篇教程中,你会发现如何通过使用机器学习算法将单变量和多变量的时 ...

  8. C#异步(下)

    上篇主要分析了async\await之前的一些异步模式,今天说异步的主要是指C#5的async\await异步.在此为了方便的表述,我们称async\await之前的异步为“旧异步”,async\aw ...

  9. javascript 模板

    今天想记录下对arttemplate模板的使用,哎,其实这玩意的兴起主要还是得从浏览器操作dom说起.如果修改浏览器的某一个dom节点就会引起文档流的重绘,然后这个重绘的耗时相当的大,是昂贵的开销.所 ...

  10. 二进制转base64

    一. 以fetch的获取数据 1. response(后台返回): const buffer = response.arrayBuffer(),将二级制转成arrayBuffer类型 2. buffe ...