标签: java-ee-8

当在字段上指定 时,javax.enterprise.context.RequestScoped 如何工作?

在代码中发现以下内容(真实姓名替换为虚拟姓名):

JAX-RS 资源

@Path("hello")
public class HelloResource {

  @Inject
  @RequestScoped
  FirstService service1;

  @Inject
  SecondService service2;

  ....

}
Run Code Online (Sandbox Code Playgroud)

依赖关系

// first
public class FirstService {

  private static final Logger LOGGER = ...

  @Inject
  HttpServletRequest request;

  ....
}

// second
@ApplicationScoped
public class SecondService { .... } 
Run Code Online (Sandbox Code Playgroud)

允许@RequestScoped在字段上声明。但无法在任何地方找到它是如何工作的。

问题1:如果我指定@RequestScoped将由容器注入的字段,我会在那里获得请求范围的真实注入实例吗?

问题 2:如果我将 DI 更改为基于构造函数会怎样?在这种情况下我应该把它放在哪里@RequestScoped

@Path("hello")
public class HelloResource {

  private final FirstService service1;
  private final SecondService service2;

  @Inject
  public HelloResource(FirstService service1, SecondService service2) {
    // …
Run Code Online (Sandbox Code Playgroud)

java scope cdi java-ee-8

4
推荐指数
1
解决办法
5938
查看次数

使用 MapStruct 将抽象类映射到 DTO

我发现了很多与此相关的主题,但在我看来,所有解决方案都走向了错误的方向。

\n\n

那么...在这种情况下我该如何使用 MapStruct 映射?

\n\n

抽象类人:

\n\n
public abstract class Person implements Serializable{\n\n     private String name;\n     private String somethingToIgnore\n\n     //Getter and Setter\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

普通的映射器不能\xc2\xb4工作:

\n\n
@Mapper(componentModel = 'cdi')\npublic interface PersonMapper{\n\n    @Mapping(target = 'somethingToIgnore', ignore = 'true')\n    Person toPerson(PersonDTO source);\n\n    @InheritInverseConfiguration\n    PersonDTO toPersonDtO(Person source);\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不允许映射抽象类。我应该使用工厂方法。我尝试过,但我根本不知道这个工厂方法应该是什么样子......

\n\n

我的尝试:

\n\n
@Mapper\npublic interface PersonMapper {\n\n    PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );\n\n    Person toPerson(PersonDTO source);\n\n    PersonDTO toPersonDtO(Person source);\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n
@Mapper\npublic abstract class PersonMapper {\n\n    public static final PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );\n\n …
Run Code Online (Sandbox Code Playgroud)

java mapping dto mapstruct java-ee-8

3
推荐指数
1
解决办法
2万
查看次数

Spring Boot在不同环境下的部署

我对 Spring Boot 很陌生,我们正在开发 Spring Boot 应用程序(打包为 War 文件)。当在tomcat服务器上的较高环境中部署此war文件时,1.我们可以为war内的diff环境(可以是dev,int,uat,prod)配置application.properties吗?或者 2. 配置 application.properties 后,我们可以将其放在 tomcat 中的 conf 文件夹中,在 war 文件之外。(类似于将 context.xml 放入传统的 Web 应用程序中)并且应用程序仍然会选择它?

java spring-boot java-ee-8

2
推荐指数
1
解决办法
1万
查看次数

ConstraintValidator 两次调用 isValid() 方法

任何 Body 解释我为什么 ConstraintValidator 类的 isValid() 方法被调用两次?
例如,这是我的示例代码:

@POST
@Path("/json/dog")
@Produces("application/json")
@Consumes("application/json")
public Response getDogByJson(@ValidAnimal JsonObject jsonObject) {
      return Response.ok("ok").build();
}



@Constraint(validatedBy = {AnimalValidation.class})
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface ValidAnimal {

    String message() default "This is not valid !";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };
}   



public class AnimalValidation implements ConstraintValidator<ValidAnimal, JsonObject> {

    @Override
    public void initialize(ValidAnimal constraintAnnotation) {

    }

    @Override
    public boolean isValid(JsonObject jsonObject, ConstraintValidatorContext context) {
        System.out.println(">>>>>> : " + jsonObject);
        return true; …
Run Code Online (Sandbox Code Playgroud)

jax-rs jersey open-liberty java-ee-8 jakarta-ee

2
推荐指数
1
解决办法
352
查看次数

NullPointerException 在 @Produces 方法中获取 bean 类名

我将此类LoggerProducer注入到@Statelessbean 中以生成日志条目,如此处所述

问题是,当CustomerBean调用 时(甚至没有调用logger.info),该@Produces方法(检索 bean 类名)失败并显示NullPointerException。这段代码有什么问题?

@Named
@Singleton
public class LoggerProducer {

    @Produces
    public Logger produceLogger(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(
                   injectionPoint.getBean().getBeanClass()); // <- error here
    }   

}
Run Code Online (Sandbox Code Playgroud)

注入记录器的bean:

import org.slf4j.Logger;

@Stateless
@LocalBean
@Named
public class CustomerBean  {

    @Inject
    private Logger logger;

    ......
      logger.info("some message");
Run Code Online (Sandbox Code Playgroud)

java logback slf4j java-ee-8 jakarta-ee

2
推荐指数
1
解决办法
326
查看次数

JavaEE CDI 的 SpringBoot 替代品是什么?

我知道 JakartaEE(以前称为 JavaEE)从 JavaEE 6 开始就支持 CDI。

据我所知,SpringBoot 没有 CDI,只有 DI。

SpringBoot 是否支持 CDI(上下文和依赖注入)或提供一些替代方案?

dependency-injection cdi spring-boot java-ee-8

2
推荐指数
1
解决办法
258
查看次数

Glassfish 5.1 服务器在启用安全管理后无法启动(NoClassDefFoundError:sun/security/ssl/HelloExtension)

我在 linux (ubuntu 18.04) 上有一个 glassfish 服务器版本 5.1。我可以毫无问题地启动它,但是在通过以下方式启用安全管理员之后

asadmin --host localhost --port 4848 enable-secure-admin

服务器似乎无法永久启动。我不能stop-domainrestart-domain,虽然start-domain说有一些东西在端口 4848 上运行。所以我必须手动终止该进程。

服务器日志:

    [2020-08-01T18:02:53.647+0000] [glassfish 5.1] [SEVERE] [] [] [tid: _ThreadID=58 _ThreadName=Thread-9] [timeMillis: 1596304973647] [levelValue: 1000] [[
  java.lang.NoClassDefFoundError: sun/security/ssl/HelloExtension
    at sun.security.ssl.SSLExtension.<clinit>(SSLExtension.java:225)
    at sun.security.ssl.SSLConfiguration.getEnabledExtensions(SSLConfiguration.java:369)
    at sun.security.ssl.ClientHello$ClientHelloKickstartProducer.produce(ClientHello.java:562)
    at sun.security.ssl.SSLHandshake.kickstart(SSLHandshake.java:509)
    at sun.security.ssl.ClientHandshakeContext.kickstart(ClientHandshakeContext.java:110)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:234)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:393)
    at sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:727)
    at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:74)
    at sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1012)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at java.io.DataOutputStream.flush(DataOutputStream.java:123)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:229)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:150)
    at com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:175)
    at com.sun.jndi.toolkit.url.GenericURLContext.rebind(GenericURLContext.java:251)
    at javax.naming.InitialContext.rebind(InitialContext.java:433) …
