Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 替换<;构造函数参数>;带Spring注释_Java_Spring_Generics_Annotations_Code Injection - Fatal编程技术网

Java 替换<;构造函数参数>;带Spring注释

Java 替换<;构造函数参数>;带Spring注释,java,spring,generics,annotations,code-injection,Java,Spring,Generics,Annotations,Code Injection,有没有办法用注释替换构造函数arg 我有一个构造器: public GenericDAOImpl(Class<T> type) { this.type = type; } public GenericDAOImpl(类类型){ this.type=type; } 我需要在我的门面上注入这一点: @Inject private GenericDAO<Auto, Long> autoDao; @Inject 专用通用道自动道; 问题是我不知道如何在costru

有没有办法用注释替换构造函数arg

我有一个构造器:

public GenericDAOImpl(Class<T> type) {
    this.type = type;
}
public GenericDAOImpl(类类型){
this.type=type;
}
我需要在我的门面上注入这一点:

@Inject
private GenericDAO<Auto, Long> autoDao;
@Inject
专用通用道自动道;
问题是我不知道如何在costructor中传递参数值

先谢谢你

[更多信息] 我试图解释我的问题

<bean id="personDao" class="genericdao.impl.GenericDaoHibernateImpl">
        <constructor-arg>
            <value>genericdaotest.domain.Person</value>
        </constructor-arg>
</bean>

genericdaotest.domain.Person
我想只使用注释来转换代码。
有人可以解释怎么做?

更新:恐怕你不可能做到你想做的事。无法从注入点的参数获取构造函数参数。一个
FactoryBean
将是第一个可以查看的地方,但是它没有被赋予注入点元数据。(需要注意的是:CDI很容易涵盖该案例)

原始答案:(如果从外部配置类型,这可能仍然有效)

只需在构造函数上使用
@Inject
。但请注意,spring不赞成构造函数注入。考虑设定器/场注入。

然而,在您的情况下,您可能有多个类型为
Class
的bean。如果是这种情况,您可以使用
@Resource(name=“beanName”)

从以下文件:

可注入构造函数用@Inject注释,并接受零个或多个依赖项作为参数@每个类最多只能应用一个构造函数

   @Inject ConstructorModifiersopt SimpleTypeName(FormalParameterListopt)  
   Throwsopt ConstructorBody

我认为单独使用
@Inject
是没有帮助的,您还必须使用
@Qualifier
注释

以下是Spring参考的相关部分:

如果我理解正确,您将不得不使用
@Qualifier
机制

如果您使用,您可能可以内联执行,如下所示:

@Repository
public class DaoImpl implements Dao{

    private final Class<?> type;

    public DaoImpl(@Qualifier("type") final Class<?> type){
        this.type = type;
    }

}
public DaoImpl(
    @Value("#{ systemProperties['dao.type'] }")
    final Class<?> type){
    this.type = type;
}
@Configuration
public class DaoConfiguration {
    @Bean
    public GenericDAO<Person> personDao() {
        return new GenericDaoHibernateImpl(Person.class);
    }
}

在构造函数中具有该类型的选项是:

public abstract class GenericDAO<T> {
    private Class<T> persistentClass;

    public GenericDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
            .getGenericSuperclass()).getActualTypeArguments()[0];
    }
...
}
公共抽象类GenericDAO{
私有类persistentClass;
公共GenericDAO(){
this.persistentClass=(Class)((ParameteredType)getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
...
}
但每个T必须有特定的不同实现

优点是您不需要将t type作为参数传递。

Spring的方法可能会有所帮助。如果您创建一个Java类,该类使用注释
@Configuration
@Bean
简单地定义您的Bean,那么它可能如下所示:

@Repository
public class DaoImpl implements Dao{

    private final Class<?> type;

    public DaoImpl(@Qualifier("type") final Class<?> type){
        this.type = type;
    }

}
public DaoImpl(
    @Value("#{ systemProperties['dao.type'] }")
    final Class<?> type){
    this.type = type;
}
@Configuration
public class DaoConfiguration {
    @Bean
    public GenericDAO<Person> personDao() {
        return new GenericDaoHibernateImpl(Person.class);
    }
}
@配置
公共类配置{
@豆子
公共通用dao personDao(){
返回新的GenericDaoHibernateImpl(Person.class);
}
}

确保扫描了
DaoConfiguration
类(通常通过
@ComponentScan
),并且将在Spring上下文中为您创建适当的DAO对象。bean将有方法的名称,在本例中是
personDao
,因此您可以使用名称
personDao
按名称注入它,或者如果类型是
GenericDAO

我已经使用@inject,但我也需要传递类type的值。有办法吗?或者我必须使用xml方法?那个值是多少?你从哪里得到的?@Inject-private-GenericDAO-autoDao;当初始化GenericDAO时,我需要传递类类型的值