Spring常用注解

Spring引入注解之前,使用.xml对bean进行注入或者配置aop、事务,缺点是.xml文件过于庞大,开发过程中 .java和.xml经常切换,开发效率低。

传统Spring的示例 ,参照这里很好的例子:

https://www.cnblogs.com/xiaoxi/p/5935009.html

1.@Autowired

   自动装配,作用是消除代码Java代码里面的getter/setter与bean属性中的property。

   不使用注解 :

 1 package com.spring.model;
 2 
 3 public class Zoo {
 4     private Tiger tiger;
 5     private Monkey monkey;
 6     
 7     public Tiger getTiger() {
 8         return tiger;
 9     }
10     public void setTiger(Tiger tiger) {
11         this.tiger = tiger;
12     }
13     public Monkey getMonkey() {
14         return monkey;
15     }
16     public void setMonkey(Monkey monkey) {
17         this.monkey = monkey;
18     }
19     
20     public String toString(){
21         return tiger + "\n" + monkey;
22     }
23     
24 }

 spring配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context-3.0.xsd
11     ">
12     
13      <bean id="zoo" class="com.spring.model.Zoo" >
14         <property name="tiger" ref="tiger" />
15         <property name="monkey" ref="monkey" />
16     </bean>
17     
18     <bean id="tiger" class="com.spring.model.Tiger" />
19     <bean id="monkey" class="com.spring.model.Monkey" />
20 
21 </beans>

  使用注解后:

 1 package com.spring.model;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 public class Zoo {
 6     
 7     @Autowired
 8     private Tiger tiger;
 9     
10     @Autowired
11     private Monkey monkey;
12     
13     public String toString(){
14         return tiger + "\n" + monkey;
15     }
16     
17 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context-3.0.xsd
11     ">
12     
13     <context:component-scan base-package="com.spring" />
14     
15     <bean id="zoo" class="com.spring.model.Zoo" />
16     <bean id="tiger" class="com.spring.model.Tiger" />
17     <bean id="monkey" class="com.spring.model.Monkey" />
18 
19 </beans>

 如果干掉16、17行的bean,Spring找不到@Autowired 标记的属性 tiger、monkey,会抛出报错信息:found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency


解决办法是 @Autowired注解的required属性设置为false即可。

2.@Qualifier 

 指定注入bean的名称。容器中有一个以上匹配的Bean,则可以通过@Qualifier注解限定Bean的名称

3.@Resource

  Resource与Autowire作用相似,其装配顺序:

  (1) @Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配;

  (2) 指定了name或者type则根据指定的类型去匹配bean;

  (3) 指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错。

   两者的区别:

   (1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配;

   (2)@Autowired是Spring的注解,@Resource是J2EE的注解;建议使用@Resource注解,以减少代码和Spring之间的耦合。

4、@Service

    简化掉.xml文件中的定义,参照例子

   Zoo.java在Spring容器中存在的形式就是"zoo",即可以通过ApplicationContext的getBean("zoo")方法来得到Zoo.java

   Service完成两件事情:

   (1)声明Zoo.java是一个bean,这点很重要,因为Zoo.java是一个bean,其他的类才可以使用@Autowired将Zoo作为一个成员变量自动注入;

   (2)Zoo.java在bean中的id是"zoo",即类名且首字母小写。

 1 package com.spring.model;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 public class Zoo {
 8     
 9     @Autowired
10     private Tiger tiger;
11     
12     @Autowired
13     private Monkey monkey;
14     
15     public String toString(){
16         return tiger + "\n" + monkey;
17     }
18     
19 }

使用注解来构造IoC容器
用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册<context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>。

例如:在base-package  指定包,表示 cn.gacl.java及其子包中的对象作为bean注册进Spring容器。

<context:component-scan base-package="cn.gacl.java"/>

也可以使用注解【@Component/@Repository/@Service/@Controller】,实现相同功能。

5.@Component

  @Component是所有受Spring 管理组件的通用形式;@Component注解可以放在类的头上,@Component不推荐使用。

6.@Controller

  @Controller对应表现层的Bean,也就是Action

7.@ Repository

   数据访问层Bean 例如:

@Repository(value="userDao")
public class UserDaoImpl extends BaseDaoImpl<User> {
………
}

@Repository(value="userDao")注解是告诉Spring,让Spring创建一个名字叫"userDao"的UserDaoImpl实例。

当Service需要使用Spring创建的名字叫"userDao"的UserDaoImpl实例时,就可以使用@Resource(name = "userDao")注解告诉Spring,Spring把创建好的userDao注入给Service即可。

// 注入userDao,从数据库中根据用户Id取出指定用户时需要用到
@Resource(name = "userDao")
private BaseDao<User> userDao;

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异步方法调用

  

猜你喜欢

转载自www.cnblogs.com/albertarmstrong/p/8946493.html