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 {

    @Override
    public 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 {

    @Override
    public 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;
    }
    
    @Override
    public 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的配置

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

  1. @Component:主要用於定義組件,一般用於DAO上使用。
  2. @Service:主要用於定義組件,一般用於Service上用。
  3. @Repository:主要用於定義組件,一般用於Action上使用。

范例:修改xxxDAOImpl類:

package com.Spring.Dao.Imp;
import org.springframework.stereotype.Component;
import com.Spring.Dao.IAdminDAO;

@Component public class AdminDAOImpl implements IAdminDAO {

    @Override
    public 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 {

    @Override
    public 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;
    @Resource private IRoleDAO roleDao;
    @Override
    public 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實現的注入操作,整個流程都是非常簡化的,以后開發都是此類模式進行。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM