【发布时间】:2016-10-28 13:35:37
【问题描述】:
这不是this 问题的重复。所以请不要因为“重复”的原因而关闭它..
我正在尝试将 bean 自动装配到 java 类中。我的问题是 playerDAO 仍然为空并且没有被初始化。
mvc-dispatcher-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="ngdemo"/>
<mvc:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
PlayerDAO.java
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class PlayerDAO {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public Player getPlayerByUsername(String username) {
//some code
}
}
LoginRestService.java
@Path("/v1")
@Controller
public class LoginRestService {
@Autowired
private PlayerDAO playerDAO;
@Path("/login")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response authenticateUser(@FormParam("username") String username,
@FormParam("password") String password, @Context UriInfo request) {
playerDAO.getPlayerByUsername(username);
//NPE, playerDAO here is null when I'm trying to access it
}
}
我错过了什么?
此处提供完整代码:https://github.com/Ferenus/hestalis
【问题讨论】:
标签: java spring spring-mvc