Run Code Online (Sandbox Code Playgroud)

java glassfish java-ee-8 jakarta-ee

1
推荐指数
1
解决办法
3265
查看次数

如何将错误捕获留给子类?

public class First {
protected void method1() throws CustomException {
    int number=10/0;
    System.out.println("method 1" + number);
    throw new CustomException("Divided by zero");
}
public class Second extends First {
protected void method2() {
        method1();
    
}
public class Third extends Second {
protected void method3(){
    try {
        method2();
        }
        catch (CustomException ex)
        {
            System.out.println("Caught the exception");
            System.out.println(ex.getMessage());
        } 
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,首先抛出一个异常,我想从第三个捕获它(第二个不会处理错误)。但是第二个方法调用不会让我通过。我怎样才能解决这个问题?

java java-ee-8

1
推荐指数
1
解决办法
97
查看次数

CDI / WELD 构造函数注入最佳实践

在“单一”Jakarta-EE 8 应用程序中,我们希望将 JSF 与 CDI 结合使用。下图给出了视图和类如何相互依赖的示例:

JSF-View -> ViewController -> BeanA --> BeanA1
                                    \-> BeanA2
                                
Run Code Online (Sandbox Code Playgroud)

在本例中,ViewControlleris @Named+ @ViewScoped,所有其他 bean ( BeanA, BeanA1, BeanA2) 都是@SessionScoped。我们希望使用构造函数注入作为最佳实践。基于此,我们的类如下所示:

@Named
@ViewScoped
public class ViewController implements Serializable {

    private final BeanA bean;
    
    @Inject
    public ViewController(final BeanA bean) {
        this.bean = bean;
    }
}

@SessionScoped
public class BeanA implements Serializable {

    private final BeanA1 bean1;
    
    private final BeanA2 bean2;
    
    @Inject
    public BeanA(final BeanA1 bean1, final BeanA2 bean2) …
Run Code Online (Sandbox Code Playgroud)

java cdi weld java-ee-8 jakarta-ee

1
推荐指数
1
解决办法
796
查看次数

哪里可以下载最新的 JDK EE 而不是 glassfish

几年前,当我下载jdk ee时,得到“jdk-8u231-windows-x64.exe”文件并安装它。它创建了“jdk1.8.0_231”文件夹,我已将其用于 JAVA_HOME。

但是当我现在尝试下载最新的 JDK EE 时,我在其中找到了“glassfish”文件夹。

这是什么变化?为什么要进行此更改?

注意:我知道 Oracle 将 Java EE 提供给了 Eclipse,现在称为 Jakarta EE。但即使在 eclipse 站点或 oracle 站点中,我也只能找到 glassfish 下载,而不是 jdk ee。

请帮助我理解这一点。

谢谢

java eclipse glassfish java-ee-8 jakarta-ee

0
推荐指数
1
解决办法
557
查看次数