在.NET工作过后,我非常精通NancyFX和Web API等微型Web框架对IoC容器的支持。
在像Sinatra这样的Ruby类似框架中(NancyFX基于Sinatra),似乎你有依赖注入的能力。
从我看到的,因为Java spark应用程序作为主要方法运行,看起来你可以传入依赖项或IoC容器。
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
我很难理解这样的框架在没有支持的情况下如何有用。
如果这个框架没有,那么是否有另一个轻量级框架(Spring不是我记忆中的轻量级框架,但可能已经发生了变化),它确实支持这个框架吗?
答案 0 :(得分:8)
Spring可以简单地与Spark集成。例如
public interface Spark {
/**
* adds filters, routes, exceptions, websockets and others
*/
void register();
}
@Configuration
public class SparkConfiguration {
@Autowired(required = false)
private List<Spark> sparks = new ArrayList<>();
@Bean
CommandLineRunner sparkRunner() {
return args -> sparks.stream().forEach( spark -> spark.register());
}
}
@Component
public class HelloSpark implements Spark {
@Autowired
private HelloWorldService helloWorldService;
@Override
public void register() {
get("/hello", (request, response) -> helloWorldService.hello() );
}
}
上找到更多信息
答案 1 :(得分:3)
使用Guice和Java Spark非常容易。基本上,您需要以下列方式扩展SparkFilter
以创建Guice注入器。
public class SparkGuiceFilter extends SparkFilter {
private Injector injector = null;
@Override
protected SparkApplication[] getApplications(final FilterConfig filterConfig) throws ServletException {
final SparkApplication[] applications = super.getApplications(filterConfig);
if (this.injector == null) {
this.injector = Guice.createInjector(new MainModule());
}
if (applications != null && applications.length != 0) {
for (SparkApplication application : applications) {
this.injector.injectMembers(application);
}
}
return applications;
}
}
然后你需要一个web.xml
并且必须使用Jetty或任何其他servlet容器将你的Spark应用程序作为普通war
应用程序运行:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<filter>
<filter-name>SparkGuiceFilter</filter-name>
<filter-class>com.devng.spark.guice.SparkGuiceFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.devng.spark.SparkApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SparkGuiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
但是,这种方法存在一些局限性。您不能将基于请求或会话范围与Guice一起使用。如果你不需要这个,那么你很高兴,否则你需要整合Guice Servlet Extensions并在GuiceFilter
中添加web.xml
,如the official Guice documentation中所述。您还需要确保在GuiceFilter
和SparkGuiceFilter
中使用相同的注入器实例来执行GuiceServletContextListener
中所需的web.xml
,如上所述{ {3}}
您可以在我的GitHub here
中找到一个完整的示例答案 2 :(得分:0)
我最近一直在使用Spark,它不包括开箱即用的IoC提供商,但是,你可以轻松地包含Spring或Guice core ,这将是一个轻量级的解决方案
您需要做的就是将依赖项添加到Maven并开始使用它。
作为替代方案,您可以查看Ninja,这是一个完整的堆栈框架,包含Guice,JPA / Hibernate开箱即用。
答案 3 :(得分:0)
自己动手IoC with Guice
。它找不到很多代码;)
链接: https://github.com/Romain-P/SparkJava-JFast
默认情况下不需要guice模块,可以检测到自动检测绑定对象。
public class Main {
public static void main(String[] args) {
/* give a class as argument for package scanning from its path recursively */
Injector injector = SparkApplication.init(Application.class);
injector.getInstance(Application.class).initialize();
}
}
@Binding
public class Application {
@Inject Service http;
@Inject HelloController helloController;
public void initialize() {
http.port(8080);
http.get("/hello", (req, res) -> helloController.hello());
}
}
@Binding
public class HelloController {
@Inject HelloService service;
public Object hello() {
//business logic
return service.hello();
}
}
@Binding
@Slf4j
public class HelloService {
public Object hello() {
log.info("hello");
return new Object();
}
}