Spring学习(5)---Bean的定义及作用域的注解实现

Bean管理的注解实现

  • Classpath扫描与组件管理
  • 类的自动检测与注册Bean
  • <context:annotation-config/>
  • @Component,@Repository,@Service,@Controller
  • @Required
  • @Autowired
  • @Qualifier
  • @Resource

(一) Classpath扫描与组件管理

  • 从Spring3.0开始,Spring javaConfig项目提供了很多特性,包括使用java而不是xml定义bean,比如:@Configuration,@Bean,@Import,@DependsOn
  • @Component是一个通用的注解,可用于任何的bean
  • @Repository,@Service,@Controller是更有针对性的注解

- @Repository 通常用于注解DAO类,即持久层

- @Service 通常用于注解 Service类,即服务层

- @Controller 通常用于注解Controller类,即控制层

(二) 类的自动检测及Bean的注册

Spring可以自动检测类并注册Bean到ApplicationContext中

(三) <context:annotation-config/>

  • 通常在基于XML的Spring配置如下标签(请注意包含上下文命名空间)
  • <context:annotation-config/>仅会查找在同一个applicationContext中的bean注解
<?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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:annotation-config/>
 <beans/>

(四)类的自动检测及Bean的注册(补充)

为了能够检测这些类并注册相应的bean,需要下面的内容:

<?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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.mypackage"></context:component-scan>
</beans>

该配置隐式注册了多个对注解进行解析的处理器,如:
 AutowiredAnnotationBeanPostProcessor    
 CommonAnnotationBeanPostProcessor
 PersistenceAnnotationBeanPostProcessor  
 RequiredAnnotationBeanPostProcessor
 其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器,其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器,所以配置了<context:component-scan  base-package="">之后,便无需再配置<context:annotation-config>

(五)使用过滤器进行自定义扫描

  • 默认情况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用@Component的自定义注解
  • 可以通过过滤器修改上面的行为,如:下面例子的xml配置忽略所有的@Repository注解并用“Stub”代替
<?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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.mypackage">
        <!-- 包含的过滤器 -->
    	<context:include-filter type="regex" expression=".*Stub.*Repository"/>
    	<!-- 排除的过滤器 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

</beans>
  • 还可以使用use-default-filters="false" 禁用自动发现并注册

(六)定义Bean

扫描过程中组件被自动检测,那么Bean名称事由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显式设置Bean Name)

@Component("exampleBean")
public class ExampleBean{
    //........
}
------------------------------
@Service("exampleBean")
public class ExampleBean{
    //........
}
------------------------------
//虽然并未显式声明,但会根据BeanNameGenerator自动生成,规则:以类名为基础,类名第一个字母小写
@Repository
public class ExampleBean{
    //........
}

可自定义bean命名策略,实现BeanNameGenerator接口,并一定要包含一个无参构造器

<!-- 自定义的命名规则,例如: 类名全小写,全大写.....;按照自己的方式实现-->
<context:component-scan base-package="com.mypackage" name-generator="com.mypackage.MyBeanNameGenerator">

</context:component-scan>

(七)作用域

通常情况下自动查找的Spring组件,其Scope是singleton,Spring提供了一个标识scope的注解@Scope

@Scope("prototype")
@Repository
public class OneInterfaceImpl extends OneInterface {
   //..........
}

也可以自定义scope策略,实现ScopeMetadataResolver接口,并提供一个无参的构造器

<context:component-scan base-package="com.mypackage" scope-resolver="com.mypackage.MyScopeMetadataResolver">
</context:component-scan>

(八)代理方式

可以使用scope-proxy属性指定代理,有三个值可选:no,interfaces,targetClass

    <context:component-scan base-package="com.mypackage" scoped-proxy="interfaces">
    </context:component-scan>
时间: 2024-12-20 03:55:45

Spring学习(5)---Bean的定义及作用域的注解实现的相关文章

