这是webxml中的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application-context.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
如何通过controller中获取bean,如果使用ClassPathApplicationContext获取会报错,请问该怎么办啊?各位大哥谁能帮一下啊
在controller中获取bean可以使用注解哇:
1.在你的Spring配置中加入
<context:component-scan base-package="cn.lonecloud.xxx" annotation-config="true"/>
2.比如你需要获取userService的一个Bean
使用
@Autowired
注解3.前提需要在你需要的注解类中加入
@service
即可获取得到