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端所用的 ...
随机推荐
- JAVA基础--线程
sleep和wait的区别: 1. sleep是Thread的方法, wait是object的方法 2. sleep占着CPU睡觉, wait等待CPU,不占用CPU 线程是一个程序内部的顺序控制流 ...
- ural1090 In the Army Now
In the Army Now Time limit: 1.0 secondMemory limit: 64 MB The sergeant ordered that all the recruits ...
- go get 代理设置
前提: 假设安装好git 我的FQ方式(也可以使用别的方式): 使用 ishadowsocks方式FQ 临时设置Windows下代理: 在控制台执行如下命令,后面的的代理值根据你具体的代理进行设置 s ...
- SQL面试题——查询课程
题目: 成绩表(Grade),包含字段:GradeID(Int,自增), SNO(int, 学号), CNO(int, 课程号), Score(float,分数) 查询每门课程的平均(最高/最低)分及 ...
- SpringMVC轻松学习-环境搭建(二)
基于spring2.5的采用XML配置的spring MVC项目 注:本项目全部基于XML配置.同时,集成了hibernate.采用的是:spring MVC+hibernate+spring的开发架 ...
- matlab中gatbx工具箱的添加
1. 从http://crystalgate.shef.ac.uk/code/下载工具箱压缩包gatbx.zip 2. 解压gatbx.zip,将其子文件夹genetic放在matlab安装目录too ...
- MySQL create table 语法
MySQL中create table语句的基本语法是: CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definitio ...
- Linux/hp unix/AIX日常巡检脚本(转)
以下为Linux/hp unix/AIX日常巡检脚本,大家可以参考着进行改写,用于自己的服务器. #!/usr/bin/ksh syserrdate=`date +"%m/%d"` ...
- random 函数
Random()在Delphi中,有一随机函数,是这样定义的:function Random [ ( Range: Integer) ]; 其中,参数Range为一整数,该函数返回值也为整数,其范围为 ...
- 用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)(转)
源:用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台) 代码在2011年全国电子大赛结束后(2011年9月3日)发布,多个版本,注释详细. /*********************** ...