Spring静态工厂和扫描器

 1 <!--*****************************************************************************************-->
 2         <!--静态工厂方法
 3             首先建立一个静态工厂类
 4             
 5             public class StaticFactory
 6             {
 7                 private static Map<String, persion> maps = new HashMap<String, persion>();
 8                 
 9                 static{
10                     maps.put("p1", new persion());
11                     maps.put("p2", new persion());
12                 }
13                 public static persion getpersion(String name)
14                 {
15                     return maps.get(name);
16                 }
17             }
18     -->
19         <bean id = "staticpersion" class = "com.wsl.StaticFactory" factory-method="getpersion">
20             <constructor-arg value = "p2"></constructor-arg><!--传入getpersion的参数-->
21         </bean>
22         <!--获取实例
23             persion sPersion = (persion) ctxApplicationContext.getBean("staticpersion");
24         -->
25         
26     <!--*****************************************************************************************-->
27         <!--注解
28                 注解扫描器
29                 @Component    基本注解
30                 @Respository  持久层注解
31                 @Service      业务层组件
32                 @Controller      表现层组件
33         在当前的spring中 , 这4类注解可以混用, 因为spring无法区分一个类属于哪一层。
34         
35         需要在xml配置文件中导入context插件
36         在此之前,需要配置spring扫描器来扫描哪些包
37         1)<context:component-scan>语句加入
38         
39         <context:component-scan base-package="com.wsl.anotation"
40             resource-pattern="/*.class"><!--只扫描特定目录,此例只扫描 com.wsl.anotation下的类,此时不包含子包-->
41         <!--</context:component-scan>
42         -->
43         
44         
45         <!--  autowire resource inject
46         @Component
47         public class annonationservice 
48         {
49 
50             @Autowired
51             annotation annotation;
52             
53             @Autowired
54             @Qualifier("annotation") //用于指定实现类,具体来说就是,当annotation是一个接口
55                                      //并且, 系统存在两个或以上的类都实现了这个街口,并且都成为
56                                      //了springBean的情况下,接口变量就必须指定一个实现类。
57             annotation annotation2;
58 
59             @Autowired//使用设置方法自动装配,跟上面一样,当接口存在多个实现类的时候,那么变量名需写成实现的类名
60                       //自然设置方法构造出来也就是符合条件了
61                       //比如接口annotation , 实现类 annotation2
62                       //在申请接口时annotation annotation2;就可以自动装配annotation2的实现到接口了
63             public void setAnnotation2(annotation annotation2) {
64                 this.annotation2 = annotation2;
65             }
66 
67             public void show()
68             {
69                 System.err.println(annotation);
70                 System.out.println(annotation2);
71             }
72         }
73         
74         resource inject类似 
75         -->
76         

 

posted @ 2015-09-10 16:16  仰望天空+脚踏实地  阅读(289)  评论(0编辑  收藏  举报