简体   繁体   中英

Creation of Spring beans without referring or coding in app context file

我想知道是否有一种方法来定义在不直接将它们编码在spring应用程序上下文文件中的情况下创建的spring bean。

You can use component scan.

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

or @ComponentScan({"com.foo.bar", "org.example"}) for java config

That way spring will scan the secified package for classes annotated with @Component (or its inheritors @Service , @Repository and @Controller ) and will register them as spring beans.

You can read more here: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s12.html

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