在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了 @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
Java代码 复制代码
1. <?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"
2. >
3.
4. <context:component-scan base-package=”com.eric.spring”>
5. </beans>
6. 其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
7. @Service public class VentorServiceImpl implements iVentorService {
8. } @Repository public class VentorDaoImpl implements iVentorDao {
9. } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() {
10. }
11. @PreDestroy public void destory() {
12. }
=======================
Spring2.5中使用注解装配属性and组件自动扫描 收藏
Spring2.5中使用注解装配属性
可在Java代码中使用@Resource或者@Autowired注解进行装配,但需在XML中配置以下信息
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
然后显式的配置<context:annotation-config/>
该配置隐式注册了多个对注解进行解析的处理器,如下列举
AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor
其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器
另外,比较建议使用@Resource注解,而不要使用@Autowired注解
因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的
在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合
并且在使用Spring时,若使用了JSR-250中的注解,如@Resource//@PostConstruct//@PreDestroy
那么还需要Spring安装目录中的SPRING_HOME\\lib\\j2ee\\common-annotations.jar包的支持
这里面的@Resource注解就是在SPRING_HOME\\lib\\j2ee\\common-annotations.jar中的
@Resource注解
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上
@Resource默认按名称装配,名称可以通过name属性指定。当找不到与名称匹配的bean时,才会按类型装配
若注解标注在字段上且未指定name属性,则默认取字段名作为bean名称寻找依赖对象
若注解标注在setter上且未指定name属性,则默认取属性名作为bean名称寻找依赖对象
如果没有指定name属性,并且按照默认的名称仍找不到依赖对象时,它就会按类型匹配
但只要指定了name属性,就只能按名称装配了
@Autowired注解
@Autowired默认是按类型装配对象的,默认情况下它要求依赖对象必须存在
如果允许null值,可以设置它的required属性为FALSE,如@Autowired(required=false)
若想要按名称装配,可以结合@Qualifier注解一起使用,如@Autowired(required=false) @Qualifier("personDaoBean")
Spring2.5的组件自动扫描
在一个稍大的项目中通常会有上百个组件,如果都使用XML的bean定义来配置组件的话
显然会增加配置文件的体积,查找及维护也不方便
而Spring2.5就为我们引入了组件自动扫描机制
它可以在classpath下寻找标注了@Service、@Repository、@Controller、@Component注解的类
并把这些类纳入Spring容器中管理,它的作用和在XML中使用bean节点配置组件是一样的
使用自动扫描机制,则需配置<context:component-scan base-package="com.jadyer"/>启动自动扫描
其中base-package指定需要扫描的包,它会扫描指定包中的类和子包里面类
@Service用于标注业务层组件
@Repository用于标注数据访问组件,即DAO组件
@Controller用于标注控制层组件,如Struts中的Action
分享到:
相关推荐
Spring注解@Component、@Repository、@Service、@Controller区别.doc
NULL 博文链接:https://bijian1013.iteye.com/blog/2064702
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...
凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...@Controller ...@Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone
Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别
4 @Component 5 @Bean 6 @Configuration 三、 注入相关 1 @Resource 2 @Autowired 3 @Qualifier 4 @value 5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @PostMapping 3 @RequestMapping 五、 前后...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
SSH笔记-annotation配置注入关系(使用@Component、@Service、@Repository、@Controller)
- @Component @Controller @Service @Repository 区别? <!-- /TOC --> Spring MVC常见知识点及源码解析 MVC 是什么 / 有什么优点? MVC是一种设计模式,遵循 模型(Model),视图(View) 和 控制器(Controller)的...
spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...
本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...
javaWeb 工具,根据entity自动生成实体类的Controller, Service, ServiceImpl, Repository。并拥有基本的增删改查,是快速搭建网站的工具类
注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam ... • @Component (不推荐使用) • @Scope • @SessionAttributes • @InitBinder
搭建SpringMVC模型,包含控制层(@Controller)、 业务逻辑层Service(@Service)、持久层(@Repository)三层注入方式。
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
NULL 博文链接:https://wolfweis.iteye.com/blog/690406
WebSphere Service Registry and Repository Handbook(文字版)