将Spring XML配置更改为@Configuration

时间:2017-02-18 19:43:11

标签: java spring

我的spring应用程序已经通过xml stye配置了。我正在尝试使用@Configuration类来更改它。

我的应用程序使用另一个项目作为maven库。我有一个服务,用@Named注释,该服务由库中的另一个服务使用。

@Named("userDetailsService")
public class UserDetailsServiceImpl extends AbstractServiceImpl implements UserDetailsService {

@Inject
private UserService userService;

@Override
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    return userService.getByUserName(username);
 }
}



@Named("userService")
public class UserServiceImpl extends BaseDaoServiceImpl<User, UserDao> implements UserService {

@Inject
private AuthorityService authorityService;

@Inject
private UserAuthorityService userAuthorityService;

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public User getByUserName(String username) {

    return dao.getByUserName(username);
}

@Override
public List<User> getUserWithHasAuthority(String authorityName) {
    return dao.getUserWithHasAuthority(authorityName);
}

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public User insert(User user) {

    user.setEnabled(true);
    super.insert(user);

    Authority authority = authorityService.getByName("ROLE_USER");

    UserAuthority userAuthority = new UserAuthority();
    userAuthority.setAuthority(authority);
    userAuthority.setUser(user);
    userAuthorityService.insert(userAuthority);

    return user;
 }
}

在我的新@Configuration类

@Configuration
@ComponentScan(basePackages = {"com.mylibrary","com.myapp"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
public class ServiceTestConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
 }

}

这是我的测试方法:

@Test
public void test() {
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("admin", "admin");
    authenticationManager.authenticate(authenticationToken);
}

它在userDetailsS​​ervice.Both中存在的userService属性上给出了空指针异常。@ Named。

Thnx的帮助

测试类:

@ContextConfiguration(classes = {ServiceTestConfiguration.class, DataSourceConfiguration.class, SecurityConfiguration.class})
@Transactional
public class AbstractTest {
}


@RunWith(SpringJUnit4ClassRunner.class)
public class ServiceTest extends AbstractTest {

@Inject
private AuthenticationManager authenticationManager;

@Test
public void test() {
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("admin", "admin");
    authenticationManager.authenticate(authenticationToken);
 }
}

0 个答案:

没有答案