Spring静态工厂和扫描器
2015-09-10 16:16
567 查看
<!--*****************************************************************************************--> <!--静态工厂方法 首先建立一个静态工厂类 public class StaticFactory { private static Map<String, persion> maps = new HashMap<String, persion>(); static{ maps.put("p1", new persion()); maps.put("p2", new persion()); } public static persion getpersion(String name) { return maps.get(name); } } --> <bean id = "staticpersion" class = "com.wsl.StaticFactory" factory-method="getpersion"> <constructor-arg value = "p2"></constructor-arg><!--传入getpersion的参数--> </bean> <!--获取实例 persion sPersion = (persion) ctxApplicationContext.getBean("staticpersion"); --> <!--*****************************************************************************************--> <!--注解 注解扫描器 @Component 基本注解 @Respository 持久层注解 @Service 业务层组件 @Controller 表现层组件 在当前的spring中 , 这4类注解可以混用, 因为spring无法区分一个类属于哪一层。 需要在xml配置文件中导入context插件 在此之前,需要配置spring扫描器来扫描哪些包 1)<context:component-scan>语句加入 <context:component-scan base-package="com.wsl.anotation" resource-pattern="/*.class"><!--只扫描特定目录,此例只扫描 com.wsl.anotation下的类,此时不包含子包--> <!--</context:component-scan> --> <!-- autowire resource inject @Component public class annonationservice { @Autowired annotation annotation; @Autowired @Qualifier("annotation") //用于指定实现类,具体来说就是,当annotation是一个接口 //并且, 系统存在两个或以上的类都实现了这个街口,并且都成为 //了springBean的情况下,接口变量就必须指定一个实现类。 annotation annotation2; @Autowired//使用设置方法自动装配,跟上面一样,当接口存在多个实现类的时候,那么变量名需写成实现的类名 //自然设置方法构造出来也就是符合条件了 //比如接口annotation , 实现类 annotation2 //在申请接口时annotation annotation2;就可以自动装配annotation2的实现到接口了 public void setAnnotation2(annotation annotation2) { this.annotation2 = annotation2; } public void show() { System.err.println(annotation); System.out.println(annotation2); } } resource inject类似 -->
相关文章推荐
- Spring MVC
- Struts2 实战(一)
- Spring-IOC
- scope的取值对spring容器创建对象的单/多例的影响
- Java web过滤器验证登录(避免未经登录进入主页)
- java动态代理(JDK和cglib)
- Java: 动手写一个简单的线程池
- spring-1
- [Tools]eclipse build path介绍与配置
- Eclipse 使用——代码自动提示
- 认识 java JVM虚拟机选项 Xms Xmx PermSize MaxPermSize 区别
- java对象和引用--HASHMAP
- Java Web开发简单入门
- 【java】StringTokenizer类的使用
- Java Map 遍历的方法
- 第六章 访问权限控制
- Java对日期的处理
- Eclipse Hibernate 入门小程序
- Spring boot初探
- Java中的static关键字