Spring Ioc 常用注解

      在开发中spring ioc的配置方式有多种方式,常用的一般都是byName、byType 、以及其自动装配可以查看http://www.cnblogs.com/gubai/p/9140840.html ,其实这些方式也能满足开发的日常需求但与Annotation 配置方式相比较 其配置方式也显得比较繁琐。下边介绍下其常用注解及其用法:@Autowired 

1.Autowired注解 

 1.Autowire注解可以修饰成员变量    配置Ioc实例如下:

 1 //Cat 类
 2 public class Cat {
 3 private String Catname="小猫咪";
 4 
 5 public String  toString(){
 6     
 7     return "CatName:"+Catname;
 8 }
 9 //Lion 类
10 public class Lion {
11     private String LionName="狮子王";
12 
13     public String toString(){
14         
15         return "LionName:"+LionName;
16     }
17 }
18 //Aniaml
19 public class Animal{
20     @Autowired
21     private Cat cat;
22     @Autowired
23     private Lion lion;
//注意这里边并没有setter getter方法
24 25 public String toString (){ 26 return cat+ "\n"+lion; 27 } 28 29 } 30 //就是简单的把Cat 类和Lion 注入到Animal 中

配置文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:util="http://www.springframework.org/schema/util"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 8         <!--byname bytype 配置时没有这一行的-->
 9         <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
10         
11         <bean id="animal"  class="com.myspring2.annotation.Animal"></bean>
12         
13         <bean id="cat"  class="com.myspring2.annotation.Cat">
14         </bean>
15     
16         <bean id="lion"  class="com.myspring2.annotation.Lion">
17         </bean> 
18         
19         </beans>

测试:

public class Main {
    public static void main(String[] args) {
        ApplicationContext  ac= new ClassPathXmlApplicationContext("applicationContext.xml");
        Animal animal=ac.getBean(Animal.class);
                
        System.out.println(animal.toString());
    }

}

答应结果:CatName:小猫咪

               LionName:狮子王

 解读:很显然我们得到了想要的结果,当spring容器启动时AutowiredAnnotationBeanPostProcessor会自动的扫描spring容器中所有的bean,当其发现有@Autowired注解标注是,就会将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,注入到该类中去。不需要再Animal类中写set get 方法。

  @Autowired 除了能修饰成员变量之外,还能修饰setter方法 以及带参数的构造方法 。

  需要注意的是:当使用这个@Autowired是应该保证spring 容器中当且仅当只有一个合适的bean,否则会报错BeanCreationException,解决方法是@Autowreid(required=false)表示的意思是找不到合适的Bean也不会抛出异常。

2.Qualifier(指定注入Bean的名称)

     当spring容器中找到1个以上的bean是,会抛出异常,测试代码如下:

 1 //猫科动物接口
 2 public interface Felidae {
 3      public String getName();
 4 }
 5 
 6 //Cat 类
 7 public class Cat implements Felidae{
 8     String catName="小猫咪";
 9     public String getName(){
10         return catName;
11     }
12 
13 }
14 public class Lion implements Felidae{
15     String lionName="狮子王";
16     public String getName(){
17         return lionName;
18     }
19 
20 }
21 public class Animal {
22     @Autowired
23     Felidae felidae;
24     public String toString(){
25         return felidae.getName();
26     }
27 
28 }

 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     <context:component-scan base-package="com.myspring2.annotation" />
13        <!--自动扫描上边的包以及其子包 需要开启-->
14         <bean id="animal"  class="com.myspring2.annotation.Animal"></bean>
15         
16     <bean id="cat"  class="com.myspring2.annotation.Cat">
17         </bean> 
18     
19         <bean id="lion"  class="com.myspring2.annotation.Lion">
20         </bean> 
21 
22 </beans>
23          

Cat类和Lion 类实现了同一个几口Felidae, 当在Animal 类中需要注入Felidae 是程序抛出了异常,原因是spring 找到了两个匹配的bean,所以无法确定 到底需要注入那个bean

,解决方法只需要在@Autowired 下边假如@Qualifier("cat")即明确告诉程序我们需要注入的bean,此处就是cat 了。

3.@Resource 次注解和 此注解和@Autowired 注解用法特别相似 ,只是其默认的Byname方法去匹配的,找不到再按Bytype 匹配的 ,@Autowired 是默认按bytype去匹配的

    Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.特定的组件包括以下几个:

  @Component:基本组件 标识了一个受spring 管理的组件,组件管理的通用形式,可以放在类上边, 一般都不推荐使用--开发中使用的比较少

  @Repository :标识持久层组件

        @Service :标识服务层组件

       @Controller:标识表现层组件

       对于这些组件spring有默认的命名策略, 一般是首字母小写,也可以通过注解中value 属性命名。
    简单实例:

 1 //表现层
 2 @Controller
 3 @Scope(value="prototype")
 4 //用scope=prototype 来保证每次访问都是单独的action来处理spring 默认scope 是单例模式(scope="singleton"),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全
 5 public class UserAction {
 6 @Autowired 
 7   UserService userService;
 8     public void save(){
 9         userService.save();
10     }
11 }
12 
13 //服务层
14 public interface UserService {
15     
16 public String save();
17 }
18 
19 @Service
20 public class UserServiceImpl implements UserService{
21    @Autowired
22    UserDao userDao;
23     @Override
24     public String save() {
25     
26         userDao.save();
27         return "ok";
28     }
29 
30 }
31 //持久层
32 @Repository
33 public class UserDao {
34  public void save(){
35      System.out.println("dao save");
36  }
37 }

xml的配置文件中,需要开启自动扫描即可

猜你喜欢

转载自www.cnblogs.com/gubai/p/9229435.html