随笔 - 72  文章 - 28  trackbacks - 0
<2014年1月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(4)

随笔分类(66)

随笔档案(72)

文章档案(19)

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

在Spring MVC中的配置中一般会遇到这两个标签,作为<context:component-scan>的子标签出现。

但在使用时要注意一下几点:

1.在很多配置中一般都会吧Spring-common.xml和Spring-MVC.xml进行分开配置,这种配置就行各施其职一样,显得特别清晰。

在Spring-MVC.xml中只对@Controller进行扫描就可,作为一个控制器,其他的事情不做。

在Spring-common.xml中只对一些事务逻辑的注解扫描。

2.现在给定一个项目包的机构:

com.fq.controlller

com.fq.service

就先给定这两个包机构

(1)在Spring-MVC.xml中有以下配置:

 

 

 

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。

posted @ 2015-10-29 10:25 kelly 阅读(234) | 评论 (0)编辑 收藏


在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: “Retrieving archetypes:”. Java heap space,可以通过以下步骤来解决问题。 
1. 找到Eclipse的根目录下的eclipse.ini(或myeclipse.ini)文件并打开

2.修改文件中的以下配置

-Dosgi.requiredJavaVersion=1.5(可选)

-Xms512m

-Xmx1024m

这是我的配置,大家可以尝试着修改下,不同的机器配置可能支持的情况不同。

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-10-12 15:09 kelly 阅读(5292) | 评论 (0)编辑 收藏

myeclipse自定义java注释:

Window->Preference->Java->Code Style->Code Template

然后展开Comments节点就是所有需设置注释的元素

-----------------

文件 (Files) 注释标签:

/** 

@Project : ${project_name}

@Title : ${file_name}

@Package ${package_name}

@Description : ${todo}

@author shenyanghong ahong2011@gmail.com

@date ${date} ${time}

@Copyright : ${year} www.1000chi.com Inc. All rights reserved.

@version V1.0 

*/

类 (Types) 注释标签(类的注释):

/**

  * @ClassName ${type_name}

  * @Description ${todo}

  * @author shenyanghong ahong2011@gmail.com

  * @date ${date}

  * ${tags}

*/

字段 (Fields) 注释标签:

/** 
* @Fields ${field} : ${todo}
*/ 


构造函数标签:

/** 
* <p>Title: </p> 
* <p>Description: </p> 
* ${tags} 
*/

 

方法 (Constructor & Methods) 标签:

/** 
* @Title: ${enclosing_method} 
* @Description: ${todo}
* @param ${tags}    
设定文件 
* @return ${return_type}    
返回类型 
* @throws 
*/

覆盖方法 (Overriding Methods) 标签:

/* ( 非 Javadoc) 
* <p>Title: ${enclosing_method}</p> 
* <p>Description: </p> 
* ${tags} 
* ${see_to_overridden} 
*/

 

代表方法 (Delegate Methods) 标签:

/** 
* ${tags} 
* ${see_to_target} 
*/ 


getter
 方法标签:

/** 
* @return ${bare_field_name} 
*/

setter 方法标签:

/** 
* @param ${param} 
要设置的 ${bare_field_name} 
*/

posted @ 2015-06-30 10:38 kelly 阅读(238) | 评论 (0)编辑 收藏

今天将写好的附件服务器的API发给同事

 

她引入我的jar后, 编译就会报错: 类文件具有错误的版本 50.0,应为 49.0

 

50.0 对应的是JDK的1.6版本, 而49.0 对应的是JDK的1.5版本

 

也就是说我的jar的版本高于她所用的版本

 

由于我们实际部署在1.5之上, 所以我就来修改我的编译环境

 

首先我先修改了Eclipse的编译环境到1.5, 但是没有效果

 

转眼一想, 我都是使用Ant来打包发布, 看来Ant是自己编译的

 

于是我就在网上找到了修改Ant编译版本的方法

 

 

 

最后完成了修改

 

写个文字记录下, 免得以后忘了= =

 

 

PS: 我发现很多人问如何查看class文件是什么版本JDK编译的, 现在我将方法写在下面:

 

使用UtralEdit打开一个class文件.

 

根据java虚拟机的规范, java的class文件的前4个字节为magic number(魔数), 0xCAFEBABE(下图的第一行0 - 3列), 标识这个文件是java的class文件

 

而紧随其后的4个字节, 存储的就是该class文件的主次版本号(下图的第一行的 4 - 7 列), 下图中的31 换算成十进制就是49, 这标识此class文件为JDK1.5编译所得, 若32 就是JDK1.6编译

 

