Spring:方法注入查找如何使用它?

我可以使用方法注入查找 - 使用实体类吗?.I使用Spring + JPA + Hibernate。 这允许将一个原型bean注入到一个singleton bean中。这对于实体bean也是可能的吗?A是原型scoped bean。我想将A(@Entity)放入类为B(例如DAO)的scope = singleton.Thanks

@Entity
public class A(){

    private String name;
    private String surname;

    ...//get and set
}//A

public interface DAO{
   public void method();
}//DAO

public class DAOImpl implements DAO{
  private A object_a;

  public void method(){
     //In this method I use everytime a new istance of A
  }//method
}//DAOImpl

你可以使用@Embedded来包含你的子bean,并在你的sql中使用。

@Entity
public class User(){

    private String name;

    @Embedded
    private Address address;

    @Bean(scope=DefaultScopes.PROTOTYPE)
    public User() {
    }

    ...//get and set
}


@Entity
public class Address(){
    private String name;
    ...//get and set
}

public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "select u from users u where u.address.name = :addressName")
    List<Blog> findUserByAddress(@Param("addressName") String addressName);
}
链接地址: http://www.djcxy.com/p/22691.html

上一篇: Spring: Method Injection Lookup How to use it?

下一篇: Injecting dependencies in a prototype bean