一、使用注解目的:
- 当bean过多时,会引起xml文件的过于臃肿,庞大,难以维护
- 加强java bean的内聚度
二、常用注解概述:
- @Service用于标注业务层组件、
- @Controller用于标注控制层组件(如struts中的action)
- @Repository用于标注数据访问组件,即DAO组件。
- @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”) 存在多个实例配合使用@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
- @PreDestroy 摧毁注解 默认 单例 启动就加载
- @Async异步方法调用
- @Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了 @Bean,就会作为这个Spring容器中的Bean。
- @Scope用于指定scope作用域的(用在类上)
- @PostConstruct用于指定初始化方法(用在方法上)
- @PreDestory用于指定销毁方法(用在方法上)
- @DependsOn:定义Bean初始化及销毁时的顺序
- @Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
- @Lazy(true) 表示延迟初始化
前面的更为常见!
三、详述
在使用注解前:要将使用注解的类都加载进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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
<!-- 这里使用base-package来引入这个包下的所有类-->
<context:component-scan base-package="com.annotation" />
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
1、@Autowired
@Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的setter与bean属性中的property。
@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。
package com.annotation.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.annotation.dao.AnnotationDao;
import com.annotation.service.AnnotationService;
@Service
@Scope("prototype")//这里的Scope和xml文件的用法一致
public class AnnotationServiceImpl2 implements AnnotationService {
@Autowired
private AnnotationDao ad;
@Override
public void p() {
System.out.println("Service注解测试2:"+this.hashCode());
ad.p();//加注解@Autowired就可以将dao注入到service中,同样在setter方法上,构造方法上,都可以注入成功,而不需要在xml中配置
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
如上,加上注解,这样在xml中就不用配置<bean></bean>
了,同样在类上加这个注解也可以。
假如:在xml中配置了bean而且有property(即没有注释的那种配置),但是,却没有setter方法,而给这个属性加了@Autowired,这样会成功吗?
答案是否定的,因为:Spring会按照xml优先的原则去相应的java文件中,” role=”presentation” style=”position: relative;”>答案是否定的,因为:Spring会按照xml优先的原则去相应的java文件中,答案是否定的,因为:Spring会按照xml优先的原则去相应的java文件中,
寻找这两个属性的setter方法来配置” role=”presentation” style=”position: relative;”>寻找这两个属性的setter方法来配置寻找这两个属性的setter方法来配置
2、@Scope
如上:scope可以指定模式,默认还是singleton,即单例模式。”prototype”表示原型即每次都会new一个新的出来。
3、@Service
@Service和@Controller、@Repository、@Component其实都是一样的,当你知道这个类具体是干什么的就用具体的注解,如果不知道就用@Component。(概述中已经叙述清楚)
另外:这里的@Service,干了两件事,1:声明该类是一个bean,这样才可以使用@Autowired来自动注入。2:这个类的bean的id是类名,且首字母小写,你也可以这样做,@Service("这里你是想要的id名")
4、@Autowired @Qualifier(“指定bean”)配套使用
这种情况是:当出现一个接口有多个实现类时来区分使用,例如下面的
AnnotationService,这个接口就有两个实现类。
package com.annotation;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.annotation.service.AnnotationService;
@Component
public class QualifierResoutce {
@Autowired
@Qualifier("annotationServiceImpl2")
// @Resource(name="annotationServiceImpl2")
public AnnotationService as;
@Resource
public List<AnnotationService> list;
@Resource
public Map<String,AnnotationService> map;
public void p(){
System.out.println("QualifierResoutce");
as.p();
}
public void print(){
System.out.println("-----list存放------");
for(AnnotationService a:list){
System.out.println(a.getClass().getName());
}
System.out.println("-----map存放------");
for(Entry<String, AnnotationService> e:map.entrySet()){
System.out.println(e.getKey()+" "+e.getValue());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
上面的@Component,就是不知道这个类是干嘛的,所以就使用这个注解,
而这两个注解的配套使用就是,自动注入这个属性,并且走的是这个属性(接口)的哪个实现类
5、@Resource
上面也出现了这个注解,这个注解和@Autowired非常类似。
@Resource的装配顺序:
(1)、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
(2)、指定了name或者type则根据指定的类型去匹配bean
(3)、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
@Resource和@Autowired的区别:
(1)、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
(2)、@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了
Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。
6、@Controller
@Controller对应表现层的Bean,也就是Action,当使用这个注解以后,spring容器中就会有一个和这个注解的类名相同(首字母小写)的action,
也可以这样@Controller(“ActionName”)来指定名字
同样@Repository,也是这样的