注解:
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,
Spring引入了注解,通过"@XXX"的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。
1、注入属性(将被依赖的bean注入)
- @Autowired:
就是自动装配,默认按类型匹配的方式(byType),在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。
<context:component-scan base-package="com.spring" />
<bean id="zoo" class="com.spring.model.Zoo" />
<bean id="tiger" class="com.spring.model.Tiger" />
<bean id="monkey" class="com.spring.model.Monkey" />
@Autowired
private Tiger tiger;
@Autowired
private Monkey monkey;
public String toString(){
return tiger + "\n" + monkey;
}
- @Qualifier(指定注入Bean的名称)
如果容器中有一个以上同种类型匹配的Bean,则可以通过@Qualifier注解限定Bean的名称,
配置文件:
<context:component-scan base-package="com.spring" />
<!-- Autowired注解配合Qualifier注解 (两个car)-->
<bean id="carFactory" class="com.spring.model.CarFactory" />
<bean id="bmwCar" class="com.spring.service.impl.BMWCar" />
<bean id="benz" class="com.spring.service.impl.BenzCar" />
@Autowired
@Qualifier("bmwCar") //指定bean名称注入(不指定那种car会出错!)
private ICar car;
public String toString(){
return car.getCarName();
}
- @Resource
@Resource注解与@Autowired注解作用非常相似,
@Resource(name="tiger")
private Tiger tiger;
@Resource(type=Monkey.class)
private Monkey monkey;
public String toString(){
return tiger + "\n" + monkey;
}
这是详细一些的用法,说一下@Resource的装配顺序:
(1)、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
(2)、指定了name或者type则根据指定的类型去匹配bean
(3)、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
然后,区分一下@Autowired和@Resource两个注解的区别:
(1)、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
(2)、@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了
2、注解类:通过注解产生bean
@Component, @Controller, @Service, @Repository, 这几个注解不同于上面的注解,上面的注解都是将被依赖的bean注入进入,而这几个注解的作用都是生产bean, 这些注解都是注解在类上,将类注解成spring的bean工厂中一个一个的bean。
首先:.xml
<--打开注解(整个包)-->
<context:component-scan base-package="com.spring" />
-
@Component是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。
-
@Controller: web层()
@Controller
@Scope("prototype")//设置范围
public class UserAction extends BaseAction<User>{
……
}
使用@Controller注解标识UserAction之后,就表示要把UserAction交给Spring容器管理,在Spring容器中会存在一个名字为"userAction"的action,这个名字是根据UserAction类名来取的。
注意:
如果@Controller不指定value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value=“UserAction”)】或者【@Controller(“UserAction”)】,则使用value作为bean的名字。
- @Service: service层
@Service("userService")
public class UserServiceImpl implements UserService {
………
}
@Service(“userService”)注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action:在Action只需要声明一个名字叫"userService"的变量来接收由Spring注入的"userService"即可,具体代码如下:
// 注入userService
@Resource(name = "userService")
private UserService userService;
- @Respository: dao层
@Repository对应数据访问层Bean
与上类似!!!!!!!
3、Spring常用注解汇总
@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@Scope注解 作用域
@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@DependsOn:定义Bean初始化及销毁时的顺序
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”) 存在多个实例配合使用
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@PostConstruct 初始化注解
@PreDestroy 摧毁注解 默认 单例 启动就加载
@Async异步方法调用