在Spring主应用程序中使用@Autowired。

huangapple 未分类评论42阅读模式
英文:

Using @Autowired in the spring main application

问题

Main Class

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private Menu obj;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        obj.start();
    }
}

Menu Class

@Component
public class Menu {
    
    public void start(){
        //implementation 
    }
}
英文:

I created a spring project and in here I implement the command line runner as the application I create need to be run on the console. So inside the run method I created an object of another class and called the method for executing the application.<br>

So when creating the object I had to make it Autowired and the relevant class a Component.<br>
Is it a good coding way to do code or is there any other way to avoid the null pointer exception.<br>

And my knowledge on why I put the Autowired is not 100% certain, I think Autowired keyword injects the beans of the class Menu so the methods and variables of that class can be called. Is it correct ?

Main Class

    @SpringBootApplication
    public class DemoApplication implements CommandLineRunner {

	@Autowired
	private Menu obj;

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		obj.start();
	}
    }

Menu Class

    @Component
    public class Menu {
        
        public void start(){
            //implementation 
        }
    }

答案1

得分: 0

我认为这好多了

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @SuppressWarnings("resource")
    public static void main(final String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication, args);

        context.getBean(Menu).start(); // <-- 这里
    }
}

希望有用

英文:

I think this much better

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @SuppressWarnings(&quot;resource&quot;)
    public static void main(final String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication, args);

        context.getBean(Menu).start(); // &lt;-- here
    }
}

Hope useful

答案2

得分: 0

我认为您可以在方法上使用 @PostConstruct 注解,而不是使用 command line runner 在应用程序运行后执行代码。

示例:

@PostConstruct
public void init(){
   obj.start();
   // 或者任何使用了 @Autowired 注入的代码
}
英文:

I think you can use @PostConstruct annotation over a method instead of command line runner to execute code after running the application.

example:

@PostConstruct
public void init(){
   obj.start();
// or any code that use @autowired injection
}

huangapple
  • 本文由 发表于 2020年7月26日 22:50:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/63101746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定