Java 如何使一个类成为一个bean?

Java 如何使一个类成为一个bean?,java,spring,Java,Spring,我有一个MongoService班 public class MongoService { private final Mongo mongo; private final String database; private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class); public MongoService(@Nonnull final String host

我有一个MongoService班

public class MongoService {

    private final Mongo mongo;
    private final String database;
    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService(@Nonnull final String host, final int port, @Nonnull final String db) throws UnknownHostException {
        mongo = new Mongo(host, port);
        database = db;
    }

    public void putDocument(@Nonnull final DBObject document) {
        LOGGER.info("inserting document - " + document.toString());
        mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
    }

    public void putDocuments(@Nonnull final List<DBObject> documents) {
        for (final DBObject document : documents) {
            putDocument(document);
        }
    }

    @Nullable
    public <T extends DBObject> T getDocument(@Nonnull final T document) {
        final DBCollection collection = mongo.getDB(database).getCollection(getCollectionName(document));
        collection.setObjectClass(document.getClass());
        //noinspection unchecked
        return (T) collection.findOne(document);
    }

    @Nonnull
    public <T extends DBObject> List<T> getDocuments(@Nonnull final T document) {
        final List<DBObject> documents = new ArrayList<DBObject>();
        final DBCollection collection = mongo.getDB(database).getCollection(getCollectionName(document));
        collection.setObjectClass(document.getClass());
        final DBCursor dbCursor = collection.find();
        if (dbCursor != null) {
            documents.add(dbCursor.next());
        }
        //noinspection unchecked
        return (List<T>) documents;
    }
}  
公共类MongoService{
私人最终蒙戈蒙戈;
私有最终字符串数据库;
私有静态最终记录器Logger=LoggerFactory.getLogger(MongoService.class);
公共MongoService(@Nonnull final String host,final int port,@Nonnull final String db)抛出UnknownHostException{
mongo=新的mongo(主机、端口);
数据库=db;
}
public void putDocument(@Nonnull final DBObject document){
LOGGER.info(“插入文档-”+document.toString());
getDB(数据库).getCollection(getCollectionName(文档)).insert(文档,WriteConcern.SAFE);
}
公共文档(@Nonnull最终列表文档){
用于(最终DBObject文档:文档){
文件(文件);
}
}
@可空
公共T getDocument(@Nonnull final T document){
final DBCollection collection=mongo.getDB(数据库).getCollection(getCollectionName(文档));
setObjectClass(document.getClass());
//未检查
返回(T)收集。完成(文件);
}
@非空
公共列表getDocuments(@Nonnull final T document){
最终列表文档=新的ArrayList();
final DBCollection collection=mongo.getDB(数据库).getCollection(getCollectionName(文档));
collection.setObjectClass(document.getClass());
final DBCursor DBCursor=collection.find();
if(dbCursor!=null){
documents.add(dbCursor.next());
}
//未检查
归还(清单)文件;
}
}  
我想创建一个此类的单例bean,它从文件中读取主机、int、端口和数据库

问题

我该如何着手实现这一目标

  • 从文件中提供配置参数的最佳方式是什么
  • 我怎么做一顿豆

我是Spring新手,不知道如何实现这一点,

Spring bean默认是单例的,所以

@Service
public class MongoService

检查注入属性,它非常漂亮。

您可以按如下方式将值传递给构造函数

<bean id="mongoService" class="MongoService">
  <constructor-arg name="host" type="java.lang.String" value="localhost"/>
  <constructor-arg name="port" type="long" value="1234"/>
  <constructor-arg name="db" type="java.lang.String" value="dbname"/>
</bean>
确保jdbc.properties位于类路径中

jdbc.properties中的条目

host=localhost
port=1234
db=dbname

编辑:对于第二个问题,此
mongoService
bean默认为单例。

这是否可以是除
jdbc.properties
之外的任何其他属性文件并保持在类路径上?是的,您可以配置PropertyPlaceHolderConfigure从类路径加载任何属性文件。在上面的示例中,更改locations属性。仅供参考:
host=localhost
port=1234
db=dbname