我了解如果通过生产者方法实例化,那些不符合JSP-299的类可以用于注入。
我已经解释为如果我想使用带有参数的构造函数生成一个注入bean,我可以通过使用一个生成器方法来实现。
但是,当我这样做时,我在部署时遇到以下异常:
2015-11-11T21:35:12.099+0000|Grave: Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: CDI deployment failure:Exception List with 2 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001435 Normal scoped bean class org.....MongoConfiguration is not proxyable because it has no no-args constructor - Producer Method [MongoConfiguration] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ApplicationScoped public org.....PropertiesProducer.produceMongoConfiguration()].
这是制片人:
public class PropertiesProducer {
private static final String PROPERTIES_FILE = "mongo.properties";
private Properties properties = new Properties();
public static final String DATABASE_NAME = "database.name";
public static final String PORT = "database.port";
public static final String HOST = "database.host";
public static final String USERNAME = "database.username";
public static final String PASSWORD = "database.password";
@Produces
@ApplicationScoped
public MongoConfiguration produceMongoConfiguration(){
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE);
if (in == null) {
return new MongoConfiguration(properties);
}
try {
properties.load(in);
} catch (IOException e) {
throw new RuntimeException("Failed to load properties", e);
}
finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
// don't care
}
}
return new MongoConfiguration(properties);
}
}
以下是用法:
public class MongoDatastore {
@Inject
MongoConfiguration mongoConfiguration;
@Inject
MongoClient mongoClient;
Datastore datastore;
@PostConstruct
private void setupDatastore() {
Morphia morphia = new Morphia();
datastore = morphia.createDatastore(mongoClient, mongoConfiguration.getDatabaseName());
}
}
我错过了一些非常明显的东西吗?
答案 0 :(得分:0)
最简单的解决方案是将范围从@ApplicationScoped
更改为@Singleton
:
import javax.inject.Singleton;
@Produces
@Singleton
public MongoConfiguration produceMongoConfiguration(){
// ...
return new MongoConfiguration(properties);
}
为了澄清,您可以看到this SO answer。
BTW:正常范围@ApplicationScoped
通常比@Singleton
伪范围更受欢迎。为什么?例如。因为这样的bean的序列化+反序列化将完美地工作。但是在日常工作中,有时候我们会遇到不可提及的第3部分课程,我们无法改变它。我们有可能的解决方案:
@Singleton
伪范围。