posted @ 2015-03-03 16:08 kelly 阅读(408) | 评论 (0)编辑 收藏
<mvc:annotation-driven />注解意义

<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

posted @ 2014-11-16 22:42 kelly 阅读(160) | 评论 (0)编辑 收藏
1、确保导入了jackson-core-asl-1.9.13.jar和jackson-mapper-asl-1.9.13.jar包
2、在spring的配置文件中加入<mvc:annotation-driven />这句,它提供了读取jason的支持
3、使用springMVC的@ResponseBody注解
@responsebody表示该方法的返回结果直接写入HTTP response body中
一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。
4、在以上配置都正确的情况下,我的项目还是不能返回json串。报错:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()。
今天终于在一个外文网站找到答案,是由于spring版本的问题引起的。我之前一直用的是3.0.0的版本。就是因为这个版本的问题。于是果断去官网下载3.2版本的,一切正常运行,成功返回json数据。
posted @ 2014-11-16 22:41 kelly 阅读(14128) | 评论 (1)编辑 收藏

struts和spring整合首先要在Web容器启动的时候自动装配ApplicationContext的配置信息,可想而知应该在web.xml做相应的配置:
[html]
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
        classpath:applicationContext.xml 
    </param-value> 
</context-param> 
<listener> 
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
   </listener> 
配置了org.springframework.web.context.ContextLoaderListener后我们就不惜要编写代码显示地实例化ApplicationContext对象了。至于为什么要使用监听是因为web.xml 的加载顺序是:context-param -> listener -> filter -> servlet 。如果你是在不想使用监听,或许你可以尝试下继承struts2的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter重写这个它的init方法在StrutsPrepareAndExecuteFilter过滤器init中实例化ApplicationContext对象加载配置信息,虽然这种方法也可行,但是当拦截每个action都会加载一次配置信息,重新实例化了一个新的web容器,不仅浪费了资源也让spring更加依赖了struts。

1、使用xml方式:
struts2配置
<package name="user" extends="struts-default">
<action name="login" class="userAction">
<result name="success">/success.jsp</result>
<result name="input" type="redirect">/index.jsp</result>
</action>
</package>
spring配置
<bean id="userDao" class="org.han.dao.impl.UserDaoImpl" />
<bean id="biz" class="org.han.service.impl.LoginBizImpl">
<property name="userdao" ref="userDao"/>
</bean>

<bean id="userAction" class="org.han.action.LoginAction" scope="prototype" >
<property name="biz" ref="biz" />
</bean>
注意红色部分,struts2的action class与对应的action bean必须相同,这样才能由spring管理action;

2、struts2使用零配置方式:
当你导入了零配置插件包的时候千万要注意约定大于配置,还是上面的spring配置,只是不需要struts2配置了。
第一种方式:只需要将Action的className对应到spring配置中的bean id就行了
@Action(value = "/login", results = { @Result(name = "success", location = "/success.jsp"),@Result(name="input",location="/index.jsp")},className="userAction")
public String login() throws Exception {
// TODO Auto-generated method stub
User u=biz.login(this.getUser());
if(u!=null){
return SUCCESS;
}
return INPUT;
}
第二种方式:
Action注解不需要className了,将spring配置稍作修改
<bean id="org.han.action.LoginAction" class="org.han.action.LoginAction" scope="prototype" >
<property name="biz" ref="biz" />
</bean>
这样可以是因为当你使用零配置的时候,action的class默认是当前类的全类名,所以和spring整合的时候刚好使用全类名在spring配置中查找以全类名为id的bean。

3、struts2、spring都使用注解方式:
www.2cto.com
<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-3.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
   <context:component-scan base-package="org.han.dao.impl,org.han.service.impl,org.han.action"/> 
</beans> 

<context:component-scan base-package=""/>用此种方式,不需要在配置文件中再配置bean,也不需要再导入上面对应的处理bean。也就是说可以不需要在配置文件中使用<context:annotation-config/>了,因为此种方式会自动导入

[java]
@Namespace("/") 
@Component(value="userLogin") 
@Scope(value="prototype") 
public class LoginAction extends ActionSupport { 
 
    public LoginAction() { 
        super(); 
        // TODO Auto-generated constructor stub 
        System.out.println("action:"+this.hashCode()); 
    } 
     
    @Autowired 
    private ILoginBiz biz; 
    private User user; 
 
    public User getUser() { 
        return user; 
    } 
 
    public void setUser(User user) { 
        this.user = user; 
    } 
    @Autowired 
    public void setBiz(ILoginBiz biz) { 
        this.biz = biz; 
    } 
 
