gpt4 book ai didi

java - Spring @Autowired 实例为 null

转载 作者:行者123 更新时间:2023-12-01 10:00:13 25 4
gpt4 key购买 nike

我目前正在尝试设置一个 REST 服务 Spring 应用程序,并实现了一个模型及其 Controller + dao 和服务来与 MySQL 数据库通信。

enter image description here

现在我的AccountController是一个带有@RestController注释的bean,而AccountService是一个@Component bean。 AccountService 持有一个 @Autowire'd AccountDao 成员,而 AccountController 持有一个 @Autowire'd AccountService 成员。

dataAccess.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/fashion" />
<property name="username" value="root" />
<property name="password" value="fashion" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>de/appsiting/fashion/model/Account.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<bean id="accountDao" class="de.appsiting.fashion.persistence.AccountDao" />
</beans>

但是,Account.hbm.xml 呈红色,表示应用程序未找到它。因此我的第一个问题是,为什么它不从资源文件夹中查找 .hbm.xml

serviceBeans.xml填充如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<task:annotation-driven/>

<bean id="hibernateStringEncryptor" class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
<property name="registeredName">
<value>strongHibernateStringEncryptor</value>
</property>
<property name="algorithm">
<value>PBEWithMD5AndTripleDES</value>
</property>
<property name="password">
<value>jasypt</value>
</property>
</bean>

<bean id="accountService" class="de.appsiting.fashion.service.AccountService">
<property name="accountDao" ref="accountDao" />
</bean>

<!-- *** TX LAYER -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="tx.advice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>

<!-- *** AOP -->
<aop:config>
<aop:pointcut id="accountServiceMethods" expression="execution(* de.appsiting.fashion.service.AccountService.*(..))" />

<aop:advisor advice-ref="tx.advice" pointcut-ref="accountServiceMethods" />
</aop:config>
</beans>

现在,当运行我的AccountTest时,accountService为空,因此测试失败:

import de.appsiting.fashion.service.AccountService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Test;


/**
* Created by mak on 4/22/16.
*/
public class AccountTest {
private static Logger log = Logger.getLogger(AccountTest.class);

@Autowired
private AccountService accountService;

@Test
public void testCreateAccount() throws Exception {
Account account = new Account();
account.setUsername("testfashion");
account.setPassword("testfashion");
account.setEmail("test@appsiting");

accountService.addAccount(account);
}
}

为什么会发生这种情况?

最佳答案

您应该使用SpringJUnit4ClassRunner为了初始化应用程序上下文,以便依赖注入(inject)能够运行。

另一个选择是使用 Mockito ,恕我直言,这可以避免很多麻烦,并且您可以获得使用模拟的好处。

关于java - Spring @Autowired 实例为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869028/

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