Spring学习九----------Bean的配置之Bean的定义及作用域的注解实现

Spring Bean常用注解 @Component:通常注解,可用于任何Bean @Repository:通常用于注解DAO层,即持久层 @Service:通常用于注解Service层,即服务层 @Controller:通常用于注解Controller层,即控制层 类的自动检测及Bean的注册 <context:component-scan base-package=""/>:自动扫描base-package定义的包或其子包下的类,并将带有@Component,@Cont

Spring中Bean的定义及作用域的注解实现

Classpath扫描与组件管理: 从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是xml定义bean,指的是注解 @Configuration,@Bean ,@Import ,@DependsOn @Component是一个通用注解,可用于任何bean @Repository:通常用于注解DAO类,即持久层 @Service:通常用于注解Service类,即服务层 @Controller:通常用于Controller类,即控制层MVC 元注

Bean的定义及作用域的注解实现

Classpath扫描与组件管理: 从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是xml定义bean,指的是注解 @Configuration,@Bean ,@Import ,@DependsOn @Component是一个通用注解,可用于任何bean @Repository:通常用于注解DAO类,即持久层 @Service:通常用于注解Service类,即服务层 @Controller:通常用于Controller类,即控制层MVC 元注

【Spring学习】Bean的扫描注册

在前面的文章<使用IDEA创建Spring mvc工程及简要分析>中,稍微讲过MVC寻找配置文件的过程,现在在这个基础上,看一下配置文件是如何加载的,着重看一下Bean的扫描注册过程.其实稍微用过Spring的人都知道,Bean可以通过Xml配置文件与注解两种方式来配置,看过本文后可以看到,这两种方式最后调用的都是相同的接口进行Bean的注册,只不过扫描的过程不一样. 一.配置文件读取 上面文章最后提到XmlWebApplicationContext类,找到了名为mvc-dispatcher-

Spring学习四----------Bean的配置之Bean的配置项及作用域

Bean的作用域(每个作用域都是在同一个Bean容器中) 1.singleton:单例,指一个Bean容器中只存在一份(默认) 2.prototype:每次请求(每次使用)创建新的实例,destory方式不生效 3.request:每次http请求创建一个实例且仅在当前request内生效(只能在web中使用) 4.session:同上,每次http请求创建一个实例,当前session内有效(只能在web中使用) 5.global session:基于portlet的web中有效(portlet

【Spring学习】Bean生命周期

我理解的Bean生命周期包括两个方面: Bean何时创建,何时销毁 Bean从创建到销毁的执行流程 一.Bean创建与销毁 Bean的创建时机主要由几个配置项共同来决定,包括: scope属性,决定是Bean是单例模式(singleton)还是多例模式(prototype),默认为单例singleton: lazy-init属性,只对单例模式有效,决定是否延时加载,默认为false,表示在容器初始化时,就会生成单例: RequestMapping属性,这个注解MVC中才有,当有该属性时,lazy

spring学习之bean的生存范围和生命周期

1.bean的生存范围: (1)Singleton:默认,单例 (2)Prototype:原型,非单例 (3)Request:在一次http请求中,容器会返回该bean的同一个实例,对于不同的请求,返回不同的实例. (4)Session:请求的作用域变为session (5)Gloabsession:全局session request,session主要用于web之中 我们这次主要探讨singleton和prototype这2个生存范围 <span style="color:#FF0000

Java Spring学习笔记----Bean的依赖注入(1)

Spring常用的两种依赖注入方式:一种是设值注入方式,利用Bean的setter方法设置Bean的属性值:另一种是构造注入,通过给Bean的构造方法传递参数来实现Bean的属性赋值: 1.设值注入方式 直接上代码例子,示例的树结构图如下 Shape.java接口内容 package chapter3; public interface Shape { public double area();//求形状的面积 } Circle.java内容: package chapter3; public

spring学习(二十九)--参数绑定注解

package springAnnotions; import java.util.Map; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springfram