开发者

Singleton and @Autowired returning NULL

开发者 https://www.devze.com 2023-02-11 04:58 出处:网络
I have a repository manager that manages my repositories.I have the @Autowired to instantiate my properties, but they are always null.The beans are correctly configured in my xml.Any reason why?

I have a repository manager that manages my repositories. I have the @Autowired to instantiate my properties, but they are always null. The beans are correctly configured in my xml. Any reason why?

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);
    }


I just ran into this myself. The problem is that when you do

new RepositoryManager();

in Instance(), you are not using Spring to create RepositoryManager, and thus the dependency injection isn't happening for your instance (no autowiring).

The solution is to do away with the Instance() singleton pattern. If you want to insist on a Singleton, then do this

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

Then wherever you need the repository manager, just autowire in a reference to it (assuming the calling bean is also managed by Spring!)

@Autowired
private RepositoryManager repositoryManager = null;


Please make sure that you have the following in your config:

<context:annotation-config />

<context:component-scan base-package="name.of.your.package"></context:component-scan>

If you have it then post your configuration xml


There is actually a very elegant way to have your cake and eat it, i.e., have a JVM singleton that's also Spring-managed. Say you have a pure java singleton with an autowired bean like this:

public final class MySingletonClass{
  private static MySingletonClass instance;

  public static MySingletonClass getInstance(){
    if(instance==null){
      synchronized{
        if(instance==null){
          instance = new MySingletonClass();
        }
      }
    }
    return instance;
  }

  @Autowired
  private SomeSpringBean bean;

  // other singleton methods omitted
}

You can force Spring to manage this singleton simply by adding in your application context the following line:

<bean class="com.mypackage.MySingletonClass" factory-method="getInstance"/>

Now your singleton will have an instance of SomeSpringBean autowired (if available in the context).

Moreover, this is a 'fix' for the typical problem with Spring singleton beans that are not truly JVM singletons because they get instantiated by Spring. Using the pattern above enforces JVM level singleton, i.e., compiler enforced singleton, together with container singleton.


The reason this is happening is because of the static method Instance()

you are creating the POJO outside of the spring context.

you can fix this, by adding <context:spring-configured /> to your configuration, and then annotating RepositoryManager with @Configurable

0

精彩评论

暂无评论...
验证码 换一张
取 消