How to use generics in spring?

user4136732

i have read lot of oline articles and couldnt find an answer and i have tried to make it correct and not able to do it.

Can anyone help,how to initialize generics in bean file and make it work?

Config File

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="collectionDemo" class="com.prashant.Collections6.CollectionDemo">
                <constructor-arg index="0" type="String" value="google"/>
                <constructor-arg index="1" type="String" value="gooogle"/>
        </bean>

    </beans>

CollectionDemo.java

public class CollectionDemo<T> {
    private T id,phoneNumber;

    public CollectionDemo(T id, T phoneNumber) {
        super();
        this.id = id;
        this.phoneNumber = phoneNumber;
    }

    public T getId() {
        return id;
    }

    public void setId(T id) {
        this.id = id;
    }

    public T getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(T phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    @Override
    public String toString() {
        return "CollectionDemo [id=" + id + ", phoneNumber=" + phoneNumber
                + "]";
    }

}

ColleactionApp.java

package com.prashanth.Collections6;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ColleactionApp {
    public static void main(String[] args) {
        ApplicationContext app=new FileSystemXmlApplicationContext
        ("/src/main/java/com/prashanth/Collections6/beans.xml");
        CollectionDemo<String> coll=(CollectionDemo<String>) app.getBean("collectionDemo");
        System.out.println(coll);
    }
}
Sotirios Delimanolis

The issue you've posted in the comments is because of a typo

<bean id="collectionDemo" class="com.prashant.Collections6.CollectionDemo">

should be

<bean id="collectionDemo" class="com.prashanth.Collections6.CollectionDemo">

assuming

package com.prashanth.Collections6;

is the correct package name.

The "issue" with generics is

<constructor-arg index="0" type="String" value="google"/>
<constructor-arg index="1" type="String" value="gooogle"/>

Spring will use the type you specified to match constructor arguments. But your class constructor's arguments are not of type String, they are of type T, which erase to Object. Either change it to

<constructor-arg index="0" type="Object" value="google"/>
<constructor-arg index="1" type="Object" value="gooogle"/>

or remove the type altogether.

Generics are a compile time concept. They do not exist at runtime. But Spring uses reflection at runtime to generate beans. There's no type argument to specify for your generic types in the bean definitions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use dynamic dispatch in Rust for trait with generics?

From Java

How to use TypeToken + generics with Gson in Kotlin

From Java

How to use generics in props in React in a functional component?

From Dev

How do I use generics with HashMap?

From Dev

How to use generics in a java method

From Dev

How to use generics

From Dev

How to use the selName function of GHC.Generics?

From Dev

how to use generics in Scala

From Dev

How to use parentheses for generics?

From Dev

How to use generics for the similar methods with different classes

From Dev

How to use multiple bounds in java generics in this case

From Dev

how to use a class with generics without having to declare the generic type

From Dev

How to use generics with multiple coupled objects?

From Dev

How to use generics as params?(Swift 2.0)

From Dev

Swift how to use 'self' as generics

From Dev

How to use Jackson with nested Generics?

From Dev

How to use java generics with restTemplate

From Dev

How do I correctly use generics in this case?

From Dev

How to Use Generics in a map of Comparator to avoid warnings

From Dev

How to use Jackson with nested Generics?

From Dev

How to use generics

From Dev

How to use generics of generics in java interfaces

From Dev

how to use generics in Scala

From Dev

How to use generics in a method declaration

From Dev

How to use generics for the similar methods with different classes

From Dev

How to use generics to combine these two methods Into one?

From Dev

How to use generics in argument need class

From Dev

How to use Nim's `of` operator with generics?

From Dev

How to use generics properly for a Holder

Related Related

HotTag

Archive