@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public List<User> getUsers() {
return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
}
}
我希望这个类autowire进入其他类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao; //here is the error
@Override
public void addUser(User user) {
}
@Override
public List<User> getUsers() {
return null;
}
}
控制器:
@Controller
public class UserController {
@Autowired
UserService userService;
String message = "This should be a list of users";
@RequestMapping("/user")
public ModelAndView showMessage() {
ModelAndView modelAndView = new ModelAndView("user");
modelAndView.addObject("message", message);
return modelAndView;
}
}
哪里可以出问题?我应该向您展示更多文件吗?谢谢。 编辑:好的,这是我的session-factory.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-3.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:8080/****</prop>
<prop key="hibernate.connection.username">****</prop>
<prop key="hibernate.connection.password">****</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.User</value>
</list>
</property>
</bean>
</beans>
-class User是简单的POJO,用hibernate映射。也许web.xml可以帮助我们:)
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
MVC-servlet的dispatcher.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我没有任何applicationContext.xml文件,也许这可能是问题所在,你能帮帮我吗? 我正在添加项目和问题的屏幕截图:http://screenshot.cz/75QVH/
答案 0 :(得分:6)
@Repository
<context:component-scan base-package="controller" />
未被@Service
按照您向我们展示的内容进行搜索(扫描)。但我们需要查看每个类的包。 Spring会依赖于您告诉您要查看的@Controller
,@Repository
,@Component
和{{1}}。
了解组件扫描在春天的工作原理。
答案 1 :(得分:0)
不要在打印bean之前打印@Autowired。否则,将发现找不到类型的豆类
@Autowired
private ProjectRepository projectRepository;