Java Spring未满足与Spring数据的依赖关系
在SecurityServiceImpl中注入此角色时,我会遇到此错误 org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“securityServiceImpl”的bean时出错:通过字段“roleRepo”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“com.tk.emd.ui.service.RoleRepo”的合格bean可用:至少需要1个符合autowire候选条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 我的spring上下文文件Java Spring未满足与Spring数据的依赖关系,java,spring,dependency-injection,spring-data-jpa,Java,Spring,Dependency Injection,Spring Data Jpa,在SecurityServiceImpl中注入此角色时,我会遇到此错误 org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“securityServiceImpl”的bean时出错:通过字段“roleRepo”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“com.tk.emd.ui.se
@Service
@Component
public class SecurityServiceImpl {
@Autowired
RoleRepo roleRepo;
@PostConstruct
public void init() {
System.out.println(roleRepo);
}
public SecurityServiceImpl (){
System.out.println("SecurityServiceImpl created");
}
}
org.hibernate.dialen.oracle10galent
线
假的
创造
我的角色课
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<!-- Create DataSource Bean -->
<beans:bean id="dbDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName"
value="java:comp/env/jdbc/EmdDS" />
</beans:bean>
<jee:jndi-lookup id="dbDataSource" jndi-name="EmdDS"
expected-type="javax.sql.DataSource" />
<!-- using JEE namespace for lookup -->
<!-- <jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/MyLocalDB" expected-type="javax.sql.DataSource"
/> -->
<context:component-scan
base-package="com.example.jpa.hibernate" />
<!-- Hibernate 3 Annotation SessionFactory Bean definition -->
<beans:bean id="hibernate3AnnotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<beans:property name="dataSource" ref="dbDataSource" />
<beans:property name="annotatedClasses">
<beans:list>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
</beans:prop>
<beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
@实体
@表(name=“A_角色”)
@NamedQuery(name=“Role.findAll”,query=“从角色f中选择f”)
公共类角色模型{
私有静态最终长serialVersionUID=1L;
@身份证
@SequenceGenerator(name=“ROLE\u ID\u GENERATOR”,sequenceName=“ROLE\u SEQ”,allocationSize=1,initialValue=1)
@GeneratedValue(策略=GenerationType.SEQUENCE,generator=“角色\u ID\u生成器”)
私人长id;
私有字符串描述;
私有字符串名称;
//到FrtUserRole的双向多对一关联
@OneToMany(mappedBy=“role”,orphanRemoving=true)
private Set userRoles=new LinkedHashSet();
公共角色(){
}
公共字符串getDescription(){
返回此.description;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getName(){
返回此.name;
}
公共void集合名(字符串名){
this.name=名称;
}
公共集合getUserRoles(){
返回用户角色;
}
public void setUserRoles(Set userRoles){
this.userRoles=userRoles;
}
@抑制警告(“未选中”)
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
@凌驾
公共字符串toString(){
返回名称;
}
}
首先需要知道错误所说的,让我们翻译错误所说的:
例如:
@Entity
@Table(name = "A_ROLE")
@NamedQuery(name = "Role.findAll", query = "SELECT f FROM Role f")
public class Role extends AbstractModel {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "ROLE_ID_GENERATOR", sequenceName = "ROLE_SEQ", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ROLE_ID_GENERATOR")
private Long id;
private String description;
private String name;
// bi-directional many-to-one association to FrtUserRole
@OneToMany(mappedBy = "role", orphanRemoval = true)
private Set<UserRole> userRoles = new LinkedHashSet<UserRole>();
public Role() {
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
@SuppressWarnings("unchecked")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return name;
}
}
也就是说,尝试注入一个未定义的bean,其原因是所需的bean未在Spring上下文中定义,您会收到此错误消息
NoSuchBeanDefinitionException: No qualifying bean of type 'com.tk.emd.ui.service.RoleRepo'
您所需的bean不存在于正确注释为bean(@Component、@Respository、@Service等)的所需bean的上下文中,该bean可能是在Spring未扫描的包中定义的
因此,正如我们所看到的,在上下文文件配置中没有对角色的bean id进行定义。当使用我们称之为
InstantRepository
的内容时,您不能使用注释@组件
。您只需创建一个简单的界面即可:
expected at least 1 bean which qualifies as autowire candidate.
public interface RoleRepo扩展了JpaRepository{
@查询(“根据ro.name从角色ro订单中”)
列出getRoles();
}
Spring将自动检测到它必须使用代理创建此接口的实现,以执行所需的行为。也,确保您的包仍在被扫描。类和接口是否都存在于同一个包中?当您使用
@Repository
而不是@Component
注释RoleRepo时会发生什么情况?@SB当我使用Repository而不是Component时,错误消息未更改。@kann这些类不是相同的包,但我正在使用tag同时显示您的角色实体代码,确保它确实包含@EntityI尝试使用@Component自动扫描roleRepo,但错误未更改。在上下文文件上定义所需的bean,如下所示:lazy init将有助于IoC容器在首次请求时而不是在启动时创建bean实例。请提供您的类包:)
expected at least 1 bean which qualifies as autowire candidate.
public interface RoleRepo extends JpaRepository<Role, Long> {
@Query("from Role ro order by ro.name")
List<Role> getRoles();
}