如何用注解簡化SSH框架


一、簡化代碼第一步,刪除映射文件,給實體類加上注解

@Entity        //聲明當前類為hibernate映射到數據庫中的實體類
@Table(name="news")        //聲明table的名稱
public class News {
    @Id        //聲明此列為主鍵,作為映射對象的標識符
    /**
     *  @GeneratedValue注解來定義生成策略
     *  GenerationType.TABLES 當前主鍵的值單獨保存到一個數據庫的表中
     *  GenerationType.SEQUENCE  利用底層數據庫提供的序列生成標識符
     *  GenerationType.IDENTITY 采取數據庫的自增策略
     *  GenerationType.AUTO 根據不同數據庫自動選擇合適的id生成方案,這里使用mysql,為遞增型
     */
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    
    @Column(name="title",nullable=false)
    private String title;
    
    @Column(name="content",nullable=false)
    private String content;
    
    @Column(name="begintime",nullable=false)
    private Date begintime;
    
    @Column(name="username",nullable=false)
    private String username;

    public News() {
        super();
    }
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    
    public Date getBegintime() {
        return begintime;
    }
    public void setBegintime(Date begintime) {
        this.begintime = begintime;
    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    
    
}

修改applicationContext.xml文件

<!-- 刪掉這段代碼 -->
<!-- <property name="mappingResources">
  <list>
    <value>news/entity/News.hbm.xml</value>
  </list>
</property> -->    
        
<!-- 加上這段代碼 -->
<!-- 掃描實體類包,解析實體類的注解 -->
<property name="packagesToScan">
  <list>
    <!-- 這里value值添實體類所在的包 -->
    <value>news.entity</value>
  </list>
</property>

二、用注解代替<bean>注入:

1.其實,注解本身做不了任何事情,和XML一樣,只起到配置的作用,主要在於背后強大的處理器,其中就包括了<context:annotation-config/>配置項里面的注解所使用的處理器,所以配置了<context:component-scanbase-package="">之后,便無需再配置<context:annotation-config>

2.@Autowired

在java代碼中使用@Autowired@Resource注解方式進行裝配 ,這兩個注解的區別是:@Autowired默認按類型裝配@Resource默認按名稱裝配,當找不到名稱匹配的bean才會按類型裝配
@Autowired一般裝配在set方法之上,也可以裝配在屬性上邊。

@Autowired是根據類型進行自動裝配的。如果當Spring上下文中存在不止一個所要裝配類型的bean時,就會拋出BeanCreationException異常;我們可以使用@Qualifier配合@Autowired來解決這些問題

3.@Resource

@Resource的作用相當於@Autowired

@Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略

4.使用Spring注解完成Bean的定義
以上我們介紹了通過@Autowired@Resource來實現在Bean中自動注入的功能,下面我們將介紹如何注解Bean,從而從XML配置文件中完全移除Bean定義的配置。
@Component:只需要在對應的類上加上一個@Component注解,就將該類定義為一個Bean了:

 使用@Component注解定義的Bean,默認的名稱(id)是小寫開頭的非限定類名。如這里定義的Bean名稱就是userDaoImpl。你也可以指定Bean的名稱:
@Component("userDao")
@Component是所有受Spring管理組件的通用形式,Spring還提供了更加細化的注解形式:

@Repository@Service@Controller,它們分別對應存儲層Bean業務層Bean,和展示層Bean。目前版本)中,這些注解與@Component的語義是一樣的,完全通用,在Spring以后的版本中可能會給它們追加更多的語義。所以,我們推薦使用@Repository@Service@Controller來替代@Component

三、使用<context:component-scan />讓Bean定義注解工作起來

 


免責聲明!

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



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