當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring自动扫描配置及使用方法
生活随笔
收集整理的這篇文章主要介紹了
Spring自动扫描配置及使用方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
首先,檢查一下你lib下有沒有 common-annotations.jar 這個jar包 沒有的話要導(dǎo)入工程。
下一步配置spring的配置文件applicationContex.xml,加入命名空間
紅色為需要添加的內(nèi)容
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context ">http://www.springframework.org/schema/context/spring-context-2.5.xsd><context:component-scan base-package="你要掃描那個包(例如com.myprogram)" annotation-config="true"/>這句話的意思時,當啟動服務(wù)器的時候spring自動掃描你設(shè)置掃描的包下面的添加注解了的類
例如
服務(wù)層service定義接口和接口實現(xiàn)類
接口
public interface UsersLogsService {public abstract void delete(UsersLogs persistentInstance); }實現(xiàn)類
@Service("UsersLogsService")//注解項 public class UsersLogsServiceImpl implements UsersLogsService {public void delete(UsersLogs persistentInstance) {}}控制層下的某個Action,
//自己定義需要注意的是"/loginAction"要與你的struts-config.xml配置文件中映射action的path一致
@Controller("/loginAction")//注解項 public class LoginAction extends DispatchAction {//通過Resource注解我們就可以獲得剛才注解了的UsersLogsService實例調(diào)用他的方法@Resource(name="UsersLogsService") private UsersLogsService usersLogsService;public ActionForward checkLogin(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {return null;}}到此位置,配置的流程就是這樣,Dao數(shù)據(jù)操作層我沒有寫,創(chuàng)建Dao添加注解
服務(wù)層調(diào)用Dao就像上面控制層調(diào)用服務(wù)層一樣,只要上面流程弄明白了,那么恭喜你,spring自動掃描的技術(shù)你也就學會了。
轉(zhuǎn)載于:https://my.oschina.net/longtutengfei/blog/164741
總結(jié)
以上是生活随笔為你收集整理的Spring自动扫描配置及使用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到的事情在现实中发生什么效应
- 下一篇: JavaScript代码片段