gpt4 book ai didi

spring - 单例和 @Autowired 返回 NULL

转载 作者:IT老高 更新时间:2023-10-28 13:47:54 25 4
gpt4 key购买 nike

我有一个存储库管理器来管理我的存储库。我有 @Autowired 来实例化我的属性,但它们始终为空。 bean 在我的 xml 中正确配置。有什么原因吗?

public class RepositoryManager {

private static RepositoryManager instance;

private RepositoryManager()
{
}

public static RepositoryManager Instance()
{
if(instance == null)
instance = new RepositoryManager();

return instance;
}

@Autowired
private IUserRepository userRepository;

@Autowired
private IRoleRepository roleRepository;

@Autowired
private IAssetRepository assetRepository;

public IUserRepository getUserRepository() {
return userRepository;
}

public void setUserRepository(IUserRepository userRepository) {
this.userRepository = userRepository;
}

public IRoleRepository getRoleReposttory() {
return roleRepository;
}

public void setRoleReposttory(IRoleRepository roleRepository) {
this.roleRepository = roleRepository;
}

public IAssetRepository getAssetRepository() {
return assetRepository;
}

public void setAssetRepository(IAssetRepository assetRepository) {
this.assetRepository = assetRepository;
}
}

dao.xml

    <!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.cisco.badges.data.*" />

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<property name="annotatedClasses">
<list>
<value>com.cisco.badges.data.domain.User</value>
<value>com.cisco.badges.data.domain.Role</value>
<value>com.cisco.badges.data.domain.Asset</value>
<value>com.cisco.badges.data.domain.UserRole</value>
<value>com.cisco.badges.data.domain.UserRole$UserRolePK</value>
<value>com.cisco.badges.data.domain.UserAsset</value>
<value>com.cisco.badges.data.domain.UserAsset$UserAssetPK</value>
</list>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>

<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

userrepository.java

    @Repository("userRepository")
public class UserRepository extends
BaseRepository<User, Long> implements
IUserRepository {

@Autowired
public UserRepository(SessionFactory sessionFactory) {
super(sessionFactory);
}

最佳答案

我自己也遇到了这个问题。问题是当你这样做时

new RepositoryManager();

在 Instance() 中,您没有使用 Spring 创建 RepositoryManager,因此您的实例没有发生依赖注入(inject)(没有 Autowiring )。

解决方案是取消 Instance() 单例模式。如果你想坚持单例,那就这样做

@Component
@Scope(value = "singleton")
public class RepositoryManager {
...
}

然后,无论您需要存储库管理器,只需 Autowiring 对它的引用(假设调用 bean 也由 Spring 管理!)

@Autowired
private RepositoryManager repositoryManager = null;

关于spring - 单例和 @Autowired 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5010219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com