Java / Spring新手在这里。我有一个关于通过SpringBoot进行自动连线的问题。
我有一个接口,一个实现,一个主类和一个配置类,如下所示:
ServiceInterface.java
public interface ServiceInterface {
static String serviceName = "service";
public void displayMessage();
public String getServiceName();
}
ServiceImpl1.java
public class ServiceImpl1 implements ServiceInterface{
static String serviceName = "default service value ";
public String getServiceName() {
return serviceName;
}
@Override
public void displayMessage()
{
System.out.println("This is implementation 1");
}
}
主要班级:
@SpringBootApplication
public class App implements CommandLineRunner{
@Autowired
private ServiceInterface serviceInterface;
public static void main(String args[])
{
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) {
serviceInterface.displayMessage();
System.out.println(serviceInterface.getServiceName());
System.out.println(serviceInterface.serviceName );
}
}
AppConfig.java
@Configuration
public class AppConfig {
@Bean
ServiceInterface serviceInterface()
{
return new ServiceImpl1();
}
}
当我运行代码时,我将其作为输出
This is implementation 1
service 1
default service value
为什么变量' serviceName' ServiceImpl1实现内部无法通过Spring通过自动装配创建的对象访问?