因此,我通过Spring来访问我的数据库,并获取基于不同属性的用户。随着@Restcontroller我得到的数据。我创建了扩展CrudRepository一个UserRepository。
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
Iterable<User> findByNachname(String nachname);
Iterable<User> findByRolle(Rolle rolle);
Optional<User> findByBenutzername(String benutzername);
@Transactional
String deleteByBenutzername(String benutzername);
}
我用@Autowire得到UserRepo的一个实例,在我的控制器级
@RestController
public class LoginController {
@Autowired
private UserRepository userRepository;
}
这工作完全正常中的所有控制器我有。但现在,当我在另一个班级尝试同样的userRepository实例为null。
public class Authentificator {
@Autowired
private UserRepository userRepository;
}
该Authentificator和LoginController中都没有在同一个包。但他们两人不在同一个包中UserRepo。
你必须确保Authentificator也是一个Spring bean -我的意思是,你必须像@Component还是@Service注释它。这一步,你还必须经过“获得” Authentificator从春天实例,而不是与实例它的新关键字。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句