Spring 學習(十三)——通過註解配置 Bean
在 classpath 中掃描元件
•元件掃描(component scanning): Spring 能夠從 classpath 下自動掃描, 偵測和例項化具有特定註解的元件.
•特定元件包括:
–@Component: 基本註解, 標識了一個受 Spring 管理的元件
–@Respository: 標識持久層元件
–@Service: 標識服務層(業務層)元件
–@Controller: 標識表現層元件
•對於掃描到的元件, Spring 有預設的命名策略: 使用非限定類名, 第一個字母小寫. 也可以在註解中通過 value 屬性值標識元件的名稱
•當在元件類上使用了特定的註解之後, 還需要在 Spring 的配置檔案中宣告 <context:component-scan> :
–base-package 屬性指定一個需要掃描的基類包,Spring 容器將會掃描這個基類包裡及其子包中的所有類.
–當需要掃描多個包時, 可以使用逗號分隔.
–如果僅希望掃描特定的類而非基包下的所有類,可使用 resource-pattern 屬性過濾特定的類,示例:
–
–
–
–<context:include-filter> 子節點表示要包含的目標類
–<context:exclude-filter
–<context:component-scan> 下可以擁有若干個 <context:include-filter> 和 <context:exclude-filter> 子節點
•<context:include-filter> 和 <context:exclude-filter> 子節點支援多種型別的過濾表示式:
testObject.java
package com.hzyc.spring.bean.annotation; import org.springframework.stereotype.Component; /** * @author xuehj2016 * @Title: TestObject * @ProjectName Spring * @Description: TODO * @date 2018/12/18 20:30 */ @Component public class TestObject { }
userController.java
package com.hzyc.spring.bean.annotation.controller;
import org.springframework.stereotype.Controller;
/**
* @author xuehj2016
* @Title: UserController
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:39
*/
@Controller
public class UserController {
public void execute(){
System.out.println("UserController Execute...");
}
}
userRepository.java
package com.hzyc.spring.bean.annotation.repository;
/**
* @author xuehj2016
* @Title: UserRepository
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:34
*/
public interface UserRepository {
void save();
}
userRepositoryImpl.java
package com.hzyc.spring.bean.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* @author xuehj2016
* @Title: UserRepositoryImpl
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:35
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository Save...");
}
}
userService.java
package com.hzyc.spring.bean.annotation.service;
import org.springframework.stereotype.Service;
/**
* @author xuehj2016
* @Title: UserService
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:37
*/
@Service
public class UserService {
public void add() {
System.out.println("UserService Add...");
}
}
bean-annotation.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 IOC 容器掃描的包-->
<!--可以通過 resource-pattern 指定掃描的資源-->
<!--
<context:component-scan
base-package="com.hzyc.spring.bean.annotation"
resource-pattern="repository/*.class"/>
-->
<!--context:exclude-filter : 子節點指定排除哪些指定表示式的元件-->
<!--context:include-filter : 子節點指定包含哪些表示式的元件,該節點需要 use-default-filters="false" 配合使用-->
<!---->
<context:component-scan
base-package="com.hzyc.spring.bean.annotation"
use-default-filters="false">
<!--
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:exclude-filter type="assignable"
expression="com.hzyc.spring.bean.annotation.repository.UserRepository"/>
-->
<context:include-filter type="assignable"
expression="com.hzyc.spring.bean.annotation.repository.UserRepository"/>
</context:component-scan>
</beans>
Main.java
package com.hzyc.spring.bean.annotation;
import com.hzyc.spring.bean.annotation.controller.UserController;
import com.hzyc.spring.bean.annotation.repository.UserRepository;
import com.hzyc.spring.bean.annotation.repository.UserRepositoryImpl;
import com.hzyc.spring.bean.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author xuehj2016
* @Title: Main
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:44
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-annotation.xml");
TestObject testObject= (TestObject) applicationContext.getBean("testObject");
System.out.println(testObject);
UserController userController= (UserController) applicationContext.getBean("userController");
System.out.println(userController);
UserService userService= (UserService) applicationContext.getBean("userService");
System.out.println(userService);
UserRepositoryImpl userRepository= (UserRepositoryImpl) applicationContext.getBean("userRepository");
System.out.println(userRepository);
}
}
元件裝配
•<context:component-scan> 元素還會自動註冊 AutowiredAnnotationBeanPostProcessor 例項, 該例項可以自動裝配具有 @Autowired 和 @Resource 、@Inject註解的屬性.
使用 @Autowired 自動裝配 Bean
•@Autowired 註解自動裝配具有相容型別的單個 Bean屬性
–構造器, 普通欄位(即使是非 public), 一切具有引數的方法都可以應用@Authwired 註解
–預設情況下, 所有使用 @Authwired 註解的屬性都需要被設定. 當 Spring 找不到匹配的 Bean 裝配屬性時, 會丟擲異常, 若某一屬性允許不被設定, 可以設定 @Authwired 註解的 required 屬性為 false
–預設情況下, 當 IOC 容器裡存在多個型別相容的 Bean 時, 通過型別的自動裝配將無法工作. 此時可以在 @Qualifier 註解裡提供 Bean 的名稱. Spring 允許對方法的入參標註 @Qualifiter 已指定注入 Bean 的名稱
– @Authwired 註解也可以應用在陣列型別的屬性上, 此時 Spring 將會把所有匹配的 Bean 進行自動裝配.
–@Authwired 註解也可以應用在集合屬性上, 此時 Spring 讀取該集合的型別資訊, 然後自動裝配所有與之相容的 Bean.
–@Authwired 註解用在 java.util.Map 上時, 若該 Map 的鍵值為 String, 那麼 Spring 將自動裝配與之 Map 值型別相容的 Bean, 此時 Bean 的名稱作為鍵值
•
使用 @Resource 或 @Inject
自動裝配 Bean
•Spring 還支援 @Resource 和 @Inject 註解,這兩個註解和 @Autowired 註解的功用類似
•@Resource 註解要求提供一個 Bean 名稱的屬性,若該屬性為空,則自動採用標註處的變數或方法名作為 Bean 的名稱
•@Inject 和 @Autowired 註解一樣也是按型別匹配注入的 Bean, 但沒有 reqired 屬性
•建議使用 @Autowired 註解
testObject.java
package com.hzyc.spring.bean.annotation;
import org.springframework.stereotype.Component;
/**
* @author xuehj2016
* @Title: TestObject
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:30
*/
@Component
public class TestObject {
}
userController.java
package com.hzyc.spring.bean.annotation.controller;
import com.hzyc.spring.bean.annotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* @author xuehj2016
* @Title: UserController
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:39
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserController Execute...");
userService.add();
}
}
userRepository.java
package com.hzyc.spring.bean.annotation.repository;
/**
* @author xuehj2016
* @Title: UserRepository
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:34
*/
public interface UserRepository {
void save();
}
userRepositoryImpl.java
package com.hzyc.spring.bean.annotation.repository;
import com.hzyc.spring.bean.annotation.TestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
* @author xuehj2016
* @Title: UserRepositoryImpl
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:35
*/
@Repository
/*("userRepository")*/
public class UserRepositoryImpl implements UserRepository {
@Autowired(required = false)
private TestObject testObject;
@Override
public void save() {
System.out.println("UserRepository Save...");
System.out.println(testObject);
}
}
userService.java
package com.hzyc.spring.bean.annotation.service;
import com.hzyc.spring.bean.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* @author xuehj2016
* @Title: UserService
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:37
*/
@Service
public class UserService {
@Autowired
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;
public void add() {
System.out.println("UserService Add...");
userRepository.save();
}
@Autowired
public void setUserRepository(@Qualifier("userRepositoryImpl")
UserRepository userRepository) {
this.userRepository = userRepository;
}
}
UserJdbcRepository.java
package com.hzyc.spring.bean.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* @author xuehj2016
* @Title: UserJdbcRepository
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 22:45
*/
@Repository
public class UserJdbcRepository implements UserRepository {
@Override
public void save() {
System.out.println("UserJdbcRepository save...");
}
}
bean-annotation.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 IOC 容器掃描的包-->
<!--可以通過 resource-pattern 指定掃描的資源-->
<!--
<context:component-scan
base-package="com.hzyc.spring.bean.annotation"
resource-pattern="repository/*.class"/>
-->
<!--context:exclude-filter : 子節點指定排除哪些指定表示式的元件-->
<!--context:include-filter : 子節點指定包含哪些表示式的元件,該節點需要 use-default-filters="false" 配合使用-->
<!---->
<context:component-scan
base-package="com.hzyc.spring.bean.annotation">
<!--
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<!--
<context:exclude-filter type="assignable"
expression="com.hzyc.spring.bean.annotation.repository.UserRepository"/>
-->
<!--
<context:include-filter type="assignable"
expression="com.hzyc.spring.bean.annotation.repository.UserRepository"/>
-->
</context:component-scan>
</beans>
Main.java
package com.hzyc.spring.bean.annotation;
import com.hzyc.spring.bean.annotation.controller.UserController;
import com.hzyc.spring.bean.annotation.repository.UserRepository;
import com.hzyc.spring.bean.annotation.repository.UserRepositoryImpl;
import com.hzyc.spring.bean.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author xuehj2016
* @Title: Main
* @ProjectName Spring
* @Description: TODO
* @date 2018/12/18 20:44
*/
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-annotation.xml");
// TestObject testObject= (TestObject) applicationContext.getBean("testObject");
// System.out.println(testObject);
UserController userController = (UserController) applicationContext.getBean("userController");
System.out.println(userController);
userController.execute();
//
// UserService userService= (UserService) applicationContext.getBean("userService");
// System.out.println(userService);
//
// UserRepositoryImpl userRepository= (UserRepositoryImpl) applicationContext.getBean("userRepository");
// System.out.println(userRepository);
}
}