[英]MyBatis + Spring mapping, nullpointerexception on interface
I am trying to integrate mybatis into my spring application. 我正在尝试将mybatis集成到我的spring应用程序中。 I added the mapping configuration, the interface for the mapping and also the xml which has the sql queries.
我添加了映射配置,映射接口以及具有sql查询的xml。
Here is my config: 这是我的配置:
Spring xml config: Spring xml配置:
<context:annotation-config transaction-manager="transactionManager"/>
<annotation-driven />
... some other sql datasource configuration here ...
<!-- Mybatis Spring setup -->
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mysql://localhost/appdb"/>
<beans:property name="username" value="root"/>
<beans:property name="password" value=""/>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation" value="WEB-INF/mybatis/sqlmap-config.xml" />
</beans:bean>
<!-- scan for mappers and let them be autowired -->
<beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<beans:property name="basePackage" value="com.project.persistence" />
</beans:bean>
sqlmap-config.xml: sqlmap-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<typeAliases>
<typeAlias type="com.project.domain.User" alias="user"/>
</typeAliases>
</configuration>
User class: 用户类别:
package com.project.domain;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 3647233284813657927L;
private String id;
private String name = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
I am using a custom authentication provider where I would like to use the sql query which gives me back that simple user: 我正在使用自定义身份验证提供程序,我想在其中使用sql查询,该查询使我回到那个简单的用户:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
UserService us = new UserService(); // HERE IS THE PROBLEM ! its always null, maybe not autowired?
User user = us.getUser(name);
System.out.println("User: " + user);
boolean isAuthenticated = false;
//... do the rest of the authentication
}
}
Here is the UserMapper.xml: 这是UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.project.persistence.UserMapper">
<resultMap id="result" type="user">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<select id="getUser" parameterType="String" resultType="User">
SELECT id, name
FROM bm_users
WHERE name=#{username};
</select>
<select id="getAllUser" parameterType="int" resultMap="result">
SELECT id,name
FROM bm_users;
</select>
<insert id="saveUser" parameterType="user">
INSERT INTO bm_users (id,name)
VALUE (#{id},#{name})
</insert>
<update id="updateUser" parameterType="user">
UPDATE bm_users
SET
name = #{name},
where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM bm_users
WHERE id = #{id}
</delete>
</mapper>
And here is the UserService.java: 这是UserService.java:
@Service
public class UserService {
@Autowired
private UserMapper usermapper;
public User getUser(String name) {
return usermapper.getUser(name);
}
}
So I got a nullpointerexception when the code reaches "User user = us.getUser(name);" 因此,当代码到达“ User user = us.getUser(name);”时,我得到了一个nullpointerexception。 in my CustomAuthenticationProvider:
在我的CustomAuthenticationProvider中:
java.lang.NullPointerException
com.project.service.UserService.getUser(UserService.java:16)
com.project.authentication.CustomAuthenticationProvider.authenticate(CustomAuthenticationProvider.java:27)
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:105)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
So it seems to me somehow its not getting autowired. 所以在我看来,它并没有自动接线。
Any suggestion what should I try? 有什么建议我应该尝试什么?
Thanks 谢谢
OK, so I could almost solve this. 好的,所以我几乎可以解决这个问题。 I had to modify my code a little bit.
我不得不修改我的代码。 It seems the problem was with my service implementation.
看来问题出在我的服务实现上。
I followed this one to solve it: http://www.javacodegeeks.com/2014/02/building-java-web-application-using-mybatis-with-spring.html 我遵循了这个解决方法: http : //www.javacodegeeks.com/2014/02/building-java-web-application-using-mybatis-with-spring.html
In the tutorial they are using "private StudentService studentService;" 在本教程中,他们使用的是“私有StudentService studentService”。 in the controller but I had to use StudentServiceImpl to get it work.
在控制器中,但我必须使用StudentServiceImpl使其正常工作。
However, other interesting thing. 但是,其他有趣的事情。 This only works from a @Controller class, and not working from my @Component CustomAuthenticationProvider.
这仅适用于@Controller类,不适用于我的@Component CustomAuthenticationProvider。 Is it how it should work?
它应该如何工作? How can I make it work inside my AuthenticationProvider?
如何使其在我的AuthenticationProvider中工作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.