简体   繁体   中英

How to programatically add beans in Java to the app-context.xml in Spring?

I was following this tutorial http://java-diaries.blogspot.com/2011/03/get-started-with-spring-jms-using.html and I do not understand why for an @Service and an @Autowired annotation you need to explicitly create beans in the app-context.xml but for the @Component, it creates one for you. Can someone explain this? Also, is there a way to add the destination and connectionFactory beans using annotations? Or do i need a solution along the lines of Add Bean Programmatically to Spring Web App Context

If you're using the context:component-scan tag, classes annotated with both @Component and @Service will be detected and don't need to be declared in your application context.

@Autowired is different. When you declare a field @Autowired, it means that the bean that's in the app context -- whether declared explicitly or picked up by the component scan -- is injected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM