0

2 つのプロジェクトを含む基本的なアプリを開発しました。1 つ目はデータ レイヤーで、2 つ目は Web アプリ (mvc プロジェクト) です。

データ層には以下が含まれます

@Entity
@Table(name="users")
public class User implements Serializable {
.....
}

public interface UserBase {
.....
  }

  @Repository
  public interface UserRepository extends JpaRepository <User, Long>{
  }



   @Service
   public class UserImpl implements UserBase {  
   private final UserRepository repository; 
   @Autowired
   public UserImpl(UserRepository repository) { 
   this.repository = repository;
    ......
   }


  @RunWith(SpringJUnit4ClassRunner.class)  
  @ContextConfiguration("Config/applicationContext.xml")
  public class testcase1 {

    @Autowired
private UserImpl userImpl;

   @Test
   public void test() {
   ..... 
   UserImpl.save(user);
   .....
   }

Web アプリ @Controller public class ManageUser {

@Autowired
UserImpl userImpl;

@RequestMapping("/addUser/")
public ModelAndView addUser() {
UserImpl.save(user);
.....
}
}

単体テストを実行すると、コードは成功し、エラーは見つかりませんでした。しかし、データ レイヤーからの出力 jar を spring mvc Web アプリの lib フォルダーに配置し、ビルド バスのデータ レイヤーへの参照を追加すると、エラー メッセージ
Injection of autowired dependencies failed; が表示されます。ネストされた例外は org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.my.domin.impl.UserImpl です

注: Tomcatを使用しています。流れる設定を使用します:

    <jpa:repositories base-package="com.sanatech.repository" />
    <context:component-scan base-package="com.my.repository"/>
    <context:component-scan base-package="com.my.domin.impl"/>
    <context:component-scan base-package="com.my.domin"/>
    <context:component-scan base-package="com.my"/>
    <context:component-scan base-package="com.my.manageuser.controller"/>
    <tx:annotation-driven/>
    <context:annotation-config />

編集

単体テストを新しいプロジェクトに分離し、ビルド バスのデータ レイヤー jar への参照を追加すると、同じ例外が見つかりました。

4

1 に答える 1

0

この例外がテストケースから発生していないことを確認してください:

public class testcase1 {

    @Autowired
private UserImpl userImpl;

@Service アノテーションで明示的に Bean 名を設定し、この名前を @Autowired で使用して、正しい構成を確認できます。

于 2013-07-09T14:12:45.940 に答える