@Component @Service @Controller @Repository注解使用
2014-02-26 16:45
453 查看
@Component 相当于实例化类的对象。
通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。
要使用自动扫描机制我们需要打开一下配置信息:
Bean.xml代码
<?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.zchen" />
</beans>
注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。
Java代码
@Component ( "userService" )
public class UserService {
@Service用于标注业务层组件、
@Controller用于标注空竹曾组件(如Struts中的action)
@Repository用于标注数据访问组件即DAO组件
@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解)
通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。
要使用自动扫描机制我们需要打开一下配置信息:
Bean.xml代码
<?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.zchen" />
</beans>
注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。
Java代码
@Component ( "userService" )
public class UserService {
@Service用于标注业务层组件、
@Controller用于标注空竹曾组件(如Struts中的action)
@Repository用于标注数据访问组件即DAO组件
@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解)
相关文章推荐
- @Component @Service @Controller @Repository注解使用
- 使用component,repository,Service,controller注解
- @Service@Repository@Controller@Component注解的使用
- Spring中的注解:@Repository、@Controller、@Service、@Component
- Spring常用注解@Component、@Controller、@ Service、@ Repository......
- spring mvc常用注解@Component @Controller @Service @Repository
- Spring注解@Componen、@Repository@Service、@Controller区别与Spring2.5中使用注解装配属性and组件自
- Spring @Repository、@Service、@Controller 和 @Component 注解作用
- spring mvc常用注解@Component @Controller @Service @Repository
- @Component @Service @Controller @Repository注解使用
- 使用spring注解@Controller @Service @Repository简化配置
- SpringMVC四大注解 Component·Controller·Service·Repository
- SpringMVC四大注解 Component·Controller·Service·Repository
- 使用spring注解@Controller @Service @Repository简化配置
- Spring中注解@Controller@Service@Repository@Component的区别
- spring注解 repository,component,service,controller
- 使用spring注解@Controller @Service @Repository简化配置
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- Spring注解 Component Repository Service Controller区别
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller