-1

I currently developing an application using Spring Data Neo4j. This is the first time I use Spring framework.

However, the autowired annotation is not working, I always get null. Here is the codes:

    public class CreateDiaryTransaction extends Transaction {
    @Autowired
    DirayRepository repository;

    @Override
    public Object perform(Map<String, Object> parameters) {
        // TODO Auto-generated method stub
        Diary diary = (Diary) DiaryFactory.getInstance().create(parameters);
        repository.save(diary);
        return diary.toJsonRepre();
    }
}

Then I test it in a unit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/applicationContext.xml" })
public class CreateDiaryTransactionTest {

    @Test
    public void test() {
        Transaction transaction = new CreateDiaryTransaction();
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("title", "Test");
        parameters.put("weather", "sun");
        parameters.put("mood", "happy");
        parameters.put("html", "<a href=\"javascript:void(0)\">link</a>");
        parameters.put("images", new ArrayList<Image>());
        try {
            transaction.execute(parameters);
        } catch (Exception e) {
            fail("Exception occur");
        }
    }
}

When I runs this, I got null object exception. However, when I place the repository directly in the unit test, it works fine. Here is another test that works:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/applicationContext.xml" })
public class DiaryRepositoryTest {
    @Autowired
    DirayRepository repository;

    @Test
    @Transactional
    public void testSaveDiary() {
        Diary diary = (Diary) DiaryFactory.getInstance().create(
                new HashMap<String, Object>());
        repository.save(diary);

        Diary retrivedDiary = repository.findOne(diary.getGraphId());
        assertEquals(diary, retrivedDiary);
    }

}

And here is my application context:

<context:annotation-config />
<neo4j:config storeDirectory="target/data/pamela"
    base-package="com.bond.pamela.domain" />
<neo4j:repositories base-package="com.bond.pamela.persistence" />
<tx:annotation-driven mode="aspectj" />

<bean id="conversionService"
    class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean
                class="com.bond.pamela.domain.valueObject.converter.ImageToJsonConverter" />
            <bean
                class="com.bond.pamela.domain.valueObject.converter.JsonToImageConverter" />
        </set>
    </property>
</bean>

I guess I somehow put the repository outside the spring context, but how can I get it wirte? Thx

Junbang Huang
  • 1,927
  • 19
  • 26

2 Answers2

1

You need following in your application context

<context:component-scan base-package="packagename.package" />

Here package name is package in which you have all your classes with annotations.

Have a look at this post for more :

Spring MVC: Difference between context:annotation-config vs context:component-scan

swapyonubuntu
  • 1,952
  • 3
  • 25
  • 34
0

Make sure you have written @Repository or @Component annotation on the implementation of DirayRepository and you have written component-scan in applicationContext.xml

sample code for DirayRepository

@Repository
  public class DirayRepositoryImpl implements DirayRepository {
   void save() { ...}

  }
hard coder
  • 5,449
  • 6
  • 36
  • 61