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; }}?