标签: autowired

@Autowire奇怪的问题

自动装配时我有一种奇怪的行为

我有类似这样的代码,它的工作原理

@Controller
public class Class1 {
    @Autowired
    private Class2 object2;
    ...
}

@Service
@Transactional
public class Class2{
   ...
}
Run Code Online (Sandbox Code Playgroud)

问题是我需要Class2实现一个接口,所以我只改变了Class2,所以它现在就像:

@Controller
public class Class1 {
    @Autowired
    private Class2 object2;
    ...
}

@Service
@Transactional
public class Class2 implements IServiceReference<Class3, Long>{
   ...
}

public interface IServiceReference<T, PK extends Serializable> {
    public T reference(PK id);
}
Run Code Online (Sandbox Code Playgroud)

用这个代码我得到了一个org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type for Class2.似乎@Transitional注释与接口不兼容,因为如果我删除@Transitional注释或者mplements IServiceReference<Class3, Long>问题消失了并且注入了bean(尽管我需要在这个类中都有).如果我将注释@Transitional放在方法而不是类中,也会发生这种情况.

如果这有帮助,我使用Spring 3.0.2.

与事务方法的接口不兼容?可能是一个Spring bug?

java spring spring-mvc autowired

23
推荐指数
2
解决办法
9326
查看次数

BuildProperties Spring Boot 2.1.5 和 eclipse 的自动装配失败

我正在使用一些 REST API 创建一个非常简单的应用程序,并且在我尝试在我的健康检查 API 上使用 BuildProperties 之前,它目前可以正常工作。在启动我的应用程序时,我收到以下错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-17 09:54:29.210 ERROR 10796 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field buildProperties in com.controller.HealthCheck required a bean of type 'org.springframework.boot.info.BuildProperties' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'buildProperties' in 'ProjectInfoAutoConfiguration' not loaded …
Run Code Online (Sandbox Code Playgroud)

java autowired spring-boot

23
推荐指数
6
解决办法
3万
查看次数

@autowired在静态类中

这是一个带有Hibernate的Spring MVC项目.我正在尝试创建一个Logger类,负责将日志输入数据库.其他类只调用具有一些属性的正确方法,这个类应该做所有的魔术.本质上它应该是一个具有静态方法的类,但这会导致autoiering dao对象出现问题.

