Skip to content

Instantly share code, notes, and snippets.

@fankaidev
Created May 11, 2019 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fankaidev/e7850ac6313b44002cec88b2c15c060c to your computer and use it in GitHub Desktop.
Save fankaidev/e7850ac6313b44002cec88b2c15c060c to your computer and use it in GitHub Desktop.
复现泛型注入失败
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