熟悉spring的请看下,并解释为什么

接口


public interface FooInterface {
public void printFoo();
public void dummyFoo();
}

实现:

public class FooImpl implements FooInterface {
private String s = "init";

public void printFoo() {
    s = "printFoo";
    System.out.println(s);
}

public void dummyFoo() {
    System.out.println(s);
}

}



配置:

<bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>test.FooInterface</value>
</property>
<property name="target">
<ref bean="fooTarget"/>
</property>
<property name="singleton">
<value>[color=red]false[/color]</value>
</property>

</bean>
<bean id="fooTarget" class="test.FooImpl" singleton="false"></bean>

main调用:
引用

ApplicationContext context = new ClassPathXmlApplicationContext("test/beans.xml");
FooInterface a = (FooInterface)context.getBean("foo");
a.printFoo();
FooInterface b = (FooInterface)context.getBean("foo");
b.dummyFoo();

也是刚看到AOP这块,试出来lz想要的结果,相互学习。
我用的环境是
spring2.5.6,配置单例有一点不同

经测试如下配置可以得到楼主想要的结果:
[code="xml"]


com.cq.spring.single.FooInterface

<!--

-->
<!--不用target,改为使用targetName-->

fooTarget

    <property name="interceptorNames" >
        <list>
            <value>logAdvisor</value>
        </list>
    </property>

    <property name="singleton">
        <value>false</value>
    </property>
</bean> 

[/code]

spring的Ioc底层实现java反射,解析xml技术,读取配置文件信息,使用反射创建类的对象

没怎么明白LZ想问什么,说清楚点可能回答者能回答得更好。

楼主,[quote]public class FooImpl implements FooInterface {

private String s = "init";

public void printFoo() {
    s = "printFoo";
    System.out.println(s);
}

public void dummyFoo() {
    System.out.println(s);
}

}[/quote]你看看,这里:
private String s = "init";

public void printFoo() {
    s = "printFoo";
    System.out.println(s);
}

public void dummyFoo() {
    System.out.println(s);
}

刚开始字符串s值是"init",但是,你在调用printFoo()方法后s的值为"printFoo",此后,s一直是"printFoo".跟本没反应出你要的效果。

的确很奇怪,百思不得其解

[color=red][/color]哥们:


ApplicationContext context = new ClassPathXmlApplicationContext("test/beans.xml");
FooInterface a = (FooInterface)context.getBean("[color=red]foo[/color]");
a.printFoo();
FooInterface b = (FooInterface)context.getBean("[color=red]foo[/color]");
b.dummyFoo();

想调用哪个对象,spring对象是单例的,在spring容器启动时就已经创建的

[quote]您没有仔细看我的意思,请您仔细看看题目和配置~~~谢谢~~ [/quote]
不好意思,我粗心了。这个bean [color=red]foo[/color]会不会是静态的呢?

补充一下:
ProxyFactoryBean源码中:
[code="java"]
public Object getObject() throws BeansException {
initializeAdvisorChain();
if (isSingleton()) {
return getSingletonInstance();
}
else {
if (this.targetName == null) { //这里可以看到多里模式和此属性有关联
logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +
"Enable prototype proxies by setting the 'targetName' property.");
}
return newPrototypeInstance();
}
}
[/code]
可以一路跟下去,找到此类中的:
[code="java"]
private TargetSource freshTargetSource() {
if (this.targetName == null) { //这里如果属性为空则直接返回
if (logger.isTraceEnabled()) {
logger.trace("Not refreshing target: Bean name not specified in 'interceptorNames'.");
}
return this.targetSource;
}
else {
if (this.beanFactory == null) {
throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
"- cannot resolve target with name '" + this.targetName + "'");
}
if (logger.isDebugEnabled()) {
logger.debug("Refreshing target with name '" + this.targetName + "'");
}
Object target = this.beanFactory.getBean(this.targetName);//这里会重新获取一次,所以如果bean是配置的scope="prototype"则应该是返回新的对象
return (target instanceof TargetSource ? (TargetSource) target : new SingletonTargetSource(target));
}
}
[/code]