Container的職責在於創建、配置與組裝bean,昨天我們學到了
如何使用SpEL來讓我們更方便設定Bean
今日將討論如何使用註解的方式來將bean設置到container中
簡單的說就是透過Java Annotation的方式進行配置,例如@Controller,而前面討論的是XML Schema-based configuration,兩者的配置方式都各有優缺點,詳細可以看Spring doc的說明Are annotations better than XML for configuring Spring?
翻譯作刻板印象註解,在系統架構分層中可以讓我們清楚了解物件是在哪一層主要負責的功能,Spring並不會區分這些註解
package com.swj.controller;
import org.springframework.stereotype.Controller;
@Controller
public class EmpServlet {
}
package com.swj.dao;
import org.springframework.stereotype.Repository;
@Repository
public class EmpDao {
}
package com.swj.service;
import org.springframework.stereotype.Service;
@Service
public class EmpService {
}
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 啟動註解掃描,掃描com.swj package之下的類 -->
<context:component-scan base-package="com.swj"></context:component-scan>
</beans>
@Test
public void testDay14(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean14.xml");
System.out.println("容器啟動完成....");
for(String name:ioc.getBeanDefinitionNames()){
System.out.println(name);
}
}
Result
測試
@Service("HelloEmpService")
@Scope(value = "prototype")
public class EmpService {
}
@Test
public void testDay14(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean14.xml");
System.out.println("容器啟動完成....");
EmpService s1 = ioc.getBean("HelloEmpService", EmpService.class);
EmpService s2 = ioc.getBean("HelloEmpService", EmpService.class);
System.out.println(s1 == s2);
}
Result
可以客製化要掃描組件,可根據屬性type決定要如何執行過濾(annotation、assignable)
需要禁用默認全掃的規則才會生效
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 啟動註解掃描,掃描com.swj package之下的類,use-default-filters=false禁用默認全掃的規則 -->
<context:component-scan base-package="com.swj" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
@Test
public void testDay14(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean14.xml");
System.out.println("容器啟動完成....");
for(String name :ioc.getBeanDefinitionNames()){
System.out.println(name);
}
}
Result
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 啟動註解掃描,掃描com.swj package之下的類 -->
<context:component-scan base-package="com.swj">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
Result