Spring xml中进行autowired的方式
可以在xml文件中进行autowired;
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"
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">
<bean id="name" class="java.lang.String">
<constructor-arg>
<value>stringBean</value>
</constructor-arg>
</bean>
<bean id="name2" class="java.lang.String">
<constructor-arg>
<value>stringBean2</value>
</constructor-arg>
</bean>
<bean id="instrument" class="com.stono.sprtest.Harmonica"></bean>
<!-- byName注入,可以将Instrument注入,但是无法注入java.lang.String对象 -->
<bean id="singer" class="com.stono.sprtest.Singer" autowire="byName"></bean>
<!-- byType注入,可以将Instrument注入,但是无法注入java.lang.String对象 -->
<bean id="singer2" class="com.stono.sprtest.Singer" autowire="byType"></bean>
<!-- 加入这个之后singer2将无法正常注入,有两个bean符合byType条件; 必须增加autowire-candidate=false条件 -->
<bean id="cymbal" class="com.stono.sprtest.Cymbal" autowire-candidate="false"></bean>
<!-- 使用constructor注入,可以实现java.lang.String类型的注入,而且String名称可以不是name -->
<bean id="singer3" class="com.stono.sprtest.Singer" autowire="constructor"></bean>
<!-- 使用default注入,没有实现bean的注入 -->
<bean id="singer4" class="com.stono.sprtest.Singer" autowire="default"></bean>
</beans>
main:
package com.stono.sprtest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans4 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans4.xml");
Singer singer = (Singer) context.getBean("singer");
System.out.println(singer);
InstrumentI instrument = (InstrumentI) context.getBean("instrument");
System.out.println(instrument);
Singer singer2 = (Singer) context.getBean("singer2");
System.out.println(singer2);
String s = (String) context.getBean("name");
System.out.println(s);
Singer singer3 = (Singer) context.getBean("singer3");
System.out.println(singer3);
Singer singer4 = (Singer) context.getBean("singer4");
System.out.println(singer4); }
}
bean:
package com.stono.sprtest;
public class Singer {
private InstrumentI instrument;
private String name;
public Singer() {
}
public Singer(InstrumentI instrument, String name) {
this.instrument = instrument;
this.name = name;
}
public InstrumentI getInstrument() {
return instrument;
}
public String getName() {
return name;
}
public void setInstrument(InstrumentI instrument) {
this.instrument = instrument;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Singer [instrument=" + instrument + ", name=" + name + "]";
}
}
Spring xml中进行autowired的方式的更多相关文章
- Spring xml中进行面向切面的配置
Spring xml中进行面向切面的配置 XML: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件
通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二). 前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...
- spring的配置文件在web.xml中加载的方式
web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...
- Spring.xml中配置注解context:annotation-config和context:component-scan简述
XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...
- xml中俩种解析方式
两种解析方式 1.from xml.etree import ElementTree as ET 利用ElementTree模块下的xml方法可以把一个字符串类型的东西转换成Element类,从而利用 ...
- Web.xml中四种验证方式
源地址:https://blog.csdn.net/imimi_/article/details/78805642 <security-constraint> 的子元素 <http- ...
- Spring实战(十一) 在Spring XML中配置AOP
如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...
- spring.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 在Spring MVC中使用注解的方式校验RequestParams
概述 Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...
随机推荐
- SUSE Linux Enterprise Server 11 SP1安装图解教程
一.说明:操作系统:SUSE Linux Enterprise Server 11 SP1下载地址:需要注册才能下载二.安装系统 用启动盘成功引导之后,出现下面的界面 系统运维 温馨提醒:qihang ...
- javactript关闭窗体,刷新父窗体
//关闭,父窗口弹出对话框,子窗口直接关闭this.Response.Write("<script language=javascript>window.close();< ...
- how to enable #ifdef macro in the command line of make?
Compilers normally use the -D flags eg Code: test.o: test.cpp $(CC) $(CFLAGS) -DTEST1 test.cpp -o $@
- USACO刷题之路
重拾经典 本科生涯结束了,在大学做的ACM竞赛现在基本忘的差不多了.USACO作为一个经典的题库,本来是面向OI选手的,但是由于题目质量很高所以受到大家的好评,所以我这次就从它开始我的刷题之路吧. 由 ...
- Java的JDBC事务详解
Java的JDBC事务详解 分类: Hibernate 2010-06-02 10:04 12298人阅读 评论(9) ...
- Linux环境变量相关文件
执行顺序为: /etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/ ...
- 关于Discuz与jQuery冲突问题的亲测解决方法
最近的一个项目整合dede和discuz程序,客户要求风格统一,所以有很多样式及特效都是要公用的.其中jQuery库定义的函数$()正好与discuz的comme.js中函数一样,这样就冲突了,导致d ...
- Spring自学教程-声明式事务处理(六)
Spring事务处理分两种: 一.编程式事务:在程序中控制事务开始,执行和提交: 1.1 使用TransactionTemplate, 使用回调函数执行事务,不需要显示开始事务,不需要显示提交事务,但 ...
- Apache 重启时会有报 AH00558
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0 ...
- jQuery扩展两类函数(对象调用,静态调用)
作者:zccst 先看小例子: $(function(){ //扩展方式1-通过对新调用 $.fn.each1=function(){ console.log("hehehehe$.fn.f ...