Tag: dependency injection

弹簧动态注塑,工厂般的图案

从dependency injection的延续,延迟注射实践 。 我有Main课程: package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import java.util.List; import java.util.Scanner; @Component public class Main { @Autowired private StringValidator stringValidator; @Autowired private StringService stringService; @Autowired private ValidationService validationService; public void main() { scanKeyboardCreateLists(); stringValidator.validate(); final List validatedList = stringValidator.getValidatedList(); for (String currentValid : validatedList) { System.out.println(currentValid); } } […]

有没有人和Guice一起使用ServiceLoader?

我一直希望通过我们的应用程序+构建系统在更大范围内尝试这一点,但更高的优先级继续将其推向次要位置。 这似乎是加载Guice模块的一种很好的方法,并避免了关于“硬编码配置”的常见抱怨。 单个配置属性很少自行更改,但您几乎总是会有一组配置文件,通常用于不同的环境(调试,生产等)。 ServiceLoader允许您提取定义为给定类型的服务的所有实现的列表。 将它与Guice放在一起,你得到: import java.util.ServiceLoader; import com.google.inject.AbstractModule; import com.google.inject.Module; public class ModuleLoader extends AbstractModule { private final Class type; public ModuleLoader(Class type) { this.type = type; } public static ModuleLoader of(Class type) { return new ModuleLoader(type); } @Override protected void configure() { ServiceLoader modules = ServiceLoader.load(type); for (Module module : modules) { install(module); […]

静态方法是DI反模式吗?

我是一名Java开发人员,他开始掌握dependency injection的全部function,我突然意识到没有办法注入静态方法。 所以它让我思考: 静态方法DI反模式? 更重要的是:如果我接受dependency injection,这是否意味着我需要停止编写静态方法? 我问,因为没有办法在unit testing中模拟它们并注入模拟静态,这对我来说是一个巨大的转折点。 编辑 :我知道“包装”和注入现有静态方法的常用方法是这样的: public class Foo { public static void bar() { … } } public interface FooWrapper { public void bar(); } public class FooWrapperImpl implements FooWrapper { public void bar() { return Foo.bar(); } } …但我不是在问如何注入一个现有的静态方法…我问我是否应该完全停止写它们,如果我所有的代码(从这一点开始)都要接受DI的概念。 此外,我看到了许多与此类似的相关问题,但无法找到提出相同问题的完全匹配。 如果你发现它确实是另一个问题的愚蠢,请指出我,我将自己关闭这个问题(请不要只是关闭它!)。

spring @Autowire property vs setter

anotate @Autowired与某个属性有什么区别,或者在setter中执行它有什么区别? 据我所知他们都有相同的结果,但有没有理由使用一个而不是另一个? 更新(更简洁) 这有什么不同吗? package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; @Autowired public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); } } 和这个 package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println(“Inside TextEditor constructor.” ); } […]

泽西岛2.26和春季4.3.10但没有HK2

是否可以配置Jersey 2.26.x,它完全取决于注射用Spring而不是HK2? 我知道Jersey现在与Spring兼容,但它是否也可以完全摆脱HK2?

配置Guice模块提供的对象

我有一个提供JDBI DBI实例的Module ,如下所示: @Provides @Singleton DBI dbi(DataSource dataSource) { return new DBI(dataSource); } 在另一个模块中,我想在该DBI实例上调用一些初始化方法(配置对特定数据类型的支持)。 放入JDBI模块本身并不合适,因为它的应用程序特定于使用JDBI的任何应用程序都不通用。 我有没有勾选做那种“额外”的配置? 我尝试使用bindListener方法,但似乎没有调用以这种方式提供的对象。

为单个maven项目创建两个工件(war)

我有一个Java Web项目,我们在两个不同客户的服务器上部署,99%的代码是相同的,现在我有两个ApplicationBuilders,它是包含每个客户的自定义的类。 每当我想部署新版本时,我必须手动注释一行,构建(使用maven),取消注释该行,注释另一行并再次构建。 public class ApplicationBuilderFactory { private static final IApplicationBuilder app; static { // app = new Customer1ApplicationBuilder() app = new Customer2ApplicationBuilder(); } } public static IApplicationBuilder get() { return app; } } 我想避免这一切,最好的事情可能就是创造两场不同的战争。 这样做的好方法是什么? 我不使用(也不喜欢)dependency injection框架,并且为一个类添加一个似乎有点过分,但我可能会考虑它。

多个实例中的Dagger 2单身人士

我刚刚测试了Dagger 2,我对单例注释有一些奇怪的行为。 我创建了一些测试代码来显示我的问题。 我的模块: @Module public class App { @Provides @Singleton ThingA provideThingA(){ return new ConcreteThingA(); } } 在singleton中我想要的东西的界面: public interface ThingA { void showMyId(); } 执行: public class ConcreteThingA implements ThingA { @Override public void showMyId() { System.out.println(this); } } 执行Dagger的代码: public void doStuff() { ThingA thingA=DaggerThingAComponent.create().provideThingA(); ThingA thingB=DaggerThingAComponent.create().provideThingA(); System.out.println(“Hello”); } 这是一个屏幕截图,显示当我要求它两次时,我没有得到相同的实例。 我错过了什么基本的东西? ThingA只是一个愚蠢的名字,在我的实际应用程序中,我希望在我的服务上有这种单例行为。

当我使用autowire和组件注释时,不会注入或创建我的spring bean

我正在尝试理解DI,但是当我使用autowire和组件注释时,我的spring bean没有被注入或创建。 相反,我得到一个nullpointerexception,因为我的bean是null。 我应该手动创建它,在那种情况下? 当几个bean相互依赖时,您在哪里指定创建bean的顺序? 这是我的代码: App.java package se.springtest; import se.springtest.Person; import org.springframework.core.io.ClassPathResource; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; public class App { public static void main( String[] args ) { BeanFactory factory = new XmlBeanFactory( new ClassPathResource(“application-context.xml”)); Person p = (Person) factory.getBean(“person”); p.show(); } } 应用程序的context.xml Person.java package se.springtest; public interface Person { public void show(); […]

Guice在注入的构造函数参数上忽略@Nullable

我正在使用Guice(v 3.0)并且有一个值被注入到构造函数中。 这个值可以为null,所以我在构造函数中使用@Nullable(来自javax.annotations)注释了该参数。 public MyClass(Parameter1 p1, @Nullable Parameter2 p2) { } 但是,Guice在初始化课程时抱怨提供错误: parameter 2 of com.abc.MyClass.() is not @Nullable 我不明白为什么这不起作用,还有什么我需要做的吗?