标签: autowired

Spring 3.0.5不会从属性中评估@Value注释

尝试在Spring 3.0.5.RELEASE中将属性自动连接到bean ,我正在使用:

因此,用户名被设置为字面意思 "${username}",因此表达式不会被解析.我对此类的其他自动连接依赖项进行了设置,Spring不会抛出任何异常.我也尝试添加,@Autowired但它没有帮助.

如果我将属性解析为单独的bean然后使用@Autowired+ @Qualifier,它可以工作:

<bean id="username" class="java.lang.String">
    <constructor-arg value="${username}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

任何想法如何使用只是@Value?也许我需要包含一些我没有的Spring依赖项?谢谢

spring frameworks properties autowired

29
推荐指数
1
解决办法
4万
查看次数

可以春天@Autowired地图?

这是地图

@Autowired
private Map<String, ISendableConverter> converters;
Run Code Online (Sandbox Code Playgroud)

ISendableConverter

public interface ISendableConverter {

    ISendableMsg convert(BaseMessage baseMessage);

    String getType();
}
Run Code Online (Sandbox Code Playgroud)

有几个类实现 ISendableConverter

我想converters通过使用spring Autowried 将它们注入变量.

类的实例作为值,方法的结果@Autowried作为键.

像这个

@Component
public class SendableVoiceMsgConverter implements ISendableConverter {

