Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在我的项目中使用 spring MVC,在服务类中我使用了“@Autowired”注释,尽管我必须在 Application-Servlet.xml 的 bean 标记中定义所有类。(如果我们有“N”个,我觉得它很复杂类)我怎样才能避免这样做?
目前尚不清楚您在 XML 配置中定义了哪些类,但我猜您正在询问您的服务类...
只需用@Service(或@Component)注释您的服务类。Spring 文档将解释使用哪个并提供示例。注意:您需要有这些 XML 配置行才能使这项工作:<context:component-scan base-package="com.foo.bar"/>和<mvc:annotation-driven />
@Service
@Component
<context:component-scan base-package="com.foo.bar"/>
<mvc:annotation-driven />
也适用于控制器,只需使用@Controller( docs ) 进行注释。
@Controller