我正在寻找创建一个示例项目,同时学习Guice,它使用JDBC来读/写SQL数据库.然而,经过多年的使用Spring并让它抽象出连接处理和事务,我正在努力从概念上运用它.
我想要一个启动和停止事务的服务,并调用大量的存储库,这些存储库重用相同的连接并参与同一个事务.我的问题是:
下面的代码显示了我将如何在Spring中执行此操作.注入每个存储库的JdbcOperations可以访问与活动事务关联的连接.
我无法找到许多涵盖此内容的教程,除了显示为事务创建拦截器的教程之外.
我很高兴继续使用Spring,因为它在我的项目中工作得非常好,但我想知道如何在纯Guice和JBBC中做到这一点(没有JPA/Hibernate/Warp/Reusing Spring)
@Service
public class MyService implements MyInterface {
@Autowired
private RepositoryA repositoryA;
@Autowired
private RepositoryB repositoryB;
@Autowired
private RepositoryC repositoryC;
@Override
@Transactional
public void doSomeWork() {
this.repositoryA.someInsert();
this.repositoryB.someUpdate();
this.repositoryC.someSelect();
}
}
@Repository
public class MyRepositoryA implements RepositoryA {
@Autowired
private JdbcOperations jdbcOperations;
@Override
public void someInsert() {
//use jdbcOperations to perform an insert
}
}
@Repository
public class MyRepositoryB implements RepositoryB {
@Autowired
private JdbcOperations jdbcOperations;
@Override
public void someUpdate() {
//use …Run Code Online (Sandbox Code Playgroud) 我正在尝试用Google Guice 2.0注入东西,我有以下结构:
FooAction implements Action
BarAction implements Action
Run Code Online (Sandbox Code Playgroud)
然后我有一个ActionLibrary与以下构造函数:
ActionLibrary (List<Action> theActions)
Run Code Online (Sandbox Code Playgroud)
当我从Guice请求一个ActionLibrary实例时,我希望Guice能够识别两个已注册的Action类(FooAction,BarAction)并将它们传递给构造函数.这里的动机是当我添加第三个动作BazAction时,它就像在模块中注册它一样简单,它会自动添加到构造函数的列表中.
这可能吗?
我有一个工厂如下,
public final class Application {
private static IFoo foo;
public static IFoo getFoo(String bar)
{
// i need to inject bar to the constructor of Foo
// obvious i have to do something, not sure what
Injector injector = Guice.createInjector();
logger = injector.getInstance(Foo.class);
return logger;
}
}
Run Code Online (Sandbox Code Playgroud)
这是Foo的定义:
class Foo
{
Foo(String bar)
{
}
}
Run Code Online (Sandbox Code Playgroud)
好.我不知道如何使用Guice将此参数传递给Foo构造函数?
有任何想法吗?
我正在扩展Guice,AbstractModule并且在扩展类中我需要访问Guice的注入器.这可能,如果是的话,怎么样?
我需要绑定一个类作为两个接口的实现.它应该绑定在单一范围内.
我做了什么:
bind(FirstSettings.class).
to(DefaultSettings.class).
in(Singleton.class);
bind(SecondSettings.class).
to(DefaultSettings.class).
in(Singleton.class);
Run Code Online (Sandbox Code Playgroud)
但是,显然,它会导致创建两个不同的实例,因为它们绑定到不同的键.
我的问题是我该怎么做?
我向Google Guice提供了连接我的对象的责任.但是,我如何测试绑定是否运行良好?
例如,假设我们有一个A具有依赖性的类B.如何测试B是否正确注入?
class A {
private B b;
public A() {}
@Inject
public void setB(B b) {
this.b = b
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,A没有getB()方法,我想断言A.b不是null.
情况:我需要一些延迟依赖实例化FooClass,所以我Injector作为构造函数参数传递给类.
private final Injector m_injector;
public FooClass(@Named("FooInjector") Injector injector) {
m_injector = injector;
}
Run Code Online (Sandbox Code Playgroud)
但是guice不允许绑定核心类(注入器,模块等).解决办法是什么?
我正在尝试迁移一个小项目,用Guice替换一些工厂(这是我的第一个Guice试验).但是,在尝试注射仿制药时,我陷入困境.我设法提取了一个带有两个类和一个模块的小玩具示例:
import com.google.inject.Inject;
public class Console<T> {
private final StringOutput<T> out;
@Inject
public Console(StringOutput<T> out) {
this.out = out;
}
public void print(T t) {
System.out.println(out.converter(t));
}
}
public class StringOutput<T> {
public String converter(T t) {
return t.toString();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(StringOutput.class);
bind(Console.class);
}
public static void main(String[] args) {
Injector injector = Guice.createInjector( new MyModule() );
StringOutput<Integer> out = injector.getInstance(StringOutput.class); …Run Code Online (Sandbox Code Playgroud) 我一直在玩Play!框架为斯卡拉现在将近一年.我目前正在使用2.5.x版.
我知道Play中控制器的发展以及开发人员如何被迫远离静态object路由.
我也知道Guice在游戏中的用法.
如果您下载激活器并运行:
activator new my-test-app play-scala
Run Code Online (Sandbox Code Playgroud)
Activator将为您生成模板项目.我的问题是围绕该模板的这个文件.
我的测试,应用程序/应用程序/服务/ Counter.scala
package services
import java.util.concurrent.atomic.AtomicInteger
import javax.inject._
/**
* This trait demonstrates how to create a component that is injected
* into a controller. The trait represents a counter that returns a
* incremented number each time it is called.
*/
trait Counter {
def nextCount(): Int
}
/**
* This class is a …Run Code Online (Sandbox Code Playgroud) 假设我有一个Guice模块ProdModule,我想依赖其他GuiceModule,ProdDbModule和ProdPubSubModule.我如何实现ProdModule的configure()?