我有一个通用的抽象模板类.我想如果我创建特定于类型的生成器,我可以直接在泛型类中注入一些DAO服务.但我不能.
为什么?我怎么能解决这个问题呢?
abstract class MyView<T> {
@Inject
MyDao<T> dao;
//some more template methods that make use of the dao
void someMethod() {
dao.use();
}
}
class CustomerView extends MyView<Customer> {
//javax.enterprise.inject.AmbiguousResolutionException: Ambigious resolution
}
class DaoManager {
@Produces
MyDao<Customer> getDaoCustomer() {
return DaoFactory.make(Customer.class);
}
@Produces
MyDao<Product> getDaoProduct() {
return DaoFactory.make(Product.class);
}
}
Run Code Online (Sandbox Code Playgroud)
当我注射例如@Inject MyDao<Customer> dao;它它完美地工作.但不是泛型......
也许我错过了什么,但是没有提供Scope.PROTOTYPE, Scope.SINGLETON静态引用的类吗?
或者我总是必须使用非类型安全的字符串作为范围?
@Scope("prototype")
@Scope("singleton")
Run Code Online (Sandbox Code Playgroud) 我正在收集分隔符文件夹中的所有依赖库,mvn package如下所示:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven.copy.plugin}</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
问题:这也包括<scope>test</scope>库.我该如何排除这些库?
我正在使用spring它RestTemplate来执行GET查询.
如何在每个请求上自动将任何请求和响应数据记录到日志文件中?
我有一个spring-boot申请.在run文件夹下,还有一个额外的配置文件:
dir/config/application.properties
应用程序启动时,它使用文件中的值并将它们注入:
@Value("${my.property}")
private String prop;
Run Code Online (Sandbox Code Playgroud)
问题:如何触发重新加载这些@Value属性?我希望能够application.properties在运行时更改配置,并@Value更新字段(可能通过调用/reload应用程序内的servlet来触发更新).
但是怎么样?
我想将bean的getter作为函数传递.调用该函数时,应调用getter.例:
public class MyConverter {
public MyConverter(Function f) {
this.f = f;
}
public void process(DTO dto) {
// I just want to call the function with the dto, and the DTO::getList should be called
List<?> list = f.call(dto);
}
}
public class DTO {
private List<String> list;
public List<String> getList() { return list; }
}
Run Code Online (Sandbox Code Playgroud)
这有可能与Java 8?
dao.create()在测试方法时,我有一个想要模拟的调用.但我错过了一些东西,因为我还在接受NPE.这有什么不对?
class MyService {
@Inject
private Dao dao;
public void myMethod() {
//..
dao.create(object);
//
}
}
Run Code Online (Sandbox Code Playgroud)
如何模拟dao.create()调用?
@RunWith(PowerMockRunner.class)
@PrepareForTest(DAO.class)
public void MyServiceTest {
@Test
public void testMyMethod() {
PowerMockito.mock(DAO.class);
MyService service = new MyService();
service.myMethod(); //NPE for dao.create()
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用spring JdbcTemplate来执行sql查询:
JdbcTemplate template = new JdbcTemplate(ds);
template.execute(sqlInsert); //returns void
Run Code Online (Sandbox Code Playgroud)
我怎么能得到受影响的行数,因为该execute()方法返回void?
我想创建与日期相关的日志文件log4j2:
<RollingFile name="APP" fileName="application-%d{yyyy-MM-dd}.log" />
Run Code Online (Sandbox Code Playgroud)
结果日志文件名:application-%d{yyyy-MM-dd}.log,不替换时间戳.为什么?
我有两个来自框架的注释。我经常在同一个字段上使用这两个注释。因此,我试图创建一个包含两者的“组合”注释。
但我不知道这是否可能:
现有的注释(我无法控制):
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiParam {
String name() default "";
}
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiModelProperty {
String name() default "";
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建的自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@ApiParam
@ApiModelProperty
public @interface SwaggerParam {
String name() default "";
}
Run Code Online (Sandbox Code Playgroud)
结果:注解不适用于注解类型。
问:有机会吗?
java ×10
spring ×5
annotations ×1
cdi ×1
java-8 ×1
java-ee ×1
junit ×1
lambda ×1
log4j ×1
log4j2 ×1
logging ×1
maven ×1
mockito ×1
powermock ×1
scope ×1
spring-data ×1
spring-mvc ×1
spring-rest ×1