Tag: autowired

Spring JUnit4手动/自动接线困境

我遇到了一个问题,这个问题只能解释为我对Spring的IoC容器设施和上下文设置缺乏了解,所以我会要求澄清一下。 仅供参考,我正在维护的应用程序具有以下堆栈技术: Java 1.6 spring2.5.6 RichFaces 3.3.1-GA UI Spring框架用于bean管理,Spring JDBC模块用于DAO支持 Maven用作构建管理器 JUnit 4.4现在作为测试引擎引入 我追溯(sic!)为应用程序编写JUnit测试,令我感到惊讶的是,我无法通过使用setter注入将bean注入测试类而不使用@Autowire表示法。 让我提供一个示例和附带的配置文件。 测试类TypeTest非常简单: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TypeTest { @Autowired private IType type; @Test public void testFindAllTypes() { List result; try { result = type.findAlltTypes(); assertNotNull(result); } catch (Exception e) { e.printStackTrace(); fail(“Exception caught with ” + e.getMessage()); } } } 其上下文在TestStackOverflowExample-context.xml定义: […]

无法自动assembly方法

我收到了这个错误 org.springframework.beans.factory.BeanCreationException: Could not autowire method: 这是我的spring的xml配置。 … 这是我的代码,我在Java类中自动assembly它 private InfoModel infoModel; @Autowired public void setInfoModel(InfoModel infoModel) { this.infoModel= infoModel; } 我错过了什么。 我怀疑我应该创建一个InfoModel Interface才能使其自动assembly? Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.model.InfoModel] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920) at […]

自动assemblySpring超类

为什么Spring在自动assembly期间会自动选择超类类型? 例如,如果我有 @Component public class Foo {} @Component public class Bar extends Foo {} 和某人自动assembly @Autowired private Foo foo; 为什么Spring总是选择超类型Foo ? 这不应该是一个“ 模糊 ”的映射(并导致Spring抛出错误)? 难道你不是技术上有两个 Foo候选人吗? (例如,当从Foo中删除@Component时,会自动选择 Bar …)

spring-nullpointerexception-无法在无注释类中访问自动连接的带注释的服务(或dao)

我有这个问题,我无法解决。 从我的@Controller ,我可以轻松访问我的自动assembly的@Service类并使用它没有问题。 但是当我从没有注释的单独类中执行此操作时,它会给我一个NullPointerException 。 我的控制器(工程) – @Controller public class UserController { @Autowired UserService userService;… 我的单独Java类(不工作) – public final class UsersManagementUtil { @Autowired UserService userService; 要么 @Autowired UserDao userDao; userService或userDao始终为null! 只是尝试其中任何一个工作。 我的组件扫描设置具有用于扫描的根级别包,因此应该没问题。 我的servlet上下文 – / .jsp 0 orion.core.models.Question orion.core.models.User orion.core.models.Space orion.core.models.UserSkill orion.core.models.Question orion.core.models.Rating ${hibernate.dialect} ${hibernate.show_sql} ${hibernate.hbm2ddl.auto} 任何线索?

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中的特定方法。

我不能使用注释来指示bean是主bean

我们知道在Spring中,有一个属性“primary”,表示如果有多个bean可以自动连接到属性,则bean是第一个候选者。 但是现在我的所有bean定义都是使用@Component / @Service等声明的,我找不到可用于声明bean的相应“primary”属性。 请告知我如何实现这一目标,谢谢。

如何在spring使用@Autowired

我的spring.xml如下 我有Test1类 package com.mkyong.service; import org.springframework.stereotype.Component; @Component(“test1”) public class Test1 { public int i=1; } 我有一个App类 package com.mkyong.common; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mkyong.service.Test1; public class App { @Autowired Test1 test1; public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext( “spring.xml”); App app=new App(); System.out.println(app.test1); } } 但我得到输出为null。我不能正确自动assembly它。我做错了什么?

无法在Junit测试中自动assemblyJpaRepository – Spring启动应用程序

大家好 我正在尝试在Spring启动应用程序中执行Junit测试,Junit应该测试一些CRUD操作,我正在使用Spring Repositories专门使用JpaRepository。 存储库calss: package au.com.bla.bla.bla.repository; import org.springframework.data.jpa.repository.JpaRepository; import au.com.bla.bla.bla.entity.Todo; public interface TodoRepository extends JpaRepository { } TodoController类 package au.com.bla.bla.bla.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import au.com.bla.bla.bla.entity.Todo; import au.com.bla.bla.bla.repository.TodoRepository; import java.util.List; import java.util.Map; @RestController @CrossOrigin public class TodoController { static final String TEXT = “text”; @Autowired […]

Spring @Autowired – 实例化新bean

需要一些Spring自动assembly和范围的帮助。 这是基本的app结构: 我有一个CustomHttpClient,注释为@Component,还从application.properties文件中提取一些与配置相关的属性(通过@Value注释)。 CustomHttpClient由我的应用程序中的多个服务使用。 每当我使用CustomHttpClient时,我通过以下方式自动assembly该实例: @Autowired private CustomHttpClient httpClient; 我使用拦截器来修改CustomHttpClient中的一些变量,如下所示: public class MyInterceptor extends HandlerInterceptorAdapter { @Autowired CustomHttpClient httpClient; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { httpClient.setSomeProperty(newValue); … 现在,这是问题所在。 如果我按照上面的描述设置了所有内容,那么每当我通过拦截器更改CustomHttpClient的任何设置时,只要VM正在运行,就会为所有其他客户端保留该新值。 因此,当我运行httpClient.setSomeProperty()时 – 该设置现在已永久保存。 即使我从另一个客户端连接到该应用程序。 基本上我需要的是两件事: 仍然能够通过拦截器覆盖CustomHttpClient的默认设置(请求拦截器,通过配置)。 确保为每个请求创建一个新的CustomHttpClient实例(在拦截器执行其魔法之后)。 我尝试将CustomHttpClient的范围更改为@Scope(“prototype”),但这样我就无法再使用拦截器更改CustomHttpClient的设置。

Spring STOMP从应用程序的任何位置发送消息

我正在构建一个使用Stomp通过websockets代理消息的应用程序。 我试图从服务器发送消息到客户端,而无需来自应用程序的任何地方的请求。 我在网上找到两个单独的选择,用于从应用程序的任何位置发送消息 第一个是在Websocket文档中找到的。 第20.4.5节: @Controller public class GreetingController { private SimpMessagingTemplate template; @Autowired public GreetingController(SimpMessagingTemplate template) { this.template = template; } @RequestMapping(value=”/greetings”, method=POST) public void greet(String greeting) { String text = “[” + getTimestamp() + “]:” + greeting; this.template.convertAndSend(“/topic/greetings”, text); } } 第二个是由一位Spring Blogger撰写的指南 : @Controller public class GreetingController { @Autowired private SimpMessagingTemplate template; […]