使用ActFramework 框架时接口自动绑定。
1、定义以下接口:
public interface DataDictionaryService { public DataDictionary getDataDictionaryByDictId(String dictId); }2、实现该接口
public class DataDictionaryServiceImpl extends BaseService implements DataDictionaryService { @Inject private DataDictionary.Dao dataDictionaryDao; public DataDictionary getDataDictionaryByDictId(String dictId) { return dataDictionaryDao.findById(dictId); } }3、controller层
@Controller @UrlContext("df") public class DataDictionaryController extends BaseController { @Inject private DataDictionaryService dataDictionaryService; @GetAction("{dictId}") public void getDataDictionaryByDictId(H.Request request, String dictId) { Message message = new Message(); message.setData(dataDictionaryService.getDataDictionaryByDictId(dictId)); Controller.Util.renderJson(message); } }
备注:发现一个问题,访问路径如果前面加上/会有问题,比如/df会报访问路径不存在,改成df则可以访问。
4、访问
http://127.0.0.1:8080/df/594241bf18834238c57558a9时报以下错误:
Server Error - org.osgl.inject.InjectException: Cannot instantiate interface com.falcon.lianyue.data.dd.service.DataDictionaryService(dataDictionaryService)
接口没法注入
6、解决办法:
接口定义时加上@AutoBind注解,在controller层用@Inject注解时接口会自动注入。
@AutoBind public interface DataDictionaryService { public DataDictionary getDataDictionaryByDictId(String dictId); }导入@AutoBind
import act.inject.AutoBind;