承接上文

自定义对象行为


.net篇(环境为vs2012+Spring.Core.dll v1.31

  public class lifeCycle :
Spring.Objects.Factory.IInitializingObject,
System.IDisposable,
Spring.Objects.Factory.IObjectFactoryAware,
Spring.Objects.Factory.IObjectNameAware
{
public Spring.Objects.Factory.IObjectFactory OFactory { get; set; }
public String Name { get; set; }
public void initMethod(){}
public void destroyMethod() {}
public void AfterPropertiesSet(){
initMethod();
}
public void Dispose(){
destroyMethod();
}
public Spring.Objects.Factory.IObjectFactory ObjectFactory{
set { OFactory = value; }
}
public string ObjectName{
set { Name = value; }
}
}
public class CustomFactory :
Spring.Objects.Factory.IFactoryObject
{
public object GetObject(){
return "1";
}
public bool IsSingleton{
get { return false; }
}
public Type ObjectType{
get { return typeof(string); }
}
}
<object id="lifeCycle"
type="SpringBase.lifeCycle, SpringBase"
init-method="initMethod"
destroy-method="destroyMethod" />
<object id="customFac" type="SpringBase.CustomFactory, SpringBase"/>

java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4

package springdemo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
public class LifeCycle implements
InitializingBean,
DisposableBean,
BeanFactoryAware, BeanNameAware {
private BeanFactory oFactory;
private String name;
public void initMethod(){}
public void destroyMethod() {}
@Override
public void afterPropertiesSet() throws Exception {
initMethod();
}
@Override
public void destroy() throws Exception {
destroyMethod();
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
oFactory=beanFactory;
}
@Override
public void setBeanName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public BeanFactory getoFactory() {
return oFactory;
}
}
class CustomFactory implements FactoryBean<String>{
@Override
public String getObject() throws Exception {
return "1";
}
@Override
public Class<?> getObjectType() {
return String.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
    <bean id="lifeCycle" class="springdemo.LifeCycle"
init-method="initMethod"
destroy-method="destroyMethod" />
<bean id="customFac" class="springdemo.CustomFactory" />

javaCsharp的共同点

  1. IObjectFactoryAware|BeanFactoryAware以及IObjectNameAware|BeanNameAware
    实现的接口的属性或者方法会在容器初始化完成后spring自动调用赋值,只要更改方法体即可
  2. IInitializingObject|InitializingBean接口的AfterPropertiesSet方法
    在属性赋值完成后调用,也可以用init-method节点来和spring框架解耦
  3. IDisposable|DisposableBean接口的destroy方法在对象释放的时候调用,
    也可以用destroy-method节点来和spring框架解耦
  4. IFactoryObject|FactoryBean为创建一个工厂类,
    通过id调用Ioc容器的时候返回的是getObject方法的返回值不是工厂类,
    前一章节的获取对象的属性就是继承了这个接口,isSingleton返回这个对象是不是单例,
    ObjectType|getObjectType返回对象类型
  5. IObjectFactoryAware|BeanFactoryAware获取当前Ioc的容器
  6. IObjectNameAware|BeanNameAware获取对象在容器里面的名称

Ⅶ.spring的点点滴滴--自定义对象行为的更多相关文章

  1. Ⅷ.spring的点点滴滴--抽象对象和子对象

    承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...

  2. Ⅵ.spring的点点滴滴--自定义类型转换器

    承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...

  3. Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)

    承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...

  4. Spring Data JPA 自定义对象接收查询结果集

    Spring Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和 ...

  5. Ⅴ.spring的点点滴滴--引用其他对象或类型的成员

    承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...

  6. XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)

    承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...

  7. Ⅸ.spring的点点滴滴--IObjectFactory与IFactoryObject的杂谈

    承接上文 ObjectFactory与IFactoryObject的杂谈 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { pu ...

  8. Ⅱ.spring的点点滴滴--对象

    承接上文 对象的各种实例化 .net篇(环境为vs2012+Spring.Core.dll) 修改原来的PersonDao对象为 public class PersonDao : IPersonDao ...

  9. 第四章 Spring.Net 如何管理您的类___自定义对象行为

    Spring.Net 一直讲求 ” 配置应用程序“,有一种需求在实际开发中不太常用,却非常有用 -> 配置对象的行为,Spring.Net 也能够完美的实现.Spring.Net 通过几个专门的 ...

随机推荐

  1. android 颜色收集及部分gridview样式设置

    < ?xml version="1.0" encoding="utf-8" ?> < resources> < color nam ...

  2. A9.linux驱动

    --- ---- ---

  3. ActionBar官方教程(3)更改标题处的图片

    Using a logo instead of an icon By default, the system uses your application icon in the action bar, ...

  4. IPv6 tutorial 1 Get started now

    https://4sysops.com/archives/ipv6-part-1-get-started-now/ You’ve probably heard the news that the In ...

  5. centos6.5安装vbox

    cd /etc/yum.repos.d wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo 下载跟CENTO ...

  6. ORA-28001: the password has expired

    大早上正式库提示: Oracle提示错误消息ORA-28001: the password has expired 解决办法: 1.利用SYSDBA权限登陆: 2.查看账户信息:select user ...

  7. WordPress Backdoor未授权访问漏洞和信息泄露漏洞

    漏洞名称: WordPress Backdoor未授权访问漏洞和信息泄露漏洞 CNNVD编号: CNNVD-201312-497 发布时间: 2013-12-27 更新时间: 2013-12-27 危 ...

  8. 用JavaScript(js)对时间格式化

    Date.prototype.format =function(format)     {         var o = {         "M+" : (this.getMo ...

  9. Growling Gears

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11587 G Growling GearsThe Best A ...

  10. 录制屏幕(gif)便于在博客中嵌入软件licecap