一、spring常用注解
1、常用注解
@Componet:通用注解,可用于任何bean,不推荐使用
@Reposity, @Service,@Controller更有针对性注解
@Reposity: 注解DAO类,即持久层
@Service: 注解Service类,即服务层
@Controller:Controller类,即控制层(MVC)
二、类的自动检测和Bean的注册
为了能够检测这些类病注册到相应的Bean需要
<context:annotation-config/> or
<context:annotation-scan/>
<context:annotation-config/>和<context:annotation-scan/>区别
<context:annotation-config/>:扫描方法和成员变量的注解
<context:annotation-scan/>:扫描整个类的注解,包含<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:component-scan base-package="org.example.beanannotation"></context:component-scan>
</beans>
三、定义Bean
Bean名称是由BeanNameGenerator生产的,分为:显示指定,非显示指定,自定义(需要包含无参数的构造器)
举例:
1.非显示
@Controller
public class UserAction extends BaseAction<User>{
……
}
2. 显示
@Service("userService")
public class UserServiceImpl implements UserService {
………
}
3. 自定义
<beans >
<context:component-scan base-package="org.example
name-generator="MyNameGenerator"></context:component-scan>
</beans>
其他可以参见: http://www.cnblogs.com/xdp-gacl/p/3495887.html