关键词:
【中文标题】如何自动装配泛型类型[重复]【英文标题】:How to autowire generic type [duplicate] 【发布时间】:2016-05-26 09:25:50 【问题描述】:我想在 FcSolrServiceImpl 类中 @autowire SolrCrudRepository,但它在 NPE 中提供。以下是我的实现。
FcSolrServiceImpl.java
public class FcSolrServiceImpl<K>
@Autowired(required = true)
SolrCrudRepository<K, String> repository;
public void saveProduct(K input)
repository.save(input); // Getting null pointer in repository
Product.java
@SolrDocument(solrCoreName = "core1")
public class Product
Product()
;
@Field
@Id
private String id;
// getter setter hashcode equals toString methods
FcIndexSyncApplication.java
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class FcIndexSyncApplication extends SpringBootServletInitializer
public static void main(String[] args)
SpringApplication.run(FcIndexSyncApplication.class, args);
FcSolrServiceImpl<Product> fpsi = new FcSolrServiceImpl<Product>();
Product input = new Product();
input.setID("abc");
fpsi.saveProduct(input);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
return application.sources(FcIndexSyncApplication.class);
@Bean
public SolrTemplate solrTemplate() throws Exception
HttpSolrServer httpSolrServer = new HttpSolrServer("$spring.data.solr.host");
return new SolrTemplate(httpSolrServer, "core1");
【问题讨论】:
看起来您创建了一个 SolrCrudRepository 以自动装配到您的应用程序上下文中。 【参考方案1】:您正在使用new
创建FcSolrServiceImpl
。 FcSolrServiceImpl<Product> fpsi = new FcSolrServiceImpl<Product>();
这样,Spring 对该对象一无所知。您必须从 Spring 上下文中获取 bean,才能使用依赖注入:
ApplicationContext ctx = SpringApplication.run(FcIndexSyncApplication.class, args);
FcSolrServiceImpl myBean = ctx.getBean(FcSolrServiceImpl.class);
myBean....
【讨论】:
【参考方案2】:问题是当你像FcSolrServiceImpl<Product> fpsi = new FcSolrServiceImpl<Product>();
这样初始化服务实例时,它不在Spring上下文中。结果,SolrCrudRepository<K, String> repository
被初始化为空值。
由于您使用的是组件扫描,请使用 @Service
注释标记您的 FcSolrServiceImpl
。
要正确初始化它,请使用以下 sn-p:
final ApplicationContext context= SpringApplication.run(FcIndexSyncApplication.class, args);
final FcSolrServiceImpl<Product> fpsi= context.getBean(PersonServiceImpl.class);
Product input = new Product();
input.setID("abc");
fpsi.saveProduct(input);
【讨论】:
使用构造函数自动装配泛型类型 [Spring 4.2.5]
】使用构造函数自动装配泛型类型[Spring4.2.5]【英文标题】:Autowiringgenerictypeswithconstructor[Spring4.2.5]【发布时间】:2016-08-1703:11:57【问题描述】:我是Spring新手,正在尝试像本文http://www.ibm.com/developerworks/library/j-genericdao/中那样实... 查看详情
如何仅通过泛型类型 T 创建实例 [重复]
】如何仅通过泛型类型T创建实例[重复]【英文标题】:HowtocreateaninstancejustbygenerictypeT[duplicate]【发布时间】:2017-07-2712:28:46【问题描述】:我想通过定义泛型类的类型来创建一个实例publicabstractclassBase<T>privatefinalTgenericTypeObjec... 查看详情
如何使用泛型避免类型安全警告[重复]
】如何使用泛型避免类型安全警告[重复]【英文标题】:Howtoavoidtypesafetywarningswithgenerics[duplicate]【发布时间】:2019-12-1808:02:19【问题描述】:我正在编写自定义迭代器,但在我的Java代码中看到不同的警告。这是我的代码:importjav... 查看详情
如何将泛型类型参数限制为 System.Enum [重复]
】如何将泛型类型参数限制为System.Enum[重复]【英文标题】:HowtolimitagenerictypeparametertoSystem.Enum[duplicate]【发布时间】:2011-02-2115:23:25【问题描述】:可能的重复:Anyoneknowagoodworkaroundforthelackofanenumgenericconstraint?CreateGenericmethodconstrai... 查看详情
.net 自动装配解决加载重复装配
】.net自动装配解决加载重复装配【英文标题】:.netautomaticassemblyresolveloadsduplicateassembly【发布时间】:2017-02-2818:16:01【问题描述】:我有一个依赖于库的可执行文件。我还有一个“加载器”应用程序,它将可执行文件加载到单独... 查看详情
c#中泛型类型的默认值[重复]
...值。此参数未初始化传递。我需要在课堂上模仿这一点。如何找到类型T的默认值?如何修改此问题以使其 查看详情
springboot自动装配流程源码分析
...呢?为什么SpringBoot可以自动装配呢?SpringBoot自动装配是如何实现的呢?SpringBoot入口写过SpringBoot 查看详情
Spring自动装配空指针异常[重复]
】Spring自动装配空指针异常[重复]【英文标题】:Springautowirenullpointerexception[duplicate]【发布时间】:2017-10-2202:15:25【问题描述】:xml配置-<beanid="DS"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driver 查看详情
如何测试泛型变量是不是属于 AnyObject [重复]
】如何测试泛型变量是不是属于AnyObject[重复]【英文标题】:HowtotestwhethergenericvariableisoftypeAnyObject[duplicate]如何测试泛型变量是否属于AnyObject[重复]【发布时间】:2016-09-1711:59:29【问题描述】:在Swift3中,我不再能够检查泛型变量... 查看详情
弹簧自动装配不起作用[重复]
】弹簧自动装配不起作用[重复]【英文标题】:Springautowiringdoesn\'twork[duplicate]【发布时间】:2013-10-2913:07:35【问题描述】:我正在尝试将messageSourcebean注入到我的一个组件类中。这是beanxml的一部分:<context:annotation-config/><co... 查看详情
如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 Spring 服务中
】如何在SpringBoot中实现通用JPA存储库-它可以自动装配到任何实体/类类型的Spring服务中【英文标题】:HowtoimplementGenericJPARepositoryinSpringBoot-Whichcanbeautowiredintospringservicesforanyentity/classtype【发布时间】:2016-02-0408:07:27【问题描述】... 查看详情
C#中的泛型,使用变量的类型作为参数[重复]
...<T>(Guidguid,ITransactiontransaction)whereT:IGloballyIdentifiable;我如何通 查看详情
Spring Security @PreAuthorize 使用 SpEL 语言访问自动装配的 bean [重复]
】SpringSecurity@PreAuthorize使用SpEL语言访问自动装配的bean[重复]【英文标题】:SpringSecurity@PreAuthorizeaccesstoautowiredbeanusingSpELlanguage[duplicate]【发布时间】:2019-04-0209:49:50【问题描述】:我想使用SpEL语言在SpringSecurity@PreAuthorize下访问我... 查看详情
Spring中@autowired注释的用途是啥。术语自动装配是啥意思[重复]
】Spring中@autowired注释的用途是啥。术语自动装配是啥意思[重复]【英文标题】:whatistheuseof@autowiredannotationinSpring.Whatismeantbythetermautowired[duplicate]Spring中@autowired注释的用途是什么。术语自动装配是什么意思[重复]【发布时间】:2013... 查看详情
自动装配失败:不是托管类型
】自动装配失败:不是托管类型【英文标题】:Autowiringfails:NotanmanagedType【发布时间】:2012-12-1317:09:58【问题描述】:我的文凭项目有一个大问题,如果你们能帮助我,我会非常高兴!我做了一个Maven多模块项目并有3个“核心项... 查看详情
自动装配有哪些局限性?
自动装配的局限性是:重写:你仍需用和配置来定义依赖,意味着总要重写自动装配。基本数据类型:你不能自动装配简单的属性,如基本数据类型,String字符串,和类。模糊特性:自动装配不如显式装配精确,如果有可能,建... 查看详情
自动装配有哪些局限性?
自动装配的局限性是:重写:你仍需用和配置来定义依赖,意味着总要重写自动装配。基本数据类型:你不能自动装配简单的属性,如基本数据类型,String字符串,和类。模糊特性:自动装配不如显式装配精确,如果有可能,建... 查看详情
spring基础:自动装配
1.自动装配 1.1byType 1.1.1根据类型自动匹配,若当前没有类型可以注入或者存在多个类型可以注入,则失败。必须要有对于的setter方法publicclassPerson{publicStringname;publicintage;publicStringgetName(){returnname;}publicvoid 查看详情