日期:2014-05-17  浏览次数:20725 次

SSH架构下创建单例的问题
怎样的SSH2架构下创建单例,这个单例我要实现下拉框内容(存放与数据库)的缓存,包括spring如何配置。

------解决方案--------------------
不知道你说的单例是否是单例模式


反正我就告诉你spring的bean默认就是单例bean
------解决方案--------------------
单例的代码哈

再给你段代码你看看

Java code

public class A {
private static A a;
private A(){
}

public A getInstance(){
   if(a == null)
        a = new A();
   return a;
}
}

------解决方案--------------------
你在xml配置文件配置了BaseDAO吗?
XML code
    <bean id="dao" class="com.XXX.BaseDAO"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default">
        <property name="sessionFactory">
            <ref bean="sessionFactory"></ref>
        </property>
    </bean>

------解决方案--------------------
public class BooksContext {
//这个地方是关键
private static ApplicationContext content=new ClassPathXmlApplicationContext("applicationContext.xml");;

public static void getnewsTypeById(HttpServletRequest request) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh点mm分"); 
Date date=new Date(); 
String d=sdf.format(date); 
System.out.println(d);
//这个地方是关键
BookService b = (BookService) content.getBean(
"BookServiceImpl");
List<BooksLordType> list = b.getAllBooksLordType();
request.setAttribute("BooksLordTypeList", list);
}