- 浏览: 68973 次
- 性别:
- 来自: 深圳
最新评论
-
wzpwap123:
为嘛 服务器端修改不了cookie啊 这是咋回事?求解释nul ...
COOKIE的读、写、修改、删除 -
xiaoniu001:
1楼的beans.xml好像没配置啊
<bean cla ...
Spring Annotation(五) -
xiaoniu001:
太详细了,帮助很大,谢楼主
Spring Annotation(四) -
sjbwylbs:
经测试,没反映beans.xml
<?xml versi ...
Spring Annotation(五) -
wxq276:
OutputStream out;
try {
ou ...
JAVA对象转换为XML格式
使用 <context:annotation-config/> 简化配置
Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
而我们前面所介绍的 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor
就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些 BeanPostProcessor
的方式,这就是 <context:annotation-config/>。请看下面的配置:
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean id="boss" class="com.baobaotao.Boss"/> <bean id="office" class="com.baobaotao.Office"> <property name="officeNo" value="001"/> </bean> <bean id="car" class="com.baobaotao.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean> </beans> |
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor
、CommonAnnotationBeanPostProcessor
、PersistenceAnnotationBeanPostProcessor
以及 equiredAnnotationBeanPostProcessor
这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。
虽然我们可以通过 @Autowired
或 @Resource
在 Bean 类中使用自动注入功能,但是 Bean 还是在 XML 文件中通过 <bean> 进行定义 —— 也就是说,在 XML 配置文件中定义 Bean,通过 @Autowired
或 @Resource
为 Bean 的成员变量、方法入参或构造函数入参提供自动注入的功能。能否也通过注释定义 Bean,从 XML 配置文件中完全移除 Bean 定义的配置呢?答案是肯定的,我们通过 Spring 2.5 提供的 @Component
注释就可以达到这个目标了。
下面,我们完全使用注释定义 Bean 并完成 Bean 之间装配:
清单 20. 使用 @Component 注释的 Car.java
package com.baobaotao; import org.springframework.stereotype.Component; @Component public class Car { … } |
仅需要在类定义处,使用 @Component
注释就可以将一个类定义了 Spring 容器中的 Bean。下面的代码将 Office
定义为一个 Bean:
清单 21. 使用 @Component 注释的 Office.java
package com.baobaotao; import org.springframework.stereotype.Component; @Component public class Office { private String officeNo = "001"; … } |
这样,我们就可以在 Boss 类中通过 @Autowired
注入前面定义的 Car
和 Office Bean
了。
清单 22. 使用 @Component 注释的 Boss.java
package com.baobaotao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Required; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component("boss") public class Boss { @Autowired private Car car; @Autowired private Office office; … } |
@Component
有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss
”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。
在使用 @Component
注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略。Spring 2.5 对 context 命名空间进行了扩展,提供了这一功能,请看下面的配置:
<?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-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.baobaotao"/> </beans> |
这里,所有通过 <bean> 元素定义 Bean 的配置内容已经被移除,仅需要添加一行 <context:component-scan/> 配置就解决所有问题了——Spring XML 配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式,通过下表说明:
注释 | 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。 |
类名指定 | 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。 |
正则表达式 | 通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.* |
AspectJ 表达式 | 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+ |
下面是一个简单的例子:
<context:component-scan base-package="com.baobaotao"> <context:include-filter type="regex" expression="com\.baobaotao\.service\..*"/> <context:exclude-filter type="aspectj" expression="com.baobaotao.util..*"/> </context:component-scan> |
值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor
),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
默认情况下通过 @Component
定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope
注释来达到目标,如以下代码所示:
清单 24. 通过 @Scope 指定 Bean 的作用范围
package com.baobaotao; import org.springframework.context.annotation.Scope; … @Scope("prototype") @Component("boss") public class Boss { … } |
这样,当从 Spring 容器中获取 boss
Bean 时,每次返回的都是新的实例了。
Spring 2.5 中除了提供 @Component
注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository
、@Service
和 @Controller
。在目前的 Spring 版本中,这 3 个注释和 @Component
是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别
和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component
相比没有什么新意,但
Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,
最好在持久层、业务层和控制层分别采用 @Repository
、@Service
和 @Controller
对分层中的类进行注释,
而用 @Component
对那些比较中立的类进行注释。
是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因:
- 注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
- 如果 Bean 不是自己编写的类(如
JdbcTemplate
、SessionFactoryBean
等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。 - 注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于
@Transaction
事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。
所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。
Spring 在 2.1 以后对注释配置提供了强力的支持,注释配置功能成为 Spring 2.5 的最大的亮点之一。合理地使用 Spring 2.5 的注释配置,可以有效减少配置的工作量,提高程序的内聚性。但是这并不意味着传统 XML 配置将走向消亡,在第三方类 Bean 的配置,以及那些诸如数据源、缓存池、持久层操作模板类、事务管理等内容的配置上,XML 配置依然拥有不可替代的地位。
评论
<bean id="name2" class="xxx.Xxx" />
<bean id="name3" class="xxx.Xxx" />
代码class="xxx.Xxx"中的"xxx.Xxx"是不是同一个类名?
如果是同一个类名就没必要写那么多了,创建一个BEAN id就行了
如果不同我话就如所说的那样写就OK啦
nowind 写道
jimmychenli 写道 nowind 写道 你好~ 关于spring annotation,有一个问题想请教: 怎样把这样的xml变为annotation呢? &amp;amp;lt;bean id="name1" class="xxx.Xxx" /&amp;amp;gt; &amp;amp;lt;bean id="name2" class="xxx.Xxx" /&amp;amp;gt; &amp;amp;lt;bean id="name3" class="xxx.Xxx" /&amp;amp;gt; 也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗? &amp;lt;bean id="name1" class="xxx.Xxx" /&amp;gt; &amp;lt;bean id="name2" class="xxx.Xxx" /&amp;gt; &amp;lt;bean id="name3" class="xxx.Xxx" /&amp;gt; 你一个类为何要声明三次勒,有意义吗? 不管你调用的是哪个bean名字,最终还是这个类,好像没有必要要那样做吧! 在我的程序中,的确是有这样的情况 比如说有三个包: com.abc.test1下: public class Test1 { &nbsp;&nbsp;&nbsp; @Autowired &nbsp;&nbsp;&nbsp; @Qualifier("name1") &nbsp;&nbsp;&nbsp; private Xxx name1; } public class Other1 { &nbsp;&nbsp;&nbsp; @Autowired &nbsp;&nbsp;&nbsp; @Qualifier("name1") &nbsp;&nbsp;&nbsp; private Xxx name1; } 它们两个用到的是同一个Xxx对象。 在另一个包: com.abc.test2下 public class Test2 { &nbsp;&nbsp;&nbsp; @Autowired &nbsp;&nbsp;&nbsp; @Qualifier("name2") &nbsp;&nbsp;&nbsp; private Xxx name2; } public class Other2 { &nbsp;&nbsp;&nbsp; @Autowired &nbsp;&nbsp;&nbsp; @Qualifier("name2") &nbsp;&nbsp;&nbsp; private Xxx name2; } 它们又用到了同一个Xxx对象 我现在的做法,就是在xml中定义: &lt;bean id="name1" class="xxx.Xxx" /&gt; &lt;bean id="name2" class="xxx.Xxx" /&gt; &lt;bean id="name3" class="xxx.Xxx" /&gt; 可是我又想把它们移到Annotation中,却找不到合适的方法。 你的两个包下分别注入的是同一个类,只不过你给了他们不同的名字, 你在com.abc.test1下可以使用 @Autowired @Qualifier("name1"); private Xxx name1; 你在com.abc.test2下使用 @Autowired @Qualifier("name2"); private Xxx name2; 其实test2下所注入的,你完全可以写成 @Autowired @Qualifier("name1"); private Xxx name1; 没必要再重新定义一个BEAN名字,两者效果是一样的 69563588有兴趣加此群
效果应该不一样吧! "name1"与"name2"代表了两个不同的实例.如果都写成"name1",就使用的是同一个实例了.
jimmychenli 写道
nowind 写道 你好~ 关于spring annotation,有一个问题想请教: 怎样把这样的xml变为annotation呢? &amp;lt;bean id="name1" class="xxx.Xxx" /&amp;gt; &amp;lt;bean id="name2" class="xxx.Xxx" /&amp;gt; &amp;lt;bean id="name3" class="xxx.Xxx" /&amp;gt; 也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗? &lt;bean id="name1" class="xxx.Xxx" /&gt; &lt;bean id="name2" class="xxx.Xxx" /&gt; &lt;bean id="name3" class="xxx.Xxx" /&gt; 你一个类为何要声明三次勒,有意义吗? 不管你调用的是哪个bean名字,最终还是这个类,好像没有必要要那样做吧! 在我的程序中,的确是有这样的情况 比如说有三个包: com.abc.test1下: public class Test1 { @Autowired @Qualifier("name1") private Xxx name1; } public class Other1 { @Autowired @Qualifier("name1") private Xxx name1; } 它们两个用到的是同一个Xxx对象。 在另一个包: com.abc.test2下 public class Test2 { @Autowired @Qualifier("name2") private Xxx name2; } public class Other2 { @Autowired @Qualifier("name2") private Xxx name2; } 它们又用到了同一个Xxx对象 我现在的做法,就是在xml中定义: <bean id="name1" class="xxx.Xxx" /> <bean id="name2" class="xxx.Xxx" /> <bean id="name3" class="xxx.Xxx" /> 可是我又想把它们移到Annotation中,却找不到合适的方法。
你的两个包下分别注入的是同一个类,只不过你给了他们不同的名字,
你在com.abc.test1下可以使用
@Autowired
@Qualifier("name1");
private Xxx name1;
你在com.abc.test2下使用
@Autowired
@Qualifier("name2");
private Xxx name2;
其实test2下所注入的,你完全可以写成
@Autowired
@Qualifier("name1");
private Xxx name1;
没必要再重新定义一个BEAN名字,两者效果是一样的
69563588有兴趣加此群
nowind 写道
你好~ 关于spring annotation,有一个问题想请教: 怎样把这样的xml变为annotation呢? &lt;bean id="name1" class="xxx.Xxx" /&gt; &lt;bean id="name2" class="xxx.Xxx" /&gt; &lt;bean id="name3" class="xxx.Xxx" /&gt; 也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗? <bean id="name1" class="xxx.Xxx" /> <bean id="name2" class="xxx.Xxx" /> <bean id="name3" class="xxx.Xxx" /> 你一个类为何要声明三次勒,有意义吗? 不管你调用的是哪个bean名字,最终还是这个类,好像没有必要要那样做吧!
在我的程序中,的确是有这样的情况
比如说有三个包:
com.abc.test1下:
public class Test1 {
@Autowired
@Qualifier("name1")
private Xxx name1;
}
public class Other1 {
@Autowired
@Qualifier("name1")
private Xxx name1;
}
它们两个用到的是同一个Xxx对象。
在另一个包:
com.abc.test2下
public class Test2 {
@Autowired
@Qualifier("name2")
private Xxx name2;
}
public class Other2 {
@Autowired
@Qualifier("name2")
private Xxx name2;
}
它们又用到了同一个Xxx对象
我现在的做法,就是在xml中定义:
<bean id="name1" class="xxx.Xxx" />
<bean id="name2" class="xxx.Xxx" />
<bean id="name3" class="xxx.Xxx" />
可是我又想把它们移到Annotation中,却找不到合适的方法。
你好~ 关于spring annotation,有一个问题想请教: 怎样把这样的xml变为annotation呢? <bean id="name1" class="xxx.Xxx" /> <bean id="name2" class="xxx.Xxx" /> <bean id="name3" class="xxx.Xxx" /> 也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗?
有需要进Q群69563588一起讨论
你好~ 关于spring annotation,有一个问题想请教: 怎样把这样的xml变为annotation呢? <bean id="name1" class="xxx.Xxx" /> <bean id="name2" class="xxx.Xxx" /> <bean id="name3" class="xxx.Xxx" /> 也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗?
<bean id="name1" class="xxx.Xxx" />
<bean id="name2" class="xxx.Xxx" />
<bean id="name3" class="xxx.Xxx" />
你一个类为何要声明三次勒,有意义吗?
不管你调用的是哪个bean名字,最终还是这个类,好像没有必要要那样做吧!
怎样把这样的xml变为annotation呢?
<bean id="name1" class="xxx.Xxx" />
<bean id="name2" class="xxx.Xxx" />
<bean id="name3" class="xxx.Xxx" />
也就是,同一个类(xxx.Xxx),有三个id。使用@Component,似乎做不到。难道这种情况不能用annotation吗?
发表评论
-
Mysql的常用命令收集
2009-04-11 21:38 1108一、连接MYSQL。 格式: mysql -h主机 ... -
javaMail邮件发送
2009-02-14 10:23 2072javaMail邮件发送 利用Sun公司提供的JavaMa ... -
什么是单例模式(singleton)?
2009-02-13 09:49 4261设计模式,单元测试,重构是今天的程序员必须掌握的程序设计技术. ... -
Spring Annotation(五)
2009-02-12 17:25 12576Spring Annotation(五) 使用 JSR-2 ... -
Spring Annotation(四)
2009-02-12 17:23 6221Spring Annotation(四) 当 ... -
Spring Annotation(三)
2009-02-12 17:22 4640Spring Annotation(三) 使 ... -
Spring Annotation(二)
2009-02-12 17:21 3150Spring Annotation(二) 在使用注释配置之 ... -
Spring 2.5 Annotation(一)
2009-02-12 17:19 3686Spring Annotation(一) 基于注释(Annot ... -
annotation事务配置
2009-02-11 16:53 2146自从spring 2.0以后,配合JDK 1.5以上版本,即可 ... -
JAVA对象转换为XML格式
2009-01-14 14:14 3420JAVA对象转换为XML格式 简单的颤述下如何将JAV ... -
MD5 加密
2008-12-16 14:58 1316MD5加密算法 import java.security. ... -
SPRING MVC基础
2008-12-12 11:18 1510其余的话不多,直接进 ...
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
spring的Annotation注解.
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
Spring的Annotation配置相关讲义
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
Spring_Annotation_AOP
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
Spring IOC Annotation 注入 学习实例
spring annotation 从入门到精通
spring annotation 注解 maven 配套的源码
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记