    @Override 
    @Action(value = "hello", results = { @Result(name = "success", location = "/success.jsp"),@Result(name="input",location="/index.jsp")}) 
    public String execute() throws Exception { 
        // TODO Auto-generated method stub 
        System.out.println("biz:"+this.biz.hashCode()); 
        User u=biz.login(this.getUser()); 
        if(u!=null){ 
            return SUCCESS; 
        } 
        return INPUT; 
    } 

@Component 有一个可选的入参,用于指定 Bean 的名称。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。

@Scope用于定义Bean的作用范围。

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法删除。

@Qualifier(“name”) 中的 name是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。

@PostConstruct 和 @PreDestroy:JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,这两个注释只能应用于方法上。标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。
通过 <bean> 元素的 init-method/destroy-method 属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct 和 @PreDestroy 注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct 或@PreDestroy 注释的方法都会在初始化 / 销毁时被执行。
更多的关于注解使用:请看官方文档

4、总结:
1、注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
2、如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。
3、注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。
所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。

posted @ 2014-01-17 21:51 kelly 阅读(271) | 评论 (0)编辑 收藏

来自:http://hanyexiaoxiao.iteye.com/blog/410123
1. 使用Spring注解来注入属性 
1.1. 使用注解以前我们是怎样注入属性的 
类的实现:

public class UserManagerImpl implements UserManager {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	...
}


配置文件:

<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
	<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
	<property name="sessionFactory" ref="mySessionFactory" />
</bean>



1.2. 引入@Autowired注解(不推荐使用,建议使用@Resource) 
类的实现(对成员变量进行标注)

public class UserManagerImpl implements UserManager {
	@Autowired
	private UserDao userDao;
	...
}


或者(对方法进行标注)

public class UserManagerImpl implements UserManager {
	private UserDao userDao;
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	...
}


配置文件

<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
	<property name="sessionFactory" ref="mySessionFactory" />
</bean>


@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。以上两种不同实现方式中,@Autowired的标注位置不同,它们都会在Spring在初始化userManagerImpl这个bean时,自动装配userDao这个属性,区别是:第一种实现中,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;第二种实现中,Spring会调用setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。 

1.3. 让@Autowired工作起来 
要使@Autowired能够工作,还需要在配置文件中加入以下代码

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />



1.4. @Qualifier 
@Autowired是根据类型进行自动装配的。在上面的例子中,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。 
1. 可能存在多个UserDao实例

	@Autowired
	public void setUserDao(@Qualifier("userDao") UserDao userDao) {
		this.userDao = userDao;
	}


这样,Spring会找到id为userDao的bean进行装配。 
2. 可能不存在UserDao实例

	@Autowired(required = false)
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}



1.5. @Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解) 
Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。 
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。 
@Resource装配顺序

  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;



1.6. @PostConstruct(JSR-250) 
在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。 
它的一个典型的应用场景是,当你需要往Bean里注入一个其父类中定义的属性,而你又无法复写父类的属性或属性的setter方法时,如:

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
	private SessionFactory mySessionFacotry;
	@Resource
	public void setMySessionFacotry(SessionFactory sessionFacotry) {
		this.mySessionFacotry = sessionFacotry;
	}
	@PostConstruct
	public void injectSessionFactory() {
		super.setSessionFactory(mySessionFacotry);
	}
	...
}


这里通过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了我们自己定义的sessionFactory(父类的setSessionFactory方法为final,不可复写),之后我们就可以通过调用super.getSessionFactory()来访问该属性了。 

1.7. @PreDestroy(JSR-250) 
在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行。由于我们当前还没有需要用到它的场景,这里不不去演示。其用法同@PostConstruct。 

1.8. 使用<context:annotation-config />简化配置 
Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。 
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是<context:annotation-config />:

<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 />
</beans>


<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。 

2. 使用Spring注解完成Bean的定义 
以上我们介绍了通过@Autowired或@Resource来实现在Bean中自动注入的功能,下面我们将介绍如何注解Bean,从而从XML配置文件中完全移除Bean定义的配置。 

2.1. @Component(不推荐使用)、@Repository、@Service、@Controller 
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

@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.2. 使用<context:component-scan />让Bean定义注解工作起来

<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表达式过滤一些类


以正则表达式为例,我列举一个应用实例:

	<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 />移除了。 

2.3. 使用@Scope来定义Bean的作用范围 
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:

@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
	...
}



3. 参考 
http://kingtai168.iteye.com/blog/244002 
http://www.iteye.com/topic/244153 
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config 
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning

posted @ 2014-01-17 12:00 kelly 阅读(237) | 评论 (0)编辑 收藏