Spring context:component-scan代替context:annotation-config
2015-09-28 12:16
691 查看
Spring context:component-scan代替context:annotation-config
XML:
AppBean:
POJO:
XML:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.stono.sprtest" /> </beans>
AppBean:
package com.stono.sprtest; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans6 { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("appbeans6.xml"); Singer2 singer2 = (Singer2) context.getBean("singer"); System.out.println(singer2); } }
POJO:
package com.stono.sprtest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("singer") // 这里可以指定Bean的ID public class Singer2 { @Autowired // @Component标注的类,其Id是camel-casing类名;两个Intrument接口实现类,必须指定其中的一个; @Qualifier("saxophone") private InstrumentI instrument; @Value("justValue") private String name; @Override public String toString() { return "Singer2 [instrument=" + instrument + ", name=" + name + "]"; } }
package com.stono.sprtest; import org.springframework.stereotype.Component; @Component public class Saxophone implements InstrumentI { private Integer age; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package com.stono.sprtest; import org.springframework.stereotype.Component; @Component public class Cymbal implements InstrumentI { }
相关文章推荐
- 基于spring,Atomikos,mybatis的分布式动态数据源JTA实现
- java设计模式之外观模式
- java的三大特性之多态
- 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现
- JAVA基础学习day18--常用工具类
- spring源码学习(1)-bean包中主要接口部分的简单介绍
- POST JSON fails with 415 Unsupported media type, SpringMVC
- eclispe中的maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.c
- java getConstructor 抛出NoSuchMethodException
- spring MVC 配置返回JSON格式的数据模型
- 主流框架Struct2 Spring Hibernate的理解
- Java线程安全与同步
- 如何 使用eclipse 建立maven3 项目 (入门级)
- Study schedule for new work.
- HIVE Exception in thread "main" java.lang.IncompatibleClassChangeError: Found class jline.Terminal,
- eclipse debug技巧
- java中的for循环
- Spring 使用context:annotation-config的设置
- Java 比较两个日期的方法
- MyEclipse 生成javaDoc文档