Spring使用静态工厂方法,然后书上说返回的随机数每次都是一样的,怎么不一样啊~~~~/***********************************************************************************************/
package bean;import java.util.Random;public class StaticFactoryBean {
public static Integer createRandom() {
return new Integer(new Random().nextInt());
}
}
/***********************************************************************************************/
<?xml version="1.0" encoding="UTF-8"?>
<beans 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.0.xsd">
<bean id="random" class="bean.StaticFactoryBean"
factory-method="createRandom">
</bean>
</beans>
/***********************************************************************************************/
package bean;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringStart {
public static void main(String args[]) {
ApplicationContext beanFactory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println(beanFactory.getBean("random"));
}
}
为什么返回的不一样,是不是每次new了一个random,,,,,哪个能回答详细点~~
package bean;import java.util.Random;public class StaticFactoryBean {
public static Integer createRandom() {
return new Integer(new Random().nextInt());
}
}
/***********************************************************************************************/
<?xml version="1.0" encoding="UTF-8"?>
<beans 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.0.xsd">
<bean id="random" class="bean.StaticFactoryBean"
factory-method="createRandom">
</bean>
</beans>
/***********************************************************************************************/
package bean;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringStart {
public static void main(String args[]) {
ApplicationContext beanFactory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println(beanFactory.getBean("random"));
}
}
为什么返回的不一样,是不是每次new了一个random,,,,,哪个能回答详细点~~
解决方案 »
- 权限控制,要控制到具体的action方法,讨论下怎么做最好?
- struts2在ACTION中给checkboxlist赋值
- 请大家来看看这个IO异常
- 推荐几本好点的j2ee的书
- 高分求救,怎样提取字符串中的数据
- String str = session.getId();这样得到的是sessionid吗?
- 这样引用include指令为什么没效?
- itext 如何读出 PDF 每页的内容(找不着资料啊,读作者/标题的我会了)
- tomcat如何配置可以直接以http://localhost访问?
- 怎样把<bean:write name = "item" property = "groupName"/>打印出的值赋给一个变量
- Hibernate+Struts2疑问??
- 关于用ssh做jsp项目时线程的问题
System.out.println(beanFactory.getBean("random")); 这句前面加句
for(int i=0;i<10;i++)
你就会发现书上说的是对的了。