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 Studio的使用(三)--包不分级、修改包名
1.如果不喜欢将包逐级展开的话,可以将每一个包名都完整展现出来,只需要勾选Flatten Packages. 2.修改包名 3.填写新的包名 4.点击确认
- python之路:进阶 二
c = collections.Counter( Counter({ b = collections.Counter( b.update(c) Counter({ Counter({ ...
- COC建筑拖动的实现
最近在玩COC,多体验一下手游的体验,因为自己毕竟一直是做页游的,有些观念需要转变一下. 好像偏了,玩了几次之后突然想起COC那个地图拖动的自己之前实现过,那是2010年左右的时候,模拟经营类页游大行 ...
- The 2014 ACM-ICPC Asia Mudanjiang Regional
继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了 [A]签到题 [B]树结构,树的中心 [C]-_-/// [D]概率DP [E ...
- Jboss 集群配置
环境配置:CentOS 7 x64 * 3 IP: 172.24.0.100 172.24.0.101 172.24.0.102 服务器配置: 172.24.0.100 ...
- [SQLite]SQL语法
SQLite常用SQL语句 创建表格 sql="CREATE TABLE IF NOT EXISTS MusicList (id integer primary key AutoIncrem ...
- mysql中SQL执行过程详解与用于预处理语句的SQL语法
mysql中SQL执行过程详解 客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的 ...
- css3动画-transition
当css属性改变的时候,控制animation的速度,让属性的变化发生在一段时间之内,而不是立即生效. 语法 transition: <property> <duration> ...
- POJ 3458 Colour Sequence
水题. #include<cstdio> #include<cstring> #include<cmath> + ; char s[maxn], v[maxn], ...
- List学习笔记
List 特点:1.有序.2.可重复. ArrayList: 底层是数组,数组是有下标的. 会自动扩容,底层默认初始化容量是10,扩大之后的容量预设是原来容量的一半(jdk 1.8).以前好像是原容量 ...