    @Override
    public ISendableMsg convert(BaseMessage baseMessage) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getType() {
        return "VOICE";
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?如何?

java spring autowired

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

@Autowired vs @Required on setter

我很想知道这样的代码有什么区别:

class MyClass {
   @Autowired
   MyService myService;
}
Run Code Online (Sandbox Code Playgroud)

和这样的代码:

class MyClass {
   MyService myService;

   @Required
   public void setMyService(MyService val) {
       this.myService = val;
   }
}
Run Code Online (Sandbox Code Playgroud)

java resources spring autowired required

28
推荐指数
2
解决办法
3万
查看次数

是否可以使用注释连接Spring MVC Interceptor?

是否可以使用注释连接Spring MVC Interceptor,如果是这样,有人可以提供一个如何操作的示例吗?

通过注释通过注释,我指的是尽可能少地在XML配置中做.例如,我在http://www.vaannila.com/spring/spring-interceptors.html找到了这个配置文件;

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />
<bean id="loggerInterceptor" class="com.vaannila.interceptor.LoggerInterceptor" />
Run Code Online (Sandbox Code Playgroud)

你有多少配置可以逃脱?我想是一个@Autowired将删除在第2行显式声明bean的需要,但是也可以用注释去掉第1行吗?

spring annotations spring-mvc autowired interceptor

27
推荐指数
3
解决办法
4万
查看次数

Spring 3.2 Autowire泛型类型

所以我在Spring 3.2中有很多泛型,理想情况下我的架构看起来像这样.

class GenericDao<T>{}

class GenericService<T, T_DAO extends GenericDao<T>>
{
    // FAILS
    @Autowired
    T_DAO;
}

@Component
class Foo{}

@Repository
class FooDao extends GenericDao<Foo>{}

@Service
FooService extends GenericService<Foo, FooDao>{}
Run Code Online (Sandbox Code Playgroud)

遗憾的是,对于泛型的多个实现,自动装配会引发有关多个匹配bean定义的错误.我假设这是因为@Autowired类型擦除之前的进程.我找到或想出的每一个解决方案看起来都很难看,或者只是莫名其妙地拒绝工作.解决这个问题的最佳方法是什么?

java generics spring-mvc inversion-of-control autowired

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

Spring依赖注入@Autowired没有setter

我现在使用Spring几个月了,我认为使用@Autowired注释的依赖注入也需要为该字段注入一个setter.

所以,我这样使用它:

@Controller
public class MyController {

    @Autowired
    MyService injectedService;

    public void setMyService(MyService injectedService) {
        this.injectedService = injectedService;
    }

    ...
Run Code Online (Sandbox Code Playgroud)

}

但我今天试过这个:

@Controller
public class MyController {

    @Autowired
    MyService injectedService;

    ...
Run Code Online (Sandbox Code Playgroud)

}

哦惊喜,没有编译错误,启动时没有错误,应用程序运行完美...

所以我的问题是,使用@Autowired注释进行依赖注入所需的setter是什么?

我正在使用Spring 3.1.1.

java spring dependency-injection autowired

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

Spring循环参考示例

我在使用spring工作的一个项目中有一个循环引用,我无法修复,并在启动时失败并出现以下错误:

'org.springframework.security.authenticationManager': Requested bean is currently in creation: Is there an unresolvable circular reference?
Run Code Online (Sandbox Code Playgroud)

我尝试在示例项目中以较小的级别重新创建相同的问题(没有我的工作项目的所有细节).然而,我无法想出弹簧因错误而失败的合理情况.这就是我所拥有的:

public class ClassA {
    @Autowired
    ClassB classB;
}

public class ClassB {
    @Autowired
    ClassC classC;
}

@Component
public class ClassC {
    @Autowired
    ClassA classA;
}

@Configuration
public class Config {
    @Bean
    public ClassA classA() {
        return new ClassA();
    }

    @Bean
    public ClassB classB() {
        return new ClassB();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的项目中我有类似的情况,但是失败了,我也期待春天在我的示例项目中抱怨.但它工作正常!有人能给我一个如何用循环引用错误打破弹簧的简单例子吗?

编辑:我使用javax.inject.Provider修复了问题.两个项目中唯一的另一个区别是使用的注释是javax.inject.Inject和javax.annotation.ManagedBean代替@Autowired和@Component.

java spring dependency-injection circular-reference autowired

26
推荐指数
3
解决办法
5万
查看次数

按类型划分的弹簧布线比按名称布线要慢

在我的项目中,我正在尝试迁移所有用法

Foo foo = (Foo) beanFactory.getBean("name");
Run Code Online (Sandbox Code Playgroud)

Foo foo = beanFactory.getBean(Foo.class);
Run Code Online (Sandbox Code Playgroud)

好处是显而易见的:类型安全性,较少复杂的代码,较少的无用常量等.通常,这些线路位于静态遗留环境中,其中这种布线是唯一的选择.

这一切都很好,直到有一天用户开始抱怨来自Spring internals的缓慢.所以我启动了一个分析器来找到一个热点

org.springframework.beans.factory.support.AbstractBeanFactory::doGetBean(String, Class<T>, Object[], boolean)

这是一个昂贵的电话

Class.isAssignableFrom(anotherClass).

我已经快速创建了一个小的性能测试,以找出字符串名称和类型查找之间的速度差异是一个百日咳350次(我正在使用StaticApplicationContext这个测试FAIW)!

在调查这一点时,我发现SPR-6870的票数很高,但由于某种原因没有得到解决.这导致我试图解决这个问题,这个问题确实显着改善了这种情况,但仍然比String查找慢了~25倍!事实证明,这种尝试只解决了一半的问题:它缓存了bean的名称以保存在O(n)迭代上,但仍然需要调用isAssignableFrom来验证类型.

所描述的问题不仅与我的场景有关,而且也适用@Autowired于在循环内创建bean的情况下使用并且可能感觉很难的bean.

其中一个解决方案是覆盖其中一个bean工厂方法并缓存同一类型的is-this-bean类型的检查结果,但显然这应该在Spring中完成而不是在我自己的代码中完成.

是否还有其他人遇到类似问题并找到解决方案?

java performance spring dependency-injection autowired

24
推荐指数
1
解决办法
3048
查看次数

如何使用Spring手动自动装配bean?

我有一个bean B,我必须创建自己(使用new B()),并有@Autowire@PostConstruct注释.

如何使Spring从我的bean处理这些注释A

相关问题:

java spring autowired

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

Springboot单元测试Autowired

我有以下课程:

ApplicationAndConfiguration类

package mypackage.service;

import mypackage.service.util.MyUtility;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ApplicationAndConfiguration {


    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ApplicationAndConfiguration.class, new String[]{});
    }

    @Bean(initMethod="init")
    public MyUtility birtUtil() {
        return new MyUtility();
    }
}
Run Code Online (Sandbox Code Playgroud)

MyRestController类

package mypackage.service.controllers;

import mypackage.service.util.MyUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @Autowired
    private MyUtility util;

    @RequestMapping("/getLibraryName")
    public String getMessageFromRest(@RequestParam String name) {

        return "name was " + name + "//" + …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing autowired spring-boot

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