Java 如何将服务类注入到非spring管理的类中

Java 如何将服务类注入到非spring管理的类中,java,Java,我有这样的情况: public class Other { public void test() { new ClassA().process(); } } public class ClassA { @Autowired private ClassB classB; public void process() { classB.executeSomething(); //--> NUllPOinter beca

我有这样的情况:

public class Other {
    public void test() {
        new ClassA().process();
    }
}

public class ClassA {

    @Autowired
    private ClassB classB;

    public void process() {
        classB.executeSomething(); //--> NUllPOinter because classA was not created by spring.
    }
}


@Service
public class ClassB {
    public void executeSomething() {
        // execute something
    }
}
我尝试使用ApplicationContext,但问题仍然存在

有人,你知道我该怎么做吗


谢谢。

这始终是声明类依赖关系的正确方法(始终适用于带注释的ioc Springbean):

在另一个类中,我们应该检索singleton ClassB实例。那么它必须是一个bean组件

@Component
public class Other {

    private final ApplicationContext applicationContext;

    @Autowired
    public Other(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void test() {
        new ClassA(applicationContext.getBean(ClassB.class)).process();
    }
}
如果其他类不能成为spring组件,则无法检索应用程序上下文,然后无法注入ClassB实例


显然,您可以将ClassB对象实例直接自动关联到其他组件中,但本例强调您可以使用ApplicationContext引用处理bean获取。

这始终是声明类依赖关系的正确方法(始终适用于带注释的ioc spring bean):

在另一个类中,我们应该检索singleton ClassB实例。那么它必须是一个bean组件

@Component
public class Other {

    private final ApplicationContext applicationContext;

    @Autowired
    public Other(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void test() {
        new ClassA(applicationContext.getBean(ClassB.class)).process();
    }
}
如果其他类不能成为spring组件,则无法检索应用程序上下文,然后无法注入ClassB实例

显然,您可以将ClassB对象实例直接自动关联到其他组件中,但本例强调您可以使用ApplicationContext引用处理bean获取。

首先,字段注入不能直接在非托管bean中工作。

更新以下类以进行测试

注意:它们在包
com.example

ClassB.java

@Service
public class ClassB {
    public void executeSomething() {
        System.out.println("Hi");
    }
}
Other.java

@Component
public class Other {

    @Autowired  
    private ClassB classB;

    public void test() {
        new ClassA(classB).process();
    }
}
public class ClassA {

    private ClassB classB;

    public ClassA(ClassB classB) {
        this.classB = classB;
    }

    public void process() {
        classB.executeSomething(); 
    }

}
ClassA.java

@Component
public class Other {

    @Autowired  
    private ClassB classB;

    public void test() {
        new ClassA(classB).process();
    }
}
public class ClassA {

    private ClassB classB;

    public ClassA(ClassB classB) {
        this.classB = classB;
    }

    public void process() {
        classB.executeSomething(); 
    }

}
我创建了一个用于测试的控制器

@Controller
public class DemoController {

    @Autowired
    private Other other;

    @RequestMapping("/")
    public void show() {
        other.test();
    }

}
更新
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.example" />

    <mvc:annotation-driven/>

</beans>
首先,字段注入不能直接在非托管bean中工作。

更新以下类以进行测试

注意:它们在包
com.example

ClassB.java

@Service
public class ClassB {
    public void executeSomething() {
        System.out.println("Hi");
    }
}
Other.java

@Component
public class Other {

    @Autowired  
    private ClassB classB;

    public void test() {
        new ClassA(classB).process();
    }
}
public class ClassA {

    private ClassB classB;

    public ClassA(ClassB classB) {
        this.classB = classB;
    }

    public void process() {
        classB.executeSomething(); 
    }

}
ClassA.java

@Component
public class Other {

    @Autowired  
    private ClassB classB;

    public void test() {
        new ClassA(classB).process();
    }
}
public class ClassA {

    private ClassB classB;

    public ClassA(ClassB classB) {
        this.classB = classB;
    }

    public void process() {
        classB.executeSomething(); 
    }

}
我创建了一个用于测试的控制器

@Controller
public class DemoController {

    @Autowired
    private Other other;

    @RequestMapping("/")
    public void show() {
        other.test();
    }

}
更新
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.example" />

    <mvc:annotation-driven/>

</beans>

您是如何尝试使用
ApplicationContext
?如果Spring不管理该类,则无法使用
@Autowired
。您是如何尝试使用
ApplicationContext
?如果Spring不管理该类,则不能使用
@Autowired