Guice 学习(二)构造器注入(Constructor Inject)

为了演示下面的支持多参数的构造函数注入,我在这里写了2个接口和其实现类。注意事项写在了程序注释里面。

1、接口 (interface)
/*
 * Creation : 2015年6月30日
 */
package com.guice.constructorInject;

import com.google.inject.ImplementedBy;

@ImplementedBy(ServiceImpl.class)
public interface Service {
    public void execute();
}
/*
 * Creation : 2015年6月30日
 */
package com.guice.constructorInject;

import com.google.inject.ImplementedBy;

@ImplementedBy(HelloGuiceImpl.class)
public interface HelloGuice {
    public void sayHello();
}
2、实现类( implementation)
package com.guice.constructorInject;

public class ServiceImpl implements Service {
    @Override
    public void execute() {
        System.out.println("Hello Guice ,this is field inject demo !");

    }
}
package com.guice.constructorInject;

import com.google.inject.Singleton;

/*
 * 保证是单例
 */
@Singleton
public class HelloGuiceImpl implements HelloGuice {

    @Override
    public void sayHello() {
        System.out.println("Hello Guice !");

    }

}
3、测试类
/*
 * Creation : 2015年6月30日
 */
package com.guice.constructorInject;

import com.google.inject.Guice;
import com.google.inject.Inject;

public class ConstructorInjectDemo {
    private Service service;
    private HelloGuice helloGuice;

    public Service getService() {
        return service;
    }

    public HelloGuice getHelloGuice() {
        return helloGuice;
    }

    /**
     * 构造函数注入: 好处:可以保证只有一个地方来完成属性注入,
     * 可以确保在构造函数中完成一些初始化工作 不足:类的实例化与参数绑定了,限制了实例化类的方式。
     */
    /*
     * @Inject 
     * public ConstructorInjectDemo(Service service) { 
     *      this.service = service; 
     * }
     */

    /*
     * 支持多参数构造函数注入 ,但是必须只有一个构造函数上面标注Inject
     */
    @Inject
    public ConstructorInjectDemo(Service service, HelloGuice helloGuice) {
        this.service = service;
        this.helloGuice = helloGuice;
    }

    public static void main(String[] args) {
        ConstructorInjectDemo instance = Guice.createInjector().getInstance(ConstructorInjectDemo.class);
        instance.getService().execute();
        instance.getHelloGuice().sayHello();

    }

}

输出结果:

Hello Guice ,this is field inject demo !
Hello Guice !

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
虽然Spring和Guice都是依赖注入框架,但它们的设计理念和实现方式有所不同。如果您想将Guice集成到Spring中,可以考虑使用Spring-Guice集成桥接,它提供了一个简单的方法来将Guice注入到Spring应用程序中。您可以使用Guice来管理一些对象,同时使用Spring来管理其他对象,这样您就可以根据需要使用这两个框架的不同优势。 要使用Spring-Guice集成桥接,您需要在Spring配置文件中声明GuiceModuleLoader bean,并指定要加载的Guice模块。然后,您可以在Spring中注入Guice管理的对象,就像注入Spring Bean一样。例如: ```xml <bean id="guiceModuleLoader" class="org.springframework.guice.moduleloader.GuiceModuleLoader"> <property name="moduleClassNames"> <list> <value>com.example.MyGuiceModule</value> </list> </property> </bean> <bean id="myService" class="com.example.MyService"> <constructor-arg ref="myDependency"/> </bean> <bean id="myDependency" factory-bean="guiceModuleLoader" factory-method="getInstance"> <constructor-arg value="com.example.MyDependency"/> </bean> ``` 在这个例子中,MyService是一个Spring Bean,它需要注入一个Guice管理的MyDependency对象。您可以通过使用GuiceModuleLoader的getInstance方法来获取MyDependency实例,然后将其注入到MyService中。 需要注意的是,在使用Spring-Guice集成桥接时,Guice管理的对象不会受到Spring的生命周期管理。因此,您需要自己负责管理这些对象的生命周期,例如在应用程序关闭时手动清理它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值