标签: spring-ioc

Spring依赖项问题 - 找不到匹配的编辑器或转换策略

我有一个Web应用程序在部署时失败.我收到以下错误:

Aug 8, 2014 7:00:21 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#2' while setting bean property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'CustomLoginSuccessHandler' while setting bean property 'authenticationSuccessHandler'; nested exception is …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-ioc

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

为什么Spring框架不允许autowire原始类型?

根据文档,已经提到过

它是设计的

我想了解这种设计背后的可能思路.

java spring dependency-injection spring-ioc

6
推荐指数
1
解决办法
3959
查看次数

spring boot @Autowired来自另一个模块的bean

我的问题是如何将一个包添加到我的组件列表中以扫描@ComponentScan(basePackages = {"io.swagger","com.company.project",就像在这里添加它一样}),但是这个包在另一个模块中在我的项目中,

这是我的项目的结构:

springbootProject(maven项目)/

  module1(mavenProject, com.company.module1)
       pom1.xml

  module2(mavenProject, com.company.module2)
       pom2.xml
Run Code Online (Sandbox Code Playgroud)

的pom.xml

在模块2中我有我的主(@SpringbootAplication)我想要@Autowired myRepository女巫在模块1中

那么我该如何添加路径呢

spring-ioc spring-boot

6
推荐指数
1
解决办法
3099
查看次数

Spring @Autowired行为在测试中与组件不同

@Autowired在编写测试时,规则/行为是否不同?似乎通过测试,您可以自动装配到具体类型,但如果您在其中尝试相同的事情@Component,它将失败.这是一个人为的例子,但这是我遇到的事情,我只是想更好地理解.

受控示例代码:

public interface Gizmo {

  void whirr();
}

@Configuration
public class GizmoConfiguration {

  @Bean
  @Profile("no-dependencies")
  public Gizmo fooGizmoBean() {
    return new FooGizmo();
  }

  @Bean
  @Profile("!no-dependencies")
  public Gizmo barGizmoBean() {
    return new BarGizmo();
  }

  public class FooGizmo implements Gizmo {
    @Override
    public void whirr() {
    }
  }

