当使用Dagger时,我发现当我在任何需要它的地方注入它时,我会得到一个单例的多个实例.我已经注释了类和提供方法@Singleton
.谁能想到为什么会这样?
编辑:
如果它有帮助,我在Dagger的GitHub(https://github.com/square/dagger/tree/master/examples/android-activity-graphs)中使用与我的应用程序相同的结构作为示例应用程序.我正在尝试在基本活动中使用Singleton,并@Provides
在自定义Application
类中使用提供的几个第三方类.是因为我在每个活动中加上原始对象图的模块吗?
(PS:我一般都是Dagger和DI的新手,所以如果你能提供解释以便我可以学习,我将不胜感激.谢谢.)
我试图在继承ActivityInstrumentationTestCase2的Android功能测试中使用Dagger.
设置代码如下所示:
@Override
protected void setUp() {
// TODO Auto-generated method stub
try {
super.setUp();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectGraph.create(new TestModule()).inject(this);
this.activity = super.getActivity();
}`
Run Code Online (Sandbox Code Playgroud)
通过调用触发的OnCreate方法super.getActivity()
不使用TestModule提供的类.但是如果我手动运行我的活动(在测试上下文之外),那么我的非测试模块会提供/注入所有相应的类.
Sonatype 网站上的说明很薄弱,有关重定位 poms 的 Maven 文档引用了基于本地文件系统的存储库文件访问(并建议移动文件)。我需要在新的 groupId 中部署多模块项目的新版本,然后在旧的 groupId 中发布该版本的重定向 pom。我已经准备好重定位 pom - 我是否只需将它们交换为主 pom.xml 文件,然后重新运行我的部署(当我这样做时,它会部署 jar 内容),或者是否有其他方法将这些 pom 工件推送到oss.sonatype.org?有人有这方面的最佳实践吗?
java-xpath-pom.xml
我使用下面的 pom.xml 文件作为 xml dom 资源,我想获取标签的值<version>
而不在我的代码中迭代,这就是我使用 xpath 的原因,但我猜我的 xpath express 不正确,这就是它不返回任何值的原因。任何帮助表示赞赏。
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("C:\\projects\\pom.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("project/version");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
Run Code Online (Sandbox Code Playgroud)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dc.app</groupId>
<artifactId>rum</artifactId>
<packaging>war</packaging>
<version>12.54-SNAPSHOT</version>
<name>rum</name>
<description>rum</description>
</project>
Run Code Online (Sandbox Code Playgroud)