日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

08-spring学习-annotation配置

發布時間:2025/3/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 08-spring学习-annotation配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用annotation配置注入關系

為了更好的解釋此類存在的意義,下面通過一段習慣性的開發進行問題的描述,例如:

現在有一個IAdminService服務層,這個服務層要調用的是IAdminDAO和IRoleDAO兩個數據層操作,于是定義如下:

?

范例:定義數據層操作。

package com.Spring.Dao;public interface IAdminDAO {public boolean findLogin();}

package com.Spring.Dao;public interface IRoleDAO {public boolean findAll(); }

實現層:

package com.Spring.Dao.Imp;import com.Spring.Dao.IAdminDAO;public class AdminDAOImpl implements IAdminDAO {@Overridepublic boolean findLogin() {System.out.println("[IAdminDAO]public boolean findLogin()");return false;} }

package com.Spring.Dao.Imp;import com.Spring.Dao.IRoleDAO;public class RoleDAOImpl implements IRoleDAO {@Overridepublic boolean findAll() {System.out.println("[IRoleDAO]public boolean findAll()");return false;} }

最早的時候,這兩個數據層的類一定要編寫工廠類,單向現在不用編寫工廠類了。

下面直接在appllicationContext.XML文件里面定義。

<bean id="adminDaoImpl" class="com.Spring.Dao.Imp.AdminDAOImpl"></bean><bean id="roleDaoImpl" class="com.Spring.Dao.Imp.RoleDAOImpl"></bean>

隨后所有的數據層都要交給業務層操作,那么下面定義業務層操作:

?

package com.Spring.Service;public interface IAdminService {public boolean login(); }

下面定義實現層:

package com.Spring.Service.Impl;import com.Spring.Dao.IAdminDAO; import com.Spring.Dao.IRoleDAO; import com.Spring.Service.IAdminService;public class IAdminServiceImpl implements IAdminService {private IAdminDAO adminDao;private IRoleDAO roleDao;//setter是給依賴注入關系使用的。public void setAdminDao(IAdminDAO adminDao) {this.adminDao = adminDao;}public void setRoleDao(IRoleDAO roleDao) {this.roleDao = roleDao;}@Overridepublic boolean login() {this.adminDao.findLogin();this.roleDao.findAll();return false;} }

定義applicationContext.xml文件,配置彼此關系。

<bean id="adminDaoImpl" class="com.Spring.Dao.Imp.AdminDAOImpl"></bean><bean id="roleDaoImpl" class="com.Spring.Dao.Imp.RoleDAOImpl"></bean><bean id="adminServiceImpl" class="com.Spring.Service.Impl.IAdminServiceImpl"><property name="adminDao" ref="adminDaoImpl"></property><property name="roleDao" ref="roleDaoImpl"></property></bean>

代碼寫到此處,可以說最原始的操作完成了,所有的關系通過applicationContext.xml文件完成了,

package com.Spring.Demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Spring.Service.IAdminService; import com.Spring.Service.Impl.IAdminServiceImpl;public class TestAdmin {public static void main(String args[]){ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext2.xml");IAdminService ia=ctx.getBean("adminServiceImpl",IAdminServiceImpl.class);ia.login();} }

執行結果:

此時最直觀感受是,避免了工廠類的編寫,的確節約了代碼,但是反過來,問題又出現了,如果一個項目存在幾百個DAO或Service。這種寫法太累了。

?

在Spring之中,可以利用annotation完全簡化以上操作。

范例:要增加新的命名空間。

在配置文件中勾選上context命名空間:

會發現配置代碼中多了以下代碼:

范例:設置annotation的支持包

<context:annotation-config/><context:component-scan base-package="com.Spring"/>

表示在“com.Spring”下所有程序支持annotation的配置。而在spring里面,針對于組件的annotation的配置

只提供三個注解定義(這三個注解定義的作用都一樣,只是單詞不同):

  • @Component:主要用于定義組件,一般用于DAO上使用。
  • @Service:主要用于定義組件,一般用于Service上用。
  • @Repository:主要用于定義組件,一般用于Action上使用。
  • 范例:修改xxxDAOImpl類:

    package com.Spring.Dao.Imp; import org.springframework.stereotype.Component; import com.Spring.Dao.IAdminDAO;@Component public class AdminDAOImpl implements IAdminDAO {@Overridepublic boolean findLogin() {System.out.println("[IAdminDAO]public boolean findLogin()");return false;} }

    ?和:

    package com.Spring.Dao.Imp;import org.springframework.stereotype.Component;import com.Spring.Dao.IRoleDAO; @Component public class RoleDAOImpl implements IRoleDAO {@Overridepublic boolean findAll() {System.out.println("[IRoleDAO]public boolean findAll()");return false;} }

    現在如果使用了注解定義組件,那么名稱默認情況下就是類名稱的結構形式:

    比如:AdiminDAOImpl:則訪問組件的名稱就是“adminDaoImpl”。

    ?

    范例:在service層上使用注解:

    相比之前,這里的兩個adminDao和roleDao的setter方法取消掉了,并且之前配置里面與setter配合使用,將會把如下的實例化作用的<bean>配置也去掉了,

    <bean id="adminDaoImpl" class="com.Spring.Dao.Imp.AdminDAOImpl"></bean><bean id="roleDaoImpl" class="com.Spring.Dao.Imp.RoleDAOImpl"></bean><bean id="adminServiceImpl" class="com.Spring.Service.Impl.IAdminServiceImpl"><property name="adminDao" ref="adminDaoImpl"></property><property name="roleDao" ref="roleDaoImpl"></property></bean>

    ?

    取而代之的是注解:@Resource,來代替上面的這兩個屬性,表示此為注入的資源

    package com.Spring.Service.Impl; import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.Spring.Dao.IAdminDAO; import com.Spring.Dao.IRoleDAO; import com.Spring.Service.IAdminService; @Service public class IAdminServiceImpl implements IAdminService { @Resource  //表示此為注入的資源private IAdminDAO adminDao;@Resourceprivate IRoleDAO roleDao;@Overridepublic boolean login() {this.adminDao.findLogin();this.roleDao.findAll();return false;} }

    此時AdminServiceImpl類的引用名稱是:IAdminServiceImpl。因此使用的使用,使用getBean操作里面的bean名稱也是這個,如下:

    測試操作:

    package com.Spring.Demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Spring.Service.IAdminService; import com.Spring.Service.Impl.IAdminServiceImpl;public class TestAdmin {public static void main(String args[]){ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext2.xml");IAdminService ia=ctx.getBean("IAdminServiceImpl",IAdminServiceImpl.class);ia.login();} }

    現在發現:利用annotation實現的注入操作,整個流程都是非常簡化的,以后開發都是此類模式進行。

    ?

    總結

    以上是生活随笔為你收集整理的08-spring学习-annotation配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。