Spring 使用context:annotation-config的设置
Spring 使用context:annotation-config的设置;
还是需要声明Bean的,并且还可能自己定义Annotation;
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">
<!-- 通知使用Annotation的方式进行注入 -->
<context:annotation-config />
<bean id="name" class="java.lang.String">
<constructor-arg>
<value>beanString</value>
</constructor-arg>
</bean>
<!-- 如果将下面的bean注释掉,会因为找不到autowired对象报错; -->
<bean id="cymbal" class="com.stono.sprtest.Cymbal"></bean>
<!-- 如果有两个Instrument接口实现类,必须指定一个的autowire-candidate为false -->
<bean id="harmonica2" class="com.stono.sprtest.Harmonica" autowire-candidate="false"></bean>
<!-- 如果在@Autowired下面增加@Qualifier指定bean的id,就可以使用两个Instrument接口实现类 -->
<bean id="harmonica" class="com.stono.sprtest.Harmonica"></bean>
<!-- @Autowired默认使用byType方式进行注入,可以注入String类型 -->
<bean id="singer" class="com.stono.sprtest.Singer"></bean>
</beans>
AppBean:
package com.stono.sprtest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans5 { @SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans5.xml");
Singer singer = (Singer) context.getBean("singer");
System.out.println(singer);
} }
Annotation:
package com.stono.sprtest; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier; @Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface HarmonicaInstrument {
}
POJO:
package com.stono.sprtest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; public class Singer {
// 可以直接在private属性上面进行设置,也可以在setter方法上面设置;
@Autowired
// @Qualifier("harmonica")
// 使用自定义annotation可以替换@Qualifier,但是在POJO上面会有Annotation
@HarmonicaInstrument
private InstrumentI instrument;
// 增加上required=false,没有string类型的也是可以为空的;
// @Autowired(required = false)
// 可以使用 @Value直接进行注入;
// @Value("annotaionValue")
// 可以使用@Value进行SpEL表达式值的获取;
@Value("#{systemProperties['os.name']}")
private String name;
public Singer() {
}
public Singer(InstrumentI instrument, String name) {
this.instrument = instrument;
this.name = name;
}
@Override
public String toString() {
return "Singer [instrument=" + instrument + ", name=" + name + "]";
}
}
package com.stono.sprtest; @HarmonicaInstrument
public class Harmonica implements InstrumentI { }
Spring 使用context:annotation-config的设置的更多相关文章
- spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
在运行一个第三方公司交付的项目的时候, 出现: Caused by: java.lang.IllegalStateException: Context namespace element 'annot ...
- [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher 问题--MyEclipse设置JDK版本
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...
- Spring配置之context:annotation与、component-scan以及annotation-driven
spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...
- Spring框架context的注解管理方法之二 使用注解注入基本类型和对象属性 注解annotation和配置文件混合使用(半注解)
首先还是xml的配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- Spring MVC 的 Java Config ( 非 XML ) 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置
远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...
- Spring Cloud 系列之 Config 配置中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Config 配置中心(一) 本篇文章讲解 Config 如何实现配置中心自动刷新. 配置中心自动刷新 点击链接观看: ...
- Spring AOP Schema aop:config、tx:advice
Spring AOP Schema aop:config.tx:advice 一. 利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...
- spring中context:property-placeholder/元素
1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的 ...
随机推荐
- 翻译的很好的一篇android mediaplayer
MediaPlayer类可用于控制音频/视频文件或流的播放.关于如何使用这个类的方法还可以阅读VideoView类的文档. 1.状态图对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一 ...
- Kafka 在行动:7步实现从RDBMS到Hadoop的实时流传输
原文:https://coyee.com/article/11095-kafka-in-action-7-steps-to-real-time-streaming-from-rdbms-to-hado ...
- android怎么打开wifi的组播功能
http://android.tgbus.com/Android/tutorial/201204/418987.shtml
- HTML 基础语言
打开DREAMWEAVER,新建HTML.. body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text ...
- JAVA中传递参数乱码问题
url传递中文如果jsp页面,myeclipse.web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8编码,直接 ...
- Leetcode题1
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- lPC1788的GPIO驱动
#include "led.h" void led_init(void) { //p1.14 p0.16 p1.13 p4.27 LPC_SC->PCONP |= (1< ...
- LPC1768的usb使用--硬件篇
LPC1768芯片带有USB设备控制器,前面写的文章都是在说比较简单的设备驱动,今天来说复杂一点的 首先是硬件层的配置 #ifndef __USBHW_H__ #define __USBHW_H__ ...
- Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(三)
继续, 上文提到的是用BUILD_STATIC_JAVA_LIBRARY在Android4.2源码下编译出来的jar包可以在Eclipse(SDK版本4.1)上使用, 找来Android6.0的源码, ...
- NSString总结
[from]http://www.jianshu.com/p/7994b0ad6b88 问题:NSString到底是不是字符串? NSString 是 OC中专门处理字符串的对象!提供了转换大小写,拼 ...