在Spring中实现一个单例

时间:2014-12-15 11:05:48

标签: java xml spring design-patterns

我正在尝试实现我基于Spring注入创建的数据库的Singelton / 这是我原来的课程:

package SingeltonDBVersion1;
public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {

        if (db != null) {
            return singalDb;
        }
        System.out.println("The database is now open");
        singalDb = new SingeltonDB(username, password);
        db.connect(username, password);
        System.out.println("The database was connected");
        return singalDb;
    }

    public void create(String tableName) throws Exception {
        db.create(tableName);
    }

    public User query(String tableName, int userID) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return null;
        }
        return (db.query(tableName, userID));
    }

    public void update(String tableName, User user) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return;
        }
        db.update(tableName, user);
    }

}

当我将它转换为弹簧时,我尝试了这种方式:

package SingeltonDBVersion2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        ApplicationContext db1 = new ClassPathXmlApplicationContext("spring.xml");
        db = (DBconnImpl) db1.getBean("MyDBconnImpl");

    }


    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {

        if (db != null) {
            return singalDb;
        }
        System.out.println("The database is now open");
        singalDb = new SingeltonDB(username, password);
        db.connect(username, password);
        System.out.println("The database was connected");
        return singalDb;
    }

    public void create(String tableName) throws Exception {
        db.create(tableName);
    }

    public User query(String tableName, int rowID) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return null;
        }
        return (db.query(tableName, rowID));
    }

    public void update(String tableName, User user) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return;
        }
        db.update(tableName, user);
    }

}

我是一个UserContoller,看起来像这样:

package SingeltonDBVersion2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserContorller {
    SingeltonDB db;

    public UserContorller(String user, String pass) throws Exception {
        ApplicationContext db1 = new ClassPathXmlApplicationContext("spring.xml");
        db = (SingeltonDB) db1.getBean("MySingeltonDB");
    }

    public void createTable(String table) throws Exception {
        db.create(table);

    }

    public void saveUser(String table, int id, String name, int age)
            throws Exception {

        db.update(table, new User(id, name, age));
    }

    public User getUser(String table, int id) throws Exception {

        return db.query(table, id);
    }

}

spring xml:

<?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-3.0.xsd">

    <bean id="MySingeltonDB" class="SingeltonDBVersion2.SingeltonDB"
        factory-method="getInstance">
        <constructor-arg value="MyAccount" />
        <constructor-arg value="123" />

    </bean>
        <bean id="MyDBconnImpl" class="SingeltonDBVersion2.DBconnImpl"
        scope="singleton">
    </bean>

</beans>

但是当我测试它时,我得到一个无限循环说:“数据库现在打开了”

为什么?

谢谢!

0 个答案:

没有答案