public class StatisticLogger {
    @Autowired
    static Dao dao;
    public static void AddLoginEvent(LogStatisticBean user){
        //TODO code it god damn it
    }
    public static void AddDocumentEvent(LogStatisticBean user, Document document, DocumentActionFlags actionPerformed){
        //TODO code it god damn it
    }
    public static void addErrorLog(Exception e, String page,  HashMap<String, Object> parameters){
        ExceptionLogBean elb=new ExceptionLogBean();
        elb.setStuntDescription(e);
        elb.setSourcePage(page);
        elb.setParameters(parameters);
        if(dao!=null){ //BUT DAO IS NULL
            dao.saveOrUpdateEntity(elb);
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么做对了?我该怎么做才能使dao对象为null?我知道我可以将它作为方法参数传递,但这不是很好.我猜测autowired无法在静态对象上工作,因为它们是为早期创建的,所以尚未创建自动维护机制.

spring hibernate spring-mvc autowired static-class

21
推荐指数
2
解决办法
4万
查看次数

什么是@Repository和@Autowired用于.(弹簧)

我正在学习java 3个月,有时我无法理解某些东西的使用目的.

一个主题是依赖注入和spring bean我想出了finally =)

现在我混淆了两个注释@Autowired和@Repository.首先Autowiring是什么意思?然后我为什么要使用它们,使用它们和不使用它们之间有什么区别?

今天我也尝试在spring mvc项目中使用hibernate,我不得不搜索大约15个(找不到类错误的原因)jar文件,因为项目中使用的其他jar文件的依赖项.这是不是必须这样?这使得初学者学习java非常困难

谢谢...

java spring annotations repository autowired

20
推荐指数
2
解决办法
5万
查看次数

自动装配时,Spring集成测试速度很慢

我正在尝试加速我们环境中的集成测试.我们所有的课程都是自动装配的.在我们的applicationContext.xml文件中,我们定义了以下内容:

<context:annotation-config/>
<context:component-scan base-package="com.mycompany.framework"/>
<context:component-scan base-package="com.mycompany.service"/>
...additional directories
Run Code Online (Sandbox Code Playgroud)

我注意到Spring正在扫描上面指出的所有目录,然后迭代每个bean并缓存每个bean的属性.(我从春天开始查看DEBUG消息)

因此,以下测试大约需要14秒才能运行:

public class MyTest extends BaseSpringTest {
  @Test
  def void myTest(){
    println "test"
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法延迟加载配置?我尝试添加default-lazy-init="true"但是没有用.

理想情况下,只实例化测试所需的bean.

提前致谢.

更新:我之前应该说过,我不想为每个测试都有一个上下文文件.我也不认为只有测试的一个上下文文件才有效.(此测试上下文文件最终会包含所有内容)

java spring unit-testing autowired

19
推荐指数
2
解决办法
8587
查看次数

是否可以在构造函数上使用@Resource?

我想知道是否可以@Resource在构造函数上使用注释.

我的用例是我想连接一个名为的最后一个字段bar.

public class Foo implements FooBar {

    private final Bar bar;

    @javax.annotation.Resource(name="myname")
    public Foo(Bar bar) {
        this.bar = bar;
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一条消息,指出@Resource此位置不允许这样做.有没有其他方法可以连接最后一个字段?

java spring autowired spring-annotations

18
推荐指数
3
解决办法
1万
查看次数

Spring预计至少有一个bean可以作为此依赖项的autowire候选者

我在这个Autowire上遇到了麻烦:

@Controller
public class ChiusuraController {

    @Autowired
    private ChiusuraProvider chiusuraProvider;
}
Run Code Online (Sandbox Code Playgroud)

用这个bean:

@Service @Transactional
public class ChiusuraProvider extends ThreadProvider {


    public void run() {}
}
Run Code Online (Sandbox Code Playgroud)

延伸

public abstract class ThreadProvider extends Thread implements InitializingBean, Runnable, DisposableBean {
...
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chiusuraController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cinebot.service.ChiusuraProvider com.cinebot.web.controller.ChiusuraController.chiusuraProvider; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cinebot.service.ChiusuraProvider] found for dependency: expected at least 1 …
Run Code Online (Sandbox Code Playgroud)

spring dependency-injection autowired

18
推荐指数
2
解决办法
11万
查看次数

Mockito @InjectMocks如何运作?

这是我的问题:

我有几个Web服务类来测试所有从通用服务继承他们的方法.我没有为每个编写单元测试,而是认为我可以通过功能区域(即三组测试方法,每组依赖于不同的底层DAO方法调用)来打破测试套件.

我打算做的是:

@Mock StateDAO mockedStateDao;
@Mock CountyDAO mockedCountyDao;
@Mock VisitorDAO mockedVisitorDao;
Run Code Online (Sandbox Code Playgroud)

然后打电话:

@InjectMocks CountyServiceImpl<County> countyService = new CountyServiceImpl<County>();
@InjectMocks StateServiceImpl<State> stateService = new StateServiceImpl<State>();
@InjectMocks VisitorServiceImpl<Visitor> visitorService = new VisitorServiceImpl<Visitor>();
Run Code Online (Sandbox Code Playgroud)

我怎样才能确定每个mockedDAO都会注入正确的服务?是否更容易自动装配所有三个(而不是使用@InjectMocks)?

我正在使用Spring,Hibernate和Mockito ......

spring dependency-injection mockito autowired

18
推荐指数
2
解决办法
5万
查看次数

如何使用注释在Spring中按名称自动装配?

我有几个定义相同类的bean:

  @Bean
  public FieldDescriptor fullSpotField() {
     FieldDescriptor ans = new FieldDescriptor("full_spot", String.class);
     return ans;
  }

  @Bean
  public FieldDescriptor annotationIdField() {
     FieldDescriptor ans = new FieldDescriptor("annotationID", Integer.class);
     return ans;
  }
Run Code Online (Sandbox Code Playgroud)

因此,当我自动给他们

   @Autowired
   public FieldDescriptor fullSpotField;

   @Autowired
   public FieldDescriptor annotationIdField;
Run Code Online (Sandbox Code Playgroud)

我得到一个例外

NoUniqueBeanDefinitionException: No qualifying bean of type [...FieldDescriptor] is defined: expected single matching bean but found ...
Run Code Online (Sandbox Code Playgroud)

如何在XML配置中按名称自动装配?

java spring autowired

18
推荐指数
1
解决办法
2万
查看次数

如何使用运行时"限定符"变量动态注入服务?

在给定运行时值的情况下,我找不到一种简单的方法来注入组件/服务.

我开始阅读@ Spring的文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers 但我找不到如何变量传递给@Qualifier注释的值.

假设我有一个具有这种界面的模型实体:

public interface Case {

    String getCountryCode();
    void setCountryCode(String countryCode);

}
Run Code Online (Sandbox Code Playgroud)

在我的客户端代码中,我会做类似的事情:

@Inject
DoService does;

(...)

Case myCase = new CaseImpl(); // ...or whatever
myCase.setCountryCode("uk");

does.whateverWith(myCase);
Run Code Online (Sandbox Code Playgroud)

......我的服务是:

@Service
public class DoService {

    @Inject
    // FIXME what kind of #$@& symbol can I use here?
    // Seems like SpEL is sadly invalid here :(
    @Qualifier("${caze.countryCode}")
    private CaseService caseService;

    public void whateverWith(Case caze) {
        caseService.modify(caze);
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望caseService是UKCaseService(参见下面的相关代码).

public interface CaseService {

    void modify(Case caze); …
Run Code Online (Sandbox Code Playgroud)

service spring dependency-injection autowired

18
推荐指数
1
解决办法
2万
查看次数