Spring自动注入Bean
2011-03-04 14:54
295 查看
通过@Autowired或@Resource来实现在Bean中自动注入的功能,但还要在配置文件中写Bean定义,下面我们将介绍如何注解Bean,从而从XML配置文件
中完全移除Bean定义的配置。
1. @Component(不推荐使用)、@Repository、@Service、@Controller
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
Java
代码
@Component
public
class
UserDaoImpl
extends
HibernateDaoSupport
implements
UserDao {
...
}
使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是
userDaoImpl。你也可以指定Bean的名称:
@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、
@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与
@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、
@Service、@Controller来替代@Component。
2. 使用<context:component-scan />让Bean定义注解工作起来
Java
代码
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package
=
"com.kedacom.ksoa"
/>
</beans>
这里,所有通过<bean>元素定义Bean的配置内容已经被移除,仅需要添加一行<context:component-
scan />配置就解决所有问题了——Spring
XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan
/>的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan
/>还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com/.kedacom/.spring/.annotation/.web/..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
Java
代码
<context:component-scan base-
package
=
"com.casheen.spring.annotation"
>
<context:exclude-filter type="regex"
expression=
"com/.casheen/.spring/.annotation/.web/..*"
/>
</context:component-scan>
值得注意的是<context:component-scan
/>配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),
因此当使用<context:component-scan />后,就可以将<context:annotation-config
/>移除了。
3. 使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完
成这项工作:
Java
代码
@Scope
(
"session"
)
@Component
()
public
class
UserSessionBean
implements
Serializable {
...
}
中完全移除Bean定义的配置。
1. @Component(不推荐使用)、@Repository、@Service、@Controller
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
Java
代码
@Component
public
class
UserDaoImpl
extends
HibernateDaoSupport
implements
UserDao {
...
}
@Component public class UserDaoImpl extends HibernateDaoSupport implements UserDao { ... }
使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是
userDaoImpl。你也可以指定Bean的名称:
@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、
@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与
@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、
@Service、@Controller来替代@Component。
2. 使用<context:component-scan />让Bean定义注解工作起来
Java
代码
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package
=
"com.kedacom.ksoa"
/>
</beans>
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.kedacom.ksoa" /> </beans>
这里,所有通过<bean>元素定义Bean的配置内容已经被移除,仅需要添加一行<context:component-
scan />配置就解决所有问题了——Spring
XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan
/>的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan
/>还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com/.kedacom/.spring/.annotation/.web/..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
Java
代码
<context:component-scan base-
package
=
"com.casheen.spring.annotation"
>
<context:exclude-filter type="regex"
expression=
"com/.casheen/.spring/.annotation/.web/..*"
/>
</context:component-scan>
<context:component-scan base-package="com.casheen.spring.annotation"> <context:exclude-filter type="regex" expression="com/.casheen/.spring/.annotation/.web/..*" /> </context:component-scan>
值得注意的是<context:component-scan
/>配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),
因此当使用<context:component-scan />后,就可以将<context:annotation-config
/>移除了。
3. 使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完
成这项工作:
Java
代码
@Scope
(
"session"
)
@Component
()
public
class
UserSessionBean
implements
Serializable {
...
}
相关文章推荐
- spring 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入
- Spring注解自动注入Bean
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- spring 自动注入bean,不用一个一个去配置<bean/>的方法
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- SSH下使用Spring注解自动注入bean
- 类注解Spring注解自动注入Bean
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
- servlet 中使用Spring自动注入的bean
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Spring自动注入装配Bean详细解释
- struts2与spring集成时,关于Action类中成员bean自动注入的问题
- 用spring的注解建立bean与bean之间的关系组件装配及自动注入依赖对象
- Servlet自动注入Spring容器中的Bean解决方法
- SSH下使用Spring注解自动注入bean
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- Spring自动装配注入合作者Bean ——autowire
- Action中如何通过@Autowired自动注入spring bean ?
- Spring定义bean的三种方式和自动注入
- 解决Spring+Quartz无法自动注入bean问题