Spring中通过注解配置bean(1)

来源:互联网 发布:php进阶书籍推荐知乎 编辑:程序博客网 时间:2024/05/20 07:58

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;@Componentpublic class TestObject {}


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

package com.at.beans.annotation.controller;import org.springframework.stereotype.Controller;@Controllerpublic 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;@Servicepublic 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@4403e3a5com.at.beans.annotation.controller.UserController@5b568885com.at.beans.annotation.repository.UserRepositoryImpl@e1d029ccom.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 点击打开链接



原创粉丝点击