我有clientDAO接口和clientDaoImpl类;我在clientDAO中声明了方法并在clientDaoImpl中定义了方法。我还在spring-servlet.xml(它是spring-config文件)中定义了mysql数据库连接作为数据源。
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
@SessionAttributes
@Controller
public class clientRegistrationController {
@Autowired
private clientDAO clientdao;
@SessionAttributes
@Controller
public class ClientRegistrationController {
@RequestMapping( value="/registration",method = RequestMethod.POST)
public @ResponseBody
String client_registration(@RequestParam(value = "date_of_registration") Date date_of_registration)
// here i want to get pojo object and call method insert method which is
// defined in DAO implement class.
return " Registered successfully";
}
@RequestMapping("/registration")
public ModelAndView showContacts() {
String message = "Hello World, Spring MVC @ Javatpoint";
return new ModelAndView("client_registration", "message",message);
}
}
我收到了以下错误:
SEVERE: Servlet [spring] in web application [/SpringTiles] threw load() exception
java.lang.Error: Unresolved compilation problems:
Autowired cannot be resolved to a type
clientDAO cannot be resolved to a type
我在javatpoint.com中使用了/ SpringTiles代码 这是我的客户端:
package dao;
import com.javatpoint.form.Client_Registration;
public interface clientDAO {
void insertData(Client_Registration patient);
}
答案 0 :(得分:0)
如果没有创建任何类型的bean,则会出现此异常。 确保在spring-servlet.xml文件中添加了context:component-scan标记。 例如
并检查是否在Dao上添加了@Repository注释
答案 1 :(得分:0)
希望您在clientDAO类中添加了@Repository。 Spring无法在加载时找到它。 检查您用于创建bean的配置(xml或java)。 共享完整的错误日志以及相关的代码片段(此处为clientDAO和配置)有助于更快地解决错误。
答案 2 :(得分:0)
你应该这样注射DAO:
<bean id="dao" class="com.elgarnaoui.ma.dao.AgentDao"></bean>
<bean id="metier" class="com.elgarnaoui.ma.metier.MetierAgent">
<property name="dao" ref="dao"></property>
</bean>