  public class BarGizmo implements Gizmo {
    @Override
    public void whirr() {
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

运行良好的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(Application.Profiles.NO_DEPENDENCIES)
public class TestClass {

  @Autowired
  private GizmoConfiguration.FooGizmo gizmo;

  @Test
  public void test() …
Run Code Online (Sandbox Code Playgroud)

spring spring-test spring-ioc

6
推荐指数
1
解决办法
600
查看次数

具有动态键和值的 Spring bean 映射

现在我在 spring bean 映射中拥有应用程序相关的数据,并将该映射传递给其他类作为参考。

地图定义如下

<bean id="sampleMap" class="java.util.HashMap">
    <constructor-arg index="0" type="java.util.Map">
        <map key-type="java.lang.Integer" value-type="java.lang.Float">
            <entry key="1" value="5"/>
            <entry key="2" value="10"/>
            <entry key="3" value="15"/>             
        </map>
    </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)

该映射在其他 bean 中被称为

<bean id="config" class="com.example.Config" abstract="false">      
    <property name="sampleMap" ref="sampleMap"/>
    .
    .
</bean>
Run Code Online (Sandbox Code Playgroud)

我只想从数据库表中检索映射值并将其注入到其他类中。我怎样才能在春天做到这一点。基本上该表包含与应用程序相关的数据。map 的键和值将是 int,Configuration。

加载配置数据的最佳位置是什么?我们可以使用 spring beans map 来做,还是有其他好的方法来从数据库加载配置并在应用程序的其他地方(如服务、委托和 DAO)引用该配置?

帮助将不胜感激

谢谢

java spring hashmap spring-ioc

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

使用Spring注释注入父类依赖项的正确方法

我有以下代码 -

Dao.java

@Component
public class Dao extends NamedParameterJdbcDaoSupport {

}
Run Code Online (Sandbox Code Playgroud)

dbContext.xml

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.jdbc.url}" />
            <property name="username" value="${db.user}" />
            <property name="password" value="${db.password}" />
        </bean>
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中

<context:component-scan base-package="com.kshitiz" />
Run Code Online (Sandbox Code Playgroud)

问题是NamedParameterJdbcDaoSupport需要数据源才能工作.既然这是超类的属性而不是我自己的类,我能想到的唯一方法就是 -

@Component
public class Dao extends NamedParameterJdbcDaoSupport {
    @Autowired
    public void setDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常难看.我可以指定我想要自动装配我的bean的所有属性吗?就像是 -

@Component(default-autowire="byType")
public class Dao extends NamedParameterJdbcDaoSupport {

}
Run Code Online (Sandbox Code Playgroud)

这在春天有可能吗?或者,注入超类依赖项的最优雅方法是什么?

编辑: 我已经知道这可以使用我目前正在使用的XML来完成.我想知道仅使用注释可以做到的最好.

java spring inversion-of-control spring-ioc

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

如何在Spring中注入List实例?

什么有效

假设我有一个ArrayList的spring bean定义:

<bean id="availableLanguages" class="java.util.ArrayList">
    <constructor-arg>
        <bean class="java.util.Arrays" factory-method="asList">
            <constructor-arg>
                <list>
                    <value>de</value>
                    <value>en</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)

现在我可以将它注入各种bean中,例如:

@Controller
class Controller {
    @Autowired
    public Controller(ArrayList<String> availableLanguages) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常有效.

怎么打破

但是,如果我稍微改变我的控制器并使用类型List而不是ArrayList这样:

@Controller
class Controller {
    @Autowired
    public Controller(List<String> availableLanguages) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我获得了所有类型bean的列表,String而不是我定义的bean.但是我实际上想要将List包装到一个不可修改的List中,但只有将我的依赖项降级到列表才能实现.

到目前为止发现了解决方法

以下XML文件:

<bean id="availableLanguages" class="java.util.Collections" factory-method="unmodifiableList">
    <constructor-arg>
        <bean class="java.util.Arrays" factory-method="asList">
            <constructor-arg>
                <list>
                    <value>de</value>
                    <value>en</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)

与此控制器一起使用:

@Controller
class Controller { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-ioc

5
推荐指数
1
解决办法
1529
查看次数

Spring Boot 测试没有从兄弟包中找到 bean

我有一个带@SpringBootTest注释的测试类,它想使用一个测试实用程序:

package org.myproject.server;

// ...

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ServerITest {

    private @Autowired TestHelperBean helper;

    // ...
}
Run Code Online (Sandbox Code Playgroud)

如果TestHelperBean是在与测试类相同的包中(或在子包中)定义的,则这可以正常工作。

package org.myproject.server;

import org.springframework.stereotype.Component;

@Component
public class TestHelperBean {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其移动到同级包,则找不到该组件:

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.myproject”类型的合格bean。testutils .TestHelperBean' 可用:预计至少有 1 个 bean 有资格作为自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我猜默认情况下组件扫描只在测试类的包和子包中查找——但是有没有办法覆盖这个默认值?我尝试将@ComponentScan注释添加到我的测试中,但这似乎没有任何效果:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ComponentScan("org.myproject")
public class ServerITest {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在 Spring Boot 测试中使用兄弟包中的 bean?

java spring spring-test spring-ioc spring-boot

5
推荐指数
1
解决办法
5861
查看次数

尽管使用了 @Primary,但两个同名的 bean 仍会导致 ConflictingBeanDefinitionException

我有一个应用程序初始值设定项类,用于将应用程序特定数据插入数据库。

@Component("applicationInitializer")
public class ApplicationInitializer {
    @PostConstruct
    public void init(){
        // some clever code here
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个DevApplicationInitializer类用于使用开发人员计算机上的一些示例数据初始化数据库(部署生产代码时不包括此类)。

@Component("applicationInitializer")
@Primary
public class DevApplicationInitializer extends ApplicationInitializer {
    @PostConstruct
    @Override
    public void init(){
        super.init();
        // even more clever code here
    }
}
Run Code Online (Sandbox Code Playgroud)

直到我给bean命名(只有@Component注释) - 一切都工作正常 - 当DevApplicationInitializer可用时,它被实例化而不是ApplicationInitializer. 当我给它们命名时applicationInitializer,抛出异常:

org.springframework.context.annotation.ConflictingBeanDefinitionException: 
Annotation-specified bean name 'applicationInitializer' for bean class
[com.example.DevApplicationInitializer] conflicts with existing, non-compatible 
bean definition of same name and class [com.example.ApplicationInitializer]
Run Code Online (Sandbox Code Playgroud)

当bean有名称时,为什么@Primary不尊重注释?我需要他们有一个,因为我确保在其他地方初始化程序已使用 …

java spring inversion-of-control spring-ioc

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

如何实现一个在原型上调用 destroy 方法的 bean 后处理器?

我正在阅读 Spring 文档并发现了这个

让 Spring 容器释放原型范围 bean 使用的资源的一种可能方法是使用自定义 bean 后处理器,该处理器将保存对需要清理的 bean 的引用。

但是,如果bean后处理器持有对原型对象的引用,那么垃圾收集器将不会清理它,并且原型bean及其资源将驻留在堆中,直到应用程序上下文关闭?

请您澄清一下好吗?

java spring prototype spring-ioc

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