在JPA @Entity和Spring @ Component / @ Resource / @ Configurable之间发生冲突

时间:2015-07-20 09:33:47

标签: spring hibernate spring-mvc

我想通过JPA @Entity自动添加一个通过@Component/@Resource/@Configurable注释的类,所以我添加了Spring的@Component @Entity @Table public class Employee{ @Autowired TestService testService; ... } 注释,但它不允许我自动加载该类。

是否需要其他注释?。

示例是:

TestService

使用@Service注释班级$("#cols").on('mouseenter',cols,function(){ cols.resizable({ maxHeight: 20, minHeight: 20, distance: 5, handles: 'e', start: function() { console.log("I've started!"); }, stop: function() { console.log("I've stopped!"); } }); });

2 个答案:

答案 0 :(得分:1)

您可以轻松地执行此操作,创建构造函数并将注入的Service作为参数传递:

@Component
@Entity
@Table
public class Employee{

    Employee(TestService testService){

        //Do some work      
    }
   ...
}

现在调用者类..

@Service
public class CallerClass{

   @Autowired   
   TestService testService;

   Employee employee =new Employee(testService);
}

答案 1 :(得分:-1)

您需要使用@Configurable

@Entity
@Table
@Configurable
public class Employee{

   @Autowired
   TestService testService;
   ...
}

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable