1. 程式人生 > 實用技巧 >Spring基礎-其1

Spring基礎-其1

  1. 使用context:exclude-filter指定掃描包時不包含的類
<context:component-scan base-package="com.xj">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<context:component-scan base-package="com.xj">
<context:exclude-filter type="assignable" expression="com.xj.com.xj.servlet.BookServlet"/>
</context:component-scan>

2. 使用context:include- filter指定掃描包時要包含的類
+ 一定要禁用掉預設規則才行use-default-filters="false"

<context:component-scan base-package="com.xj" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

可以寫的屬性

type="annotation":指定排除規則;按照註解進行排除。標註了指定註解的元件不要
type="assignable"指定排除某個具體的類,按照類排除
type="aspectj"後來aspectj表示式
type="custom"自定義一個TypeFilter;自己寫程式碼決定哪些使用
type="regex"還可以寫正則表示式
expression="":註解的全類名


3. Autowired自動注入

@Qualifier("新id")
@AutoWired(required=false)//能裝就裝,不能裝null
private BookService bookservice;

+ 可以寫在方法上:執行時,每一個引數都會自動注入

4. @Autowired、@Resource、@Inject都是自動裝配的意思
@Autowired:最強大; Spring自己的註解
@Resource: j2ee; java的標準,擴充套件性強,如果切換成另外一個容器框架,resource還是能被認識

**@Resource:擴充套件性更強;如果切換成另外一個容器框架,@Resource還是可以使用的,@Autqwired就不行了**


5. 使用Spring的單元測試模組來執行標了@Test註解的測試方法;

@ContextConfiguration(locations="")使用它來指定Spring的配置檔案的位置
@RunWith指定用哪種驅動進行單元測試,預設就是junit

@ContextConfiguration(locations="classpath:applicationContext.xml")
@RunWith (SpringJUnit4ClassRunner.class)//使用spring的單元測試模組
public class IOCTest{
}