整理了一下之前学习Spring框架时候的一点笔记。如有错误欢迎指正,不喜勿喷。

   上一节学习了如何搭建SpringIOC的环境,下一步我们就来讨论一下如何利用ioc来管理对象和维护对象关系。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
</beans>

这个是applicationContext.xml的 dtd。(补充上一节)

我们知道有以下集几种方法来创建对象:

  • 采用new关键字创建对象
  • 静态工厂方法,例如:Calendar cal=Calendar.getInstance();
  • 对象(现有)工厂方法

    所以ioc中在aplicationContext.xml中也对应了三种创建对象的途径
<bean id="" class="">①
<bean id="" class="" factory-method="">②
<bean id="" factory-bean="" factory-method="">③

id可以自定义

Class需要自己在工程中创建

接下来写一个简单的demo



applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="c1" class="com.Spring.Demo.HelloWorld">
</beans>

HelloWorld.java :

package com.Spring.Demo;

public class HelloWorld {
public void show(){
System.out.println("欢迎Spring!");
} }

接下来写一个测试

TestHelloWorld.java :

package com.Spring.Demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestHelloWorld {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hw=(HelloWorld)context.getBean("c1");
hw.show();
} }

运行后控制台输出为:

欢迎Spring!

这是第一种创建对象的方式,下面我们继续看看怎么使用动态工厂创建对象。

<!--静态工厂创建对象配置文件-->
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <bean id="service" class="dynamicFactory.ServiceFactory" factory-method="getService"/>
</beans>
//需要创建的对象,实现的接口(接口化编程,降低耦合度)
package staticFactory; public interface SomeServices {
String doFirst();
void doSecond();
}
//实现类,实现上面的接口
package staticFactory; public class SomeServiceImp implements SomeServices{ @Override
public String doFirst() {
System.out.println("print first");
return null; } @Override
public void doSecond() {
System.out.println("print second"); } }
//静态工厂,产生上面类的实例
package staticFactory; public class ServiceFactory {
public static SomeServiceImp getService() {
return new SomeServiceImp();
}
}
//测试类(Junit)
public class test { @Test
public void Test01() {
String resource = "staticFactory/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
SomeServices service = (SomeServices)ac.getBean("service");
service.doFirst();
service.doSecond();
}
}

控制台输出:

print first
print second

上面的是静态工厂bean的创建方式,接下来我们来看普通工厂对象创建方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <bean id="serviceFactory" class="dynamicFactory.ServiceFactory"></bean>
<!--普通工厂bean,factory-bean填的是上面的serviceFactory-->
<bean id="service" factory-bean="serviceFactory" factory-method="getService"/> </beans>
//需要创建的对象,实现的接口(接口化编程,降低耦合度)
package dynamicFactory; public interface SomeServices {
String doFirst();
void doSecond();
}
//实现类,实现上面的接口
package dynamicFactory; public class SomeServiceImp implements SomeServices{ @Override
public String doFirst() {
System.out.println("print first");
return null; } @Override
public void doSecond() {
System.out.println("print second"); } }
//这个和第二种不同,这里的工厂类方法是非静态的
package dynamicFactory; public class ServiceFactory {
public SomeServiceImp getService() {
return new SomeServiceImp();
}
}
//测试类(Junit)
public class test { @Test
public void Test01() {
String resource = "dynamicFactory/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
SomeServices service = (SomeServices)ac.getBean("service");
service.doFirst();
service.doSecond();
}
}

控制台输出:

print first
print second

这样我们的三种创建方式就说完了。

未完待续

02 Spring框架 简单配置和三种bean的创建方式的更多相关文章

  1. Spring随笔 —— IOC配置的三种不同方式简介

    在spring framework中,IOC的配置是最基础的部分,常见的配置方式有基于xml文件和基于注解的配置方式.除了这两种配置方式之外,今天这里再介绍另一种配置方式,先用小demo重温下我们熟悉 ...

  2. 【转】Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  3. Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  4. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  5. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  6. 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程

    一.依赖注入DI 学习目标1)搞清楚构造参数依赖注入的过程及类2)搞清楚注解方式的属性依赖注入在哪里完成的.学习思路1)思考我们手写时是如何做的2)读 spring 源码对比看它的实现3)Spring ...

  7. Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...

  8. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  9. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

随机推荐

  1. centos7下安装mysql5.7和jdk 1.8

    安装mysql5.7 具体安装过程可参见官网:A Quick Guide to Using the MySQL Yum Repository 进入/usr/local/src文件夹. cd /usr/ ...

  2. 使用Charles对Https请求进行抓包

    昨天对某个APP做分析的时候发现其请求是Https的,抓包工具不能正常的显示请求的内容及返回的情况.通过搜索发现Charles是支持针对Https抓包的.具体的操作如下: 1.电脑端安装SSL证书 2 ...

  3. PyTorch在64位Windows下的Conda包(转载)

    PyTorch在64位Windows下的Conda包 昨天发了一篇PyTorch在64位Windows下的编译过程的文章,有朋友觉得能不能发个包,这样就不用折腾了.于是,这个包就诞生了.感谢@晴天14 ...

  4. 不再为无限级树结构烦恼,且看此篇s

    很久都没有写点什么出来分享了,最近在做多级树的时候,发现来来回回写过很多遍,于是封装成用户控件,以方便日后重复使用. 首先上效果: 我们看到以上2种效果,都是支持任意级的,这里源码中使用的是递归,以便 ...

  5. 学习lofter 让图片适应各个分辨率的方法

    只要图片的分辨率足够大,那么可以任意的width,不用担心失真 那么就可以根据屏幕的分辨率给图片相应的宽度 大分辨率浏览 小分辨率浏览 两个分辨率的图片地址是一样的,排除了换图的可能 大分辨率下的代码 ...

  6. jQuery 库 - 特性

    jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaScript 特效和动画 HTM ...

  7. ios -逆向-代码混淆

    该方法只能针对有.m.h的类进行混淆,静态库等只有.h文件的没法进行混淆 代码混淆,刚刚看到是不是有点懵逼,反正我是最近才接触到这么个东西,因为之前对于代码和APP,只需要实现功能就好了,根本没有考虑 ...

  8. Win MYSQL5.7.19压缩版安装

    最近需要在wins上安装MYSQL,发现最新的版本和之前的有点差距,再次记录一下 1.下载:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5. ...

  9. 如何停止和扭转UIView的动画

    本文转载至  http://codego.net/576089/ 我有它收缩时碰到切换按钮UIView的动画跳和它扩展恢复到原来的大小当再次接触到按钮.密封式前大灯一切都工作得很好.问题是,动画师注意 ...

  10. tsinsen A1333. 矩阵乘法(梁 盾)

    A1333. 矩阵乘法(梁 盾) 时间限制:2.0s   内存限制:256.0MB   总提交次数:515   AC次数:211   平均分:54.14   将本题分享到:        查看未格式化 ...