Spring 自动装配无法工作
Spring autowired can not work
我目前正在使用 Spring Data Neo4j 开发应用程序。这是我第一次使用Spring框架。
但是,自动装配注解不起作用,我总是得到 null。这是代码:
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();
}
}
然后我在单元测试中测试它:
@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");
}
}
}
当我运行它时,我得到了空对象异常。但是,当我将存储库直接放在单元测试中时,它工作正常。这是另一个有效的测试:
@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);
}
}
这是我的应用程序上下文:
<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>
我想我以某种方式将存储库放在了 spring 上下文之外,但是我怎样才能得到它呢?谢谢
您需要在您的应用程序上下文中关注
<context:component-scan base-package="packagename.package" />
这里的包名是你所有的 类 都带有注释的包。
看看这个 post 了解更多:
Spring MVC: Difference between context:annotation-config vs context:component-scan
确保你已经在 DirayRepository
的实现上写了 @Repository
或 @Component
注释并且你已经写了 component-scan
applicationContext.xml
DirayRepository
的示例代码
@Repository
public class DirayRepositoryImpl implements DirayRepository {
void save() { ...}
}
我目前正在使用 Spring Data Neo4j 开发应用程序。这是我第一次使用Spring框架。
但是,自动装配注解不起作用,我总是得到 null。这是代码:
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();
}
}
然后我在单元测试中测试它:
@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");
}
}
}
当我运行它时,我得到了空对象异常。但是,当我将存储库直接放在单元测试中时,它工作正常。这是另一个有效的测试:
@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);
}
}
这是我的应用程序上下文:
<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>
我想我以某种方式将存储库放在了 spring 上下文之外,但是我怎样才能得到它呢?谢谢
您需要在您的应用程序上下文中关注
<context:component-scan base-package="packagename.package" />
这里的包名是你所有的 类 都带有注释的包。
看看这个 post 了解更多:
Spring MVC: Difference between context:annotation-config vs context:component-scan
确保你已经在 DirayRepository
的实现上写了 @Repository
或 @Component
注释并且你已经写了 component-scan
applicationContext.xml
DirayRepository
@Repository
public class DirayRepositoryImpl implements DirayRepository {
void save() { ...}
}