相关疑难解决方法(0)

在一个非单例bean上修复Spring代理上的BeanNotOfRequiredTypeException?

我在从应用程序上下文中提取Spring bean时遇到问题.

当我尝试;

InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class);
Run Code Online (Sandbox Code Playgroud)

我明白了

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'innerThread' must be of type [com.generic.InnerThread], but was actually of type [$Proxy26]
Run Code Online (Sandbox Code Playgroud)

如果没有getBean()调用中的指定类,我会得到一个ClassCastException(您可以在下面详细介绍).

InnerThread bean被初始化为非单例,因为我需要多个实例.InnerThread类还扩展了Thread.有趣的是,这个错误出现在OuterThread中,它的设置方式与InnerThread完全相同.

我试图在下面包含所有相关的代码清单/堆栈跟踪.拥有更多Spring经验的人能告诉我这里发生了什么吗?


代码/配置清单

OuterThread.java片段:

public class OuterThread extends Thread {
    private Queue<InnerThread> createInnerThreads() {
        Queue<InnerThread> threads = new ArrayBlockingQueue();

        ApplicationContext ctx = SpringContextFactory.getApplicationContext();
        int i = 0;
        for (SearchRule search : searches) {
            logger.debug("Number of times looped " + i++);
            //Seprated lines to get a better sense of what is going on
            Object …
Run Code Online (Sandbox Code Playgroud)

java aop spring casting proxy-classes

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

获取Spring错误"Bean命名为'x'必须是[y]类型,但实际上是Jenkins类型[$ Proxy]"

我现在已经调试了一段时间了,我希望有人能在这里说清楚.

我有一个使用JDK 1.6添加到Jenkins的Maven项目.我在这个项目中使用AOP来处理数据库事务.

当我在Jenkins中运行构建时,我的测试用例失败,但有以下例外: -

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataHandlerClassificationImpl': 
Injection of resource dependencies failed; nested exception is 
org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData], 
but was actually of type [$Proxy17]
    ...
    ...
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData], 
but was actually of type [$Proxy17]
    ...
    ...
Run Code Online (Sandbox Code Playgroud)

DataHandlerClassificationImpl课程看起来像这样: -

@Service
public class DataHandlerClassificationImpl extends DataHandler {

    @Resource(name="writerDataLocationImpl")
    private WriterData writerData;

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

WriterData 是一个具有多个实现的接口.

我能够在没有问题的情况下从IDE执行代码.为了确定它是Maven问题还是Jenkins问题,我使用命令行导航到Jenkins的项目作业文件夹,我能够毫无错误地运行mvn test …

junit spring unit-testing hudson jenkins

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

自动装配的spring bean不是代理

我正在开发一个连接MySQL数据库的非常小的应用程序.

我正在尝试创建表记录但是"没有正在进行的事务".

我有适当的东西:

  • 服务接口MyService及其实现MyServiceImpl
  • 我用@Service注释了服务impl
  • 在控制器中,我使用字段@Autowired MyService的接口名称
  • 我有正确的事务配置,因为它最初是由roo生成的
  • MyServiceImpl实现了一个公共方法MyService.create(...)

但,

当我远程调试并检查控制器的myService字段时,我看到的是com.some.package.services.MyService@12345(而不是像$ Proxy73这样的东西)对我来说是不对的,因为应该自动装配的是代理不是他的目标豆(我认为这是).如果我是正确的,那么没有事务是有意义的,因为注释只会在调用在代理上使用@Transactional注释的公共方法时启动.

请告诉我为什么spring在这个设置中注入目标bean.

谢谢

service proxy spring target autowired

9
推荐指数
1
解决办法
8247
查看次数

Spring AOP介绍中原始接口丢失

这是我的 Spring AOP 配置。

<bean id="myObject" class="com.madzone.learn.spring.aop.OriginalClass"></bean>
<bean id="aspect" class="com.madzone.learn.spring.aop.AspectClass"></bean>
<aop:config>
    <aop:aspect ref="aspect">
        <aop:declare-parents
            types-matching="com.madzone.learn.spring.aop.OriginalClass+"
            implement-interface="com.madzone.learn.spring.aop.IntroducedInterface"
            default-impl="com.madzone.learn.spring.aop.IntroducedInterfaceImpl" />
    </aop:aspect>
Run Code Online (Sandbox Code Playgroud)

ApplicationContext context = new ClassPathXmlApplicationContext("myApp.xml");
Object myObject = context.getBean("myObject");
if (myObject instanceof OriginalClass) {
    System.out.println("This is OriginalClass");
}
if(myObject instanceof IntroducedInterface) {
    System.out.println("This is IntroducedInterface");
}
Run Code Online (Sandbox Code Playgroud)

通过这个介绍,我能够调用IntroducedInterface 中的方法。但是,我无法访问 OriginalClass 的方法。在上面的代码片段中,我从未打印出“This is OriginalClass”。

根据“介绍”的定义,我了解到实现新接口的代理将从 OriginalClass 扩展并使其方法也可访问。

我在这里错过了什么吗?如果有的话,有人可以解释原因吗?

PS:以下是Spring in Action(第3版)中描述的图片。 调用者可以访问接口“现有方法”和“引入方法”吗?

java spring spring-aop

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