Created
May 11, 2019 01:24
-
-
Save fankaidev/e7850ac6313b44002cec88b2c15c060c to your computer and use it in GitHub Desktop.
复现泛型注入失败
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.fankai; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.DependsOn; | |
import org.springframework.stereotype.Service; | |
import javax.annotation.PostConstruct; | |
/** | |
* @author fankai | |
*/ | |
@Service | |
@DependsOn("client2") | |
public class DemoGenericAutowireService { | |
public static class Client1 {} | |
public static class Client2 {} | |
public interface RpcClient<T> {} | |
public interface ConfigurableRpcClient<T> extends RpcClient<T> {} | |
public static class PooledZkRpcClient<T> implements ConfigurableRpcClient<T> { | |
T t; | |
} | |
@Configuration | |
public static class Conf { | |
@Bean | |
public RpcClient<Client1> client1() { | |
return new PooledZkRpcClient<>(); | |
} | |
@Bean | |
@DependsOn("client1") | |
public RpcClient<Client2> client2() { | |
return new PooledZkRpcClient<>(); | |
} | |
} | |
// 去掉注释就能看到autowire失败 | |
// @Autowired(required = false) | |
// private ConfigurableRpcClient<?> manager; | |
@Autowired | |
private RpcClient<Client1> instance; | |
@PostConstruct | |
public void init() { | |
System.out.println("inited"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment