1. 程式人生 > 其它 >SSM框架學習之Spring(二)全部程式碼

SSM框架學習之Spring(二)全部程式碼

技術標籤:ssmspringssm

SSM框架學習之Spring(二)全部程式碼

在這裡插入圖片描述

AccountDaoImpl

package com.itheima.dao.impl;
/**
 * @author :班奈
 * @version 1.0
 * @date 2021/2/4 15:52
 */
import com.itheima.dao.IAccountDao;
import org.springframework.stereotype.Repository;

/**
 * 賬戶的持久層實現類
 */
@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao
    {
public void saveAccount(){ System.out.println("儲存了賬戶1"); } }

AccountDaoImpl2

package com.itheima.dao.impl;
/**
 * @author :班奈
 * @version 1.0
 * @date 2021/2/4 15:52
 */
import com.itheima.dao.IAccountDao;
import org.springframework.stereotype.Repository;

/**
 * 賬戶的持久層實現類
 */
@Repository(
"accountDao2") public class AccountDaoImpl2 implements IAccountDao { public void saveAccount(){ System.out.println("儲存了賬戶2"); } }

IAccountDao

package com.itheima.dao;
/**
 * @author :班奈
 * @version 1.0
 * @date 2021/2/4 15:55
 */
/**
 * 賬戶的持久層介面
 */
public interface IAccountDao {
/** * 模擬儲存賬戶 */ void saveAccount(); }

AccountServiceImpl

package com.itheima.service.impl;
/**
 * @author :班奈
 * @version 1.0
 * @date 2021/2/4 16:00
 */
import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * 賬戶的業務層實現類
 *
 * 曾經XML的配置:
 *  <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
 *        scope=""  init-method="" destroy-method="">
 *      <property name=""  value="" | ref=""></property>
 *  </bean>
 *
 * 用於建立物件的
 *      他們的作用就和在XML配置檔案中編寫一個<bean>標籤實現的功能是一樣的
 *      Component:
 *          作用:用於把當前類物件存入spring容器中
 *          屬性:
 *              value:用於指定bean的id。當我們不寫時,它的預設值是當前類名,且首字母改小寫。
 *      Controller:一般用在表現層
 *      Service:一般用在業務層
 *      Repository:一般用在持久層
 *      以上三個註解他們的作用和屬性與Component是一模一樣。
 *      他們三個是spring框架為我們提供明確的三層使用的註解,使我們的三層物件更加清晰
 *
 *
 * 用於注入資料的
 *      他們的作用就和在xml配置檔案中的bean標籤中寫一個<property>標籤的作用是一樣的
 *      Autowired:
 *          作用:自動按照型別注入。只要容器中有唯一的一個bean物件型別和要注入的變數型別匹配,就可以注入成功
 *                如果ioc容器中沒有任何bean的型別和要注入的變數型別匹配,則報錯。
 *                如果Ioc容器中有多個型別匹配時:
 *          出現位置:
 *              可以是變數上,也可以是方法上
 *          細節:
 *              在使用註解注入時,set方法就不是必須的了。
 *      Qualifier:
 *          作用:在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用。但是在給方法引數注入時可以(稍後我們講)
 *          屬性:
 *              value:用於指定注入bean的id。
 *      Resource
 *          作用:直接按照bean的id注入。它可以獨立使用
 *          屬性:
 *              name:用於指定bean的id。
 *      以上三個注入都只能注入其他bean型別的資料,而基本型別和String型別無法使用上述註解實現。
 *      另外,集合型別的注入只能通過XML來實現。
 *
 *      Value
 *          作用:用於注入基本型別和String型別的資料
 *          屬性:
 *              value:用於指定資料的值。它可以使用spring中SpEL(也就是spring的el表示式)
 *                      SpEL的寫法:${表示式}
 *
 * 用於改變作用範圍的
 *      他們的作用就和在bean標籤中使用scope屬性實現的功能是一樣的
 *      Scope
 *          作用:用於指定bean的作用範圍
 *          屬性:
 *              value:指定範圍的取值。常用取值:singleton prototype
 *
 * 和生命週期相關 瞭解
 *      他們的作用就和在bean標籤中使用init-method和destroy-methode的作用是一樣的
 *      PreDestroy
 *          作用:用於指定銷燬方法
 *      PostConstruct
 *          作用:用於指定初始化方法
 */
//@Service("accountService")
//@Scope("prototype")
@Component("accountService")
public class AccountServiceImpl implements IAccountService {

//    @Autowired
//    @Qualifier("accountDao1")
   /* @Resource(name = "accountDao2")
    private IAccountDao accountDao = null;

    @PostConstruct
    public void  init(){
        System.out.println("初始化方法執行了");
    }

    @PreDestroy
    public void  destroy(){
        System.out.println("銷燬方法執行了");
    }*/
  //@Autowired
  //@Qualifier("accountDao2")
    @Resource(name="accountDao2")
    private IAccountDao accountDao ;
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

IAccountService

package com.itheima.service;
/**
 * @author :班奈
 * @version 1.0
 * @date 2021/2/4 15:57
 */
/**
 * 賬戶業務層的介面
 */
public interface IAccountService {

    /**
     * 模擬儲存賬戶
     */
    void saveAccount();
}

Client

/**

  • @author :班奈
  • @version 1.0
  • @date 2021/2/4 15:43
    */
package com.itheima.ui;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 模擬一個表現層,用於呼叫業務層
 */

public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //1.獲取核心容器物件
       ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根據id獲取Bean物件
        IAccountService as  = (IAccountService)ac.getBean("accountService");
       // IAccountDao ad = (IAccountDao)ac.getBean("accountDao2");
        //System.out.println(as);
        //System.out.println(ad);
        as.saveAccount();
    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

<!--告知spring在建立容器時要掃描的包,配置所需要的標籤不是在beans的約束中,而是一個名稱為
context名稱空間和約束中-->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>