註解配置IOC
1.創建maven工程在pom.xml中添加依賴
<dependencies>
<!-- 單元測試依賴-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- 註解依賴-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
- 在bean.xml中聲明使用xmlns:context的約束
<?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="org.example"></context:component-scan>
</beans>
- 編寫持久層業務層的註解代碼
package org.example.dao;
public interface IAccountDao {
public void saveAccount();
}
package org.example.dao.impl;
import org.example.dao.IAccountDao;
import org.springframework.stereotype.Repository;
@Repository(value = "accountDao")
public class AccountDaoImpl implements IAccountDao {
public void saveAccount(){
System.out.println("保存了賬戶000");
}
}
package org.example.serice;
public interface IAccountService {
void saveAccount();
}
package org.example.serice.impl;
import org.example.dao.IAccountDao;
import org.example.serice.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
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")
public class AccountServiceImpl implements IAccountService {
@Resource(name = "accountDao")
private IAccountDao accountDao;
public void saveAccount(){
accountDao.saveAccount();
}
}
測試
public static void main( String[] args )
{
//1.獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根據id獲取Bean對象
IAccountService as = (IAccountService)ac.getBean("accountService");
as.saveAccount();
}
純註解使用
上面的例子中還是有bean.xml文件,用註解替代bean.xml文件功能
- 持久層和業務層代碼例子不變,增加一個配置類來替代bean.xml
package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 該類是一個配置類,它的作用和bean.xml是一樣的
* spring中的新註解
* Configuration
* 作用:指定當前類是一個配置類
* 細節:當配置類作爲AnnotationConfigApplicationContext對象創建的參數時,該註解可以不寫。
* ComponentScan
* 作用:用於通過註解指定spring在創建容器時要掃描的包
* 屬性:
* value:它和basePackages的作用是一樣的,都是用於指定創建容器時要掃描的包。
* 我們使用此註解就等同於在xml中配置了:
* <context:component-scan base-package="com.itheima"></context:component-scan>
* Bean
* 作用:用於把當前方法的返回值作爲bean對象存入spring的ioc容器中
* 屬性:
* name:用於指定bean的id。當不寫時,默認值是當前方法的名稱
* 細節:
* 當我們使用註解配置方法時,如果方法有參數,spring框架會去容器中查找有沒有可用的bean對象。
* 查找的方式和Autowired註解的作用是一樣的
* Import
* 作用:用於導入其他的配置類
* 屬性:
* value:用於指定其他配置類的字節碼。
* 當我們使用Import的註解之後,有Import註解的類就父配置類,而導入的都是子配置類
* PropertySource
* 作用:用於指定properties文件的位置
* 屬性:
* value:指定文件的名稱和路徑。
* 關鍵字:classpath,表示類路徑下
*/
@Configuration
@ComponentScan(value = "org.example")
public class SpringConfiguration {
}
使用Spring整合junit的配置來測試
package org.example;
import static org.junit.Assert.assertTrue;
import org.example.config.SpringConfiguration;
import org.example.dao.IAccountDao;
import org.example.serice.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Unit test for simple App.
*/
/**
* 使用Junit單元測試:測試我們的配置
* Spring整合junit的配置
* 1、導入spring整合junit的jar(座標)
* 2、使用Junit提供的一個註解把原有的main方法替換了,替換成spring提供的
* @Runwith
* 3、告知spring的運行器,spring和ioc創建是基於xml還是註解的,並且說明位置
* @ContextConfiguration
* locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下
* classes:指定註解類所在地位置
*
* 當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AppTest
{
@Autowired
private IAccountService accountService;
@Test
public void test()
{
//從sprig容器中獲取對象
// //1.獲取核心容器對象
// ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
// //2.根據id獲取Bean對象
// IAccountService as = (IAccountService)ac.getBean("accountService");
// as.saveAccount();
//因爲使用了spring測試jar包替代了junit的main方法這樣就有了spring的容器,這時候就可以直接使用對象
accountService.saveAccount();
}
}