Javaアノテーションインジェクションの問題

1.コントローラーレイヤーでのサービスインジェクション
問題
Beanを取得せずにBeanを取得するには、2つの一般的な方法があり
ます。1。アノテーション
を介してイン
ジェクトします。2。xmlを介してアノテーションインジェクションを構成します。コントローラーレイヤーをスキャンしてから、サービスオブジェクトをインジェクトします。

   @Controller  //web层



   //@Autowired
    private RoleService roleService;// RoleService 必须先创建bean 然后在注入
 public void setRoleService(RoleService roleService) {
    
    
	  this.roleService = roleService;}

xml配置

   <bean id="roleController" class="com.itheima.controller.RoleController">  <!--不采用注解  必须有set方法无参构造 -->

       <property name="roleService" ref="roleService"></property>

    </bean>

おすすめ

転載: blog.csdn.net/fxwentian/article/details/113900147