Spring中通過註解配置bean(1)

Spring中通過註解配置bean(1)


一、說在前面

1、在使用註解配置bean之前需要添加spring-aop-4.0.0.RELEASE.jar包


2、組件掃描(component scanning):Spring能夠從 classpath 下自動掃描, 偵測和實例化具有特定註解的組件。
3、特定組件包括
(1)@Component: 基本註解, 標識了一個受 Spring 管理的組件。
(2)@Respository: 標識持久層組件。
(3)@Service: 標識服務層(業務層)組件。
(4)@Controller: 標識表現層組件。
4、對於掃描到的組件, Spring 有默認的命名策略:使用非限定類名, 第一個字母小寫;也可以在註解中通過 value 屬性值標識組件的名稱。
5、當在組件類上使用了特定的註解之後, 還需要在 Spring 的配置文件中聲明 <context:component-scan> :
(1)base-package 屬性指定一個需要掃描的基類包,Spring 容器將會掃描這個基類包裏及其子包中的所有類。
(2)當需要掃描多個包時, 可以使用逗號分隔。
(3)如果僅希望掃描特定的類而非基包下的所有類,可使用 resource-pattern 屬性過濾特定的類。
(4)<context:include-filter> 子節點表示要包含的目標類。
(5)<context:exclude-filter> 子節點表示要排除在外的目標類。
(6)<context:component-scan> 下可以擁有若干個 <context:include-filter> 和 <context:exclude-filter> 子節點。
(7)<context:include-filter> 和 <context:exclude-filter> 子節點支持多種類型的過濾表達式,常見的類型有annotation(註解標籤方式)和assignable(類名方式)。


二、實例演示

1、com.at.beans.annotation包下面有TestObject類

package com.at.beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}


2、com.at.beans.annotation.controller包下面有UserController類

package com.at.beans.annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {

	public void execute(){
		System.out.println("UserController execute......");
	}
}


3、com.at.beans.annotation.repository包下面有UserRepository接口和對應的實現類UserRepositoryImpl

package com.at.beans.annotation.repository;

public interface UserRepository {

	void save();
}


package com.at.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{

	public void save() {
		System.out.println("UserRepository Save......");
	}

}

4、com.at.beans.annotation.service包下面有UserService類

package com.at.beans.annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

	public void add(){
		System.out.println("UserService add......");
	}
}


5、bean配置

<!-- 指定Spring ioc容器掃描的包 -->
<context:component-scan base-package="com.at.beans.annotation"></context:component-scan>

6、測試函數

package com.at.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.at.beans.annotation.controller.UserController;
import com.at.beans.annotation.repository.UserRepository;
import com.at.beans.annotation.service.UserService;

public class TestAnnotation {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
		TestObject testObject = (TestObject) ctx.getBean("testObject");
		System.out.println(testObject);
		UserController userController = (UserController) ctx.getBean("userController");
		System.out.println(userController);
		UserRepository userRepository = (UserRepository)ctx.getBean("userRepository");
		System.out.println(userRepository);
		UserService userService = (UserService) ctx.getBean("userService");
		System.out.println(userService);
	}
}

7、運行結果

六月 02, 2017 3:56:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6d8ced6: startup date [Fri Jun 02 15:56:40 CST 2017]; root of context hierarchy
六月 02, 2017 3:56:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-annotation.xml]
com.at.beans.annotation.TestObject@4403e3a5
com.at.beans.annotation.controller.UserController@5b568885
com.at.beans.annotation.repository.UserRepositoryImpl@e1d029c
com.at.beans.annotation.service.UserService@12965701

三、特殊情況

1、只掃描特定的包下面的類,使用resource-pattern

	<context:component-scan base-package="com.at.beans.annotation"
		resource-pattern="repository/*.class">
	</context:component-scan>

2、使用<context:exclude-filter/>,type類型爲annotation來排除使用指定註解標籤的類

	<context:component-scan base-package="com.at.beans.annotation">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan>

3、使用<context:include-filter/>,type類型爲annotation來指定使用特定的註解標籤的類,此時需要將context:component-scan的use-default-filters屬性設置爲false

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

4、使用<context:exclude-filter/>,type類型爲assignable來排除使用指定註解標籤的類

	<context:component-scan base-package="com.at.beans.annotation">
		<context:exclude-filter type="assignable" expression="com.at.beans.annotation.repository.UserRepository"/>
	</context:component-scan>	

5、使用<context:include-filter/>,type類型爲assignable來指定使用特定的註解標籤的類,此時需要將context:component-scan的use-default-filters屬性設置爲false
	<context:component-scan base-package="com.at.beans.annotation" use-default-filters="false">
		<context:include-filter type="assignable" expression="com.at.beans.annotation.repository.UserRepository"/>
	</context:component-scan>


By luoyepiaoxue2014

微博地址:http://weibo.com/luoyepiaoxue2014 點擊打開鏈接



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章