首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Google Guice 依赖注入形式

2012-06-30 
Google Guice 依赖注入方式  Google Guice有三种依赖注入方式。?  一。Field注入?package com.template.guic

Google Guice 依赖注入方式

  Google Guice有三种依赖注入方式。

?

  一。Field注入

?

package com.template.guice;import com.google.inject.Inject;/** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-8-2 * Time: 下午9:39 */public class CommentServiceImpl implements CommentService {    @Inject    private CommentDao commentDao;    @Override    public void comment() {        commentDao.comment("This is a comment message!");    }}

?

  二。Constructor注入

?

package com.template.guice;import com.google.inject.Inject;/** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-8-2 * Time: 下午9:39 */public class CommentServiceImpl implements CommentService {    private CommentDao commentDao;    @Inject    public CommentServiceImpl(CommentDao commentDao) {        this.commentDao = commentDao;    }    @Override    public void comment() {        commentDao.comment("This is a comment message!");    }}

?

?

  三。Setter注入

?

package com.template.guice;import com.google.inject.Inject;/** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-8-2 * Time: 下午9:39 */public class CommentServiceImpl implements CommentService {    private CommentDao commentDao;        @Override    public void comment() {        commentDao.comment("This is a comment message!");    }    @Inject    public void setCommentDao(CommentDao commentDao) {        this.commentDao = commentDao;    }}
?

热点排行