Tag: dependency injection

使用注释注入依赖项是否会消除dependency injection(外部配置)的主要好处?

我使用Spring,这是一个控制器: @Controller public class PersonController { @Resource(name=”PersonService”) private PersonService personService; @RequestMapping(value = “/Person”, method = RequestMethod.GET) public String getPersons(Model model) { // Retrieve all persons by delegating the call to PersonService List persons = personService.getAll(); // Attach persons to the Model model.addAttribute(“persons”, persons); //then return to view jsp } 这是一项服务: @Service(“personService”) @Transactional public class PersonService […]

Spring – 如何注入具体的接口实现?

我需要通过@Autowired注入服务类的具体实现。 服务界面: public interface PostService { … } 执行: @Service(“postServiceImpl”) public class PostServiceImpl implements PostService { … } 服务中的方法使用@Transactional注释 现在我想将postServiceImpl注入我的控制器 – 因为我需要使用实现中的一个方法,而不是在接口中: @Autowired @Qualifier(“postServiceImpl”) private PostServiceImpl postService; 我收到NoSuchBeanDefinitionException并带有以下消息: 没有为依赖项找到类型为[(…).PostServiceImpl]的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。 当我将控制器中的字段更改为: private PostService postService 它工作,但我不能使用PostServiceImpl中的特定方法。

如何让google guice注入一个自定义记录器,比如一个commons-logging或log4j logger

Google guice具有内置的记录器绑定function 。 但是,如果我想使用commons-logging或log4j记录器呢? 我是否可以注入一个由其创建的日志 LogFactory.getLog(CLASS.class) 但具有与内置绑定相同的行为: 绑定自动将记录器的名称设置为注入Logger的类的名称。 它甚至有意义吗? 或者喊我只是使用内置的java Logger? 或者只使用没有注射的公共记录?

记录Spring bean创建/dependency injection

我正在寻找一种方法来设置Log4j (或任何其他记录器),以便我可以在Spring创建bean或设置bean属性时在日志中看到。 例如。 像这样的东西: 1:00:00 Creating bean Foo (Foo@ef5c94) 1:00:01 Creating bean Bar (Bar@147a87e) 1:00:02 Setting bean Foo (Foo@ef5c94) to Bar (Bar@147a87e) (…) 这很容易吗? 我正在使用Spring 2.5.6 (没有选择:/)和Log4j (版本并不重要,我期待)。

ejb与客户端工件 – 运行时依赖?

我们公司在两个工件中创建了一个ejb。 impl工件包含实现,客户端工件包含所有接口。 这意味着impl工件对客户端工件具有编译依赖性。 现在,在运行时,客户端工件需要impl工件 – 否则容器无法注入所需的对象。 这意味着耳朵需要包含所有客户端工件的impl工件。 这是否意味着客户端工件应该对impl工件具有runtime依赖性? 或者是否应该避免这些“循环”依赖,即使一个方向是compile而另一个是runtime ?

播放2.0 / 2.1 for Java和dependency injection

我们有一个新的Play 2.0项目,我们计划在添加一些复杂的第三方集成代码时引入DI。 有一个适用于Play 2.0的Guice插件,但看起来它在2.1时已经过时了,我有一种预感2.1不再那么远了。 https://github.com/typesafehub/play-plugins/tree/master/guice Guice是Play 2.0 / 2.1的安全赌注,还是我们应该考虑其他选择?

交易注释错误

当我在我的Service类中添加“ @Transactional(readOnly=false) ”注释时,我收到以下错误 描述: bean“studentService”无法作为“ com.student.service.StudentServiceImpl ”注入,因为它是一个实现的JDK动态代理:com.student.service.StudentService 示例代码: @Service(“studentService”) @Transactional(readOnly=false) public class StudentServiceImpl implements StudentService { } public interface StudentService { } 行动: 考虑通过在@EnableAsync和/或@EnableCaching上设置proxyTargetClass=true来将bean注入其接口之一或强制使用基于CGLib的代理。 进程以退出代码1结束 是什么造成的?

Java中的资源注入和dependency injection(CDI)有什么区别?

我一直在学习Java EE,并发现Java EE提供了两种类型的注入机制 资源注入 dependency injection 请引导我理解资源注入和dependency injection之间的区别。

使用spring的条件bean

我正在尝试编写一个ValidatorFactory ,它将根据其类型为我提供validation器 public Validator getNewValidator(ValidatorType type){ switch: case a : new Validator1(); break; case b : new Validator2(); break; } 我想用spring xml bean定义来编写 我可以使用方法注入,但它只允许我创建一个对象,而方法可以 不接受任何争论。 我不想使用FactoryBean ..我只是想看看我们是否可以使用spring xml来做到这一点 bean的定义。

导入org.springframework无法解析

我正在尝试使用Spring DI创建一个简单的HelloWorld应用程序。 我创建了一个Java项目并导入了一些教程类,其中一个很简单: package helloworld; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; import java.io.FileInputStream; import java.util.Properties; public class HelloWorldSpring { public static void main(String[] args) throws Exception { // get the bean factory BeanFactory factory = getBeanFactory(); 但是我收到以下错误: The import org.springframework cannot be resolved 。 我正在使用Eclipse Spring Tool Suite 3.7.2,我认为通过使用这个Eclipse版本,当我点击“修复项目设置”时,它会为我添加这些依赖项。 我在这做错了什么? 即使在STS中,我是否需要手动添加这些依赖项? 如果是,那么正确的方法是什么?