我想写一个CDI拦截器.如果我的注释仅包含1个参数,则截取效果很好,但如果使用2个参数则会中断.问题是为什么?
拦截器类:
@Monitored
@Interceptor
@Priority(APPLICATION)
public class MonitoringInterceptor {
@AroundInvoke
public Object logInvocation(InvocationContext ctx) throws Exception {
LOGGER.error("METHOD CALLED!!!"); //this is not called when annotation has 2 parameters
return ctx.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
注释:
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Monitored {
public String layer() default "BUSINESS";
public String useCase() default "N/A";
}
Run Code Online (Sandbox Code Playgroud)
现在有趣的部分:
@Stateless
public class MyBean {
//this does not work, why?
@Monitored(layer = "BUSINESS", useCase = "test")
//if I use the following annotation it works well
//@Monitored(layer …Run Code Online (Sandbox Code Playgroud) 我有以下界面
public interface IMyMapper<T> {}
Run Code Online (Sandbox Code Playgroud)
和实施
public class MyMapper implements IMyMapper<MyClazz> {}
Run Code Online (Sandbox Code Playgroud)
现在我想将类MyMapper注入无状态bean:
@Stateless
public class MyService {
@Inject
MyMapper mapper; //<-- does not work
@Inject
IMyMapper<MyClazz> mapper; //<-- also does not work
}
Run Code Online (Sandbox Code Playgroud)
我的问题是注射不起作用.我得到一个例外:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type MyMapper with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject mypackage.MyService.mapper
at mypackage.MyService.mapper(MyService.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:370)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:291)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:165)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:529)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:515)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:490)
at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:419)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:225)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
at …Run Code Online (Sandbox Code Playgroud) Java EE 7 应用程序在 Wildfly 9.0.2.Final 上运行。从@Asynchronous 方法中访问请求范围的数据存在问题。
在 Web 过滤器中,数据(例如令牌)被设置到 RequestScoped CDI bean 中。稍后我们要访问这些数据。如果我们在一个线程中工作,则一切正常。但是如果需要异步运行代码,就会出现问题。CDI 注入空 bean,请求数据丢失。
这是示例:
@RequestScoped
public class CurrentUserService implements Serializable {
public String token;
}
@Stateless
public class Service {
@Inject
private RestClient client;
@Resource
private ManagedExecutorService executorService;
@Resource
private ContextService contextService;
@Asynchronous
private <T> Future<T> getFuture(Supplier<T> supplier) {
Callable<T> task = supplier::get;
Callable<T> callable = contextService.createContextualProxy(task, Callable.class);
return executorService.submit(callable);
}
public String getToken() throws Exception {
return getFuture(client::getToken).get();
}
}
@ApplicationScoped
public class RestClient { …Run Code Online (Sandbox Code Playgroud) 我最近将一个简单的Java EE 6项目迁移到了Java EE 7.详细地说,这意味着我只是将依赖关系更改javax:javaee-api:6.0为javax:javaee-api:7.0并将其部署到Glassfish 4而不是Glassfish 3.
之后应用程序不再起作用,因为CDI无法注入带注释的依赖项.
我正在尝试开发一个Java EE 7 Web应用程序,它使用websocket端点并将其部署在Jetty服务器上.
该应用程序具有以下结构:
Game/
src/
main/
java/
game/
WebSocketEndpoint.java
webapp/
index.html
scripts/
variousjavascriptstuff.js
WEB-INF/
beans.xml
web.xml
Run Code Online (Sandbox Code Playgroud)
在beans.xml文件中:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
Run Code Online (Sandbox Code Playgroud)
WebSocketEndpoint被正确注释并且可以与Netbeans/Glassfish4一起使用,但是,应用程序必须部署在Jetty服务器上.
所以,我的问题 - 如何将websocket端点映射到web.xml文件中的URL /游戏?我已经找到了一些映射servlet的例子,但我认为这不适用于服务器端点.
或者,有没有办法为Jetty编写web.xml文件,以便它自动发现ll带注释的类/方法(类似于上面的beans.xml)
我有一个有状态的EJB,@Asynchronous返回一个事务方法Future<T>.它是从Web层(@SessionScopedCDI bean)调用的,如下所示:
@SessionScoped
@Named
public class SessionBean {
@EJB
EjbService service
public void call() {
Future<Object> response = service.process();
}
}
@Stateful
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EjbService {
@Asynchronous
public Future<Object> process() {
//
}
}
Run Code Online (Sandbox Code Playgroud)
问题是如果用户在执行此异步调用期间终止Web会话,事务会发生什么?
以下示例适用于我的Java EE6(Glassfish3)项目,但在切换到Java EE7(Glassfish4)后失败.HTTP请求返回"500内部错误",Glassfish服务器日志中没有任何消息.该项目使用NetBeans8作为Maven Web Project进行设置,没有特殊的依赖项,beans.xml或其他配置.
@RequestScoped
@Path("generic")
public class GenericResource {
@GET
@Path("ping")
@Produces(APPLICATION_JSON)
public List<String> debugPing() {
return Arrays.asList("pong");
}
Run Code Online (Sandbox Code Playgroud)
然后:
$ curl -v http://localhost:8080/mavenproject2/webresources/generic/ping
> GET /mavenproject2/webresources/generic/ping HTTP/1.1
...
< HTTP/1.1 500 Internal Server Error
Run Code Online (Sandbox Code Playgroud)
据我了解,所有REST处理都是由Jackson参考实现完成的,Jackson使用Jersey作为底层JSON库.其中一个应该为所有基本数据类型提供某种提供程序.只有自定义类需要自编写的ObjectMapper.这些概念仍然正确吗?
注意:类似的问题已经在三年前提出过,在EE 6的时候,看看如何为一个类实例化多个CDI/Weld bean?EE 7有什么变化吗?
在Spring中,可以通过在xml conf中定义相应的bean来实例化任何类.也可以为一个具有不同参数的类实例化更多的bean .....
是否可以在CDI中执行此操作,我的意思是创建一个实例而不创建另一个类?
春天的例子:
<bean id="foo" class="MyBean">
<property name="someProperty" value="42"/>
</bean>
<bean id="hoo" class="MyBean">
<property name="someProperty" value="666"/>
</bean>
Run Code Online (Sandbox Code Playgroud) 我的非常简单的Web应用程序出了什么问题:Web应用程序成功部署到app服务器但是hello bean没有注入index.xhtml页面(页面显示来自Facelets的Hello:#{hello.value})?
(这是我第一次使用JSF,所以也许这个问题很容易,而且我也使用了很好的文章http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java- ee-app-without-any.html)
我有下一个战争档案结构:
mywebapp
|
- WEB_INF
|
- classes
|
- Hello.class
- index.html
Run Code Online (Sandbox Code Playgroud)
Hello.java有:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class Hello {
private String value;
public String getValue() {
return "Hello JSF";
}
public void setValue(String value) {
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>My Facelet Page Title</title>
</h:head>
<h:body>
Hello from Facelets: #{hello.value}
</h:body>
</html> …Run Code Online (Sandbox Code Playgroud) 我想使用IntelliJ Idea Community Edition在Java Enterprise和Google Web Tool中开发应用程序.可能吗?或者我必须购买终极版.