基于注解的IOC
- 注解的配置
-
- 導入jar包,具體需要的jar包如下
- 用于建立bean對象
- XML配置
- 注入資料
- 用于改變作用範圍
- Spring的新注解
注解的配置
導入jar包,具體需要的jar包如下
- maven配置
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
用于建立bean對象
- @componet
- 作用:就相當于配置了一個bean标簽
- 它出現的位置:類上面
- 屬性:value。含義是指bean的id。當不寫時,它有預設值。預設值是目前類的短名,首字母小寫
- 由此注解衍生的三個注解
- @Controller 一般用于表現層
- @Service 一般用于業務層
- @Repository 一般用于持久層
- 他們和@Componet的作用以及屬性一摸一樣
@Service
public class CustomerServiceImpl implements ICustomerService{
private ICustomerDao customerDao;
public void setCustomerDao(ICustomerDao customerDao) {
this.customerDao = customerDao;
}
public void saveCustomer() {
System.out.println("業務層調用持久層");
}
}
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">
<!-- 配置自動掃描器 -->
<context:component-scan base-package="fujuspring.com.fuju.service"></context:component-scan>
</beans>
注入資料
- @Autowired
- 作用:自動按照類型注入
- 當我們使用注解注入的時候,set方法就不是必須的了
- 如果注入的bean在容器中類型不唯一時,它會把變量名稱作為bean的id在容器中查找,找到後也會注入成功
- 如果沒有找到一緻的bean的id,則報錯
- @Qualifier
- 作用:在自動按照類型注入的基礎上,再按照bean的id注入,它在類成員注入資料時,不能獨立使用,但是在給方法的形參注入資料時,可以獨立使用
- 屬性
- value:用于指定bean的id
- @Resource
- 作用:直接按照bean的id注入
- 屬性:
- name:用于指定bean的id
- 屬性:
- 作用:直接按照bean的id注入
- 以上三個注解都是用于注入其他bean類型的。用于注入基本類型和String類型需要使用Value
- @Value
- 作用:用于注入基本類型和String類型資料。可以借助spring的el表達式讀取properties檔案中的配置
- 屬性:value:用于指定要注入的資料
== java代碼 ==
@Repository("customerDaoImpl")
public class CustomerDaoImpl implements ICustomerDao {
public void saveCustomer() {
System.out.println("持久層儲存了客戶");
}
}
@Service("customerServiceImpl")
public class CustomerServiceImpl implements ICustomerService{
@Value("XXX")
private String name;
@Autowired
// @Qualifier("customerDaoImpl")
private ICustomerDao customerDao;
public void saveCustomer() {
System.out.println("業務層調用持久層"+name);
customerDao.saveCustomer();
}
}
用于改變作用範圍
- @Scope
- 用于改變bean的作用範圍
- 屬性:value:用于指定範圍的值
- 取值和xml中的scope屬性的取值是一樣的。singleton prototype request session globalsession
== java代碼如下 ==
@Service("customerServiceImpl")
@Scope("prototype")
public class CustomerServiceImpl implements ICustomerService{
@Value("XXX")
private String name;
@Autowired
// @Qualifier("customerDaoImpl")
private ICustomerDao customerDao;
public void saveCustomer() {
System.out.println("業務層調用持久層"+name);
customerDao.saveCustomer();
}
}
Spring的新注解
- @ComponentScan
- 配置自動掃描
- @Bean
- 作用:它是把方法的傳回值存在spring容器中,該注解有個屬性有個name用于指定bean的ID,當不指定時,它有預設值,預設值是方法的名稱
- @Configuration
- 作用:把目前類看做spring的配置類
- @Import
- 作用:導入其他配置類
- @PropertySource
- 作用:加載配置檔案如jdbc.properties,可以解析占位符