spring - 具有作用域原型的抽象 class 中的自动装配接口不起作用
spring - autowiring interface in abstract class with scope prototype does not work
我有一个 example spring project,其中我有一个抽象 class Athlete
,我想将接口 Trainer
.
自动装配到其中
Athlete
是 Runnable
并且其实现覆盖 perform()
方法。
当我在 Sprinter
中调用 perform()
(扩展 Athlete
)时,我想自动装配到 Athlete
中的 Trainer
实例仍然为 null
运动员:
@Component
@Scope("prototype")
public abstract class Athlete implements Runnable{
protected final String name;
@Autowired protected Trainer trainer;
public Athlete(String name)
{
this.name = name;
}
protected abstract void perform();
@Override
public void run() {
perform();
}
}
短跑选手:
@Component
@Scope("prototype")
public class Sprinter extends Athlete {
public Sprinter(String name) {
super(name);
}
@Override
protected void perform()
{
this.trainer.giveAdviceTo(name); // TRAINER IS NULL !!!!!!
for(int i=0;i<3;i++)
{
System.out.println("Sprinting...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
实施Trainer
@Component
public class TrainerImpl implements Trainer{
@Override
public void giveAdviceTo(String name)
{
System.out.println("Go " + name + "!!");
}
}
感谢您的帮助
在我看来,在您的主要 class 中,您正在自己创建这些对象。
Athlete a1 = new Sprinter("adam");
Spring 只能将对象自动装配到(托管)bean 中。此时,Spring 根本不知道您创建的 Sprinter 实例是否存在。
当您让 Spring 为您创建 bean 时,它还会注入所有 @Autowired 依赖项。
@Autowired
private BeanFactory beanFactory;
@Override
public void run(String... args) throws Exception {
Sprinter adam = beanFactory.getBean(Sprinter.class, "adam");
TennisPlayer roger = beanFactory.getBean(TennisPlayer.class, "roger");
executor.execute(adam);
executor.execute(roger);
}
您正在尝试使用非默认构造函数(带参数的构造函数)创建 bean 对象。您可以在 class 中声明默认构造函数,或者如果您真的想使用非默认构造函数创建 bean 实例,那么您可以这样做。
我有一个 example spring project,其中我有一个抽象 class Athlete
,我想将接口 Trainer
.
Athlete
是 Runnable
并且其实现覆盖 perform()
方法。
当我在 Sprinter
中调用 perform()
(扩展 Athlete
)时,我想自动装配到 Athlete
中的 Trainer
实例仍然为 null
运动员:
@Component
@Scope("prototype")
public abstract class Athlete implements Runnable{
protected final String name;
@Autowired protected Trainer trainer;
public Athlete(String name)
{
this.name = name;
}
protected abstract void perform();
@Override
public void run() {
perform();
}
}
短跑选手:
@Component
@Scope("prototype")
public class Sprinter extends Athlete {
public Sprinter(String name) {
super(name);
}
@Override
protected void perform()
{
this.trainer.giveAdviceTo(name); // TRAINER IS NULL !!!!!!
for(int i=0;i<3;i++)
{
System.out.println("Sprinting...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
实施Trainer
@Component
public class TrainerImpl implements Trainer{
@Override
public void giveAdviceTo(String name)
{
System.out.println("Go " + name + "!!");
}
}
感谢您的帮助
在我看来,在您的主要 class 中,您正在自己创建这些对象。
Athlete a1 = new Sprinter("adam");
Spring 只能将对象自动装配到(托管)bean 中。此时,Spring 根本不知道您创建的 Sprinter 实例是否存在。
当您让 Spring 为您创建 bean 时,它还会注入所有 @Autowired 依赖项。
@Autowired
private BeanFactory beanFactory;
@Override
public void run(String... args) throws Exception {
Sprinter adam = beanFactory.getBean(Sprinter.class, "adam");
TennisPlayer roger = beanFactory.getBean(TennisPlayer.class, "roger");
executor.execute(adam);
executor.execute(roger);
}
您正在尝试使用非默认构造函数(带参数的构造函数)创建 bean 对象。您可以在 class 中声明默认构造函数,或者如果您真的想使用非默认构造函数创建 bean 实例,那么您可以这样做。