可以在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的方式的更多相关文章

  1. Spring xml中进行面向切面的配置

    Spring xml中进行面向切面的配置 XML: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件

    通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二).   前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...

  3. spring的配置文件在web.xml中加载的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...

  4. Spring.xml中配置注解context:annotation-config和context:component-scan简述

    XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...

  5. xml中俩种解析方式

    两种解析方式 1.from xml.etree import ElementTree as ET 利用ElementTree模块下的xml方法可以把一个字符串类型的东西转换成Element类,从而利用 ...

  6. Web.xml中四种验证方式

    源地址:https://blog.csdn.net/imimi_/article/details/78805642 <security-constraint> 的子元素 <http- ...

  7. Spring实战(十一) 在Spring XML中配置AOP

    如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...

  8. spring.xml中的配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. 在Spring MVC中使用注解的方式校验RequestParams

    概述   Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...

随机推荐

  1. Tickets 基础DP

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. C语言写的俄罗斯方块

    源:C语言写的俄罗斯方块 2014年最后一天, 任天堂将风靡全球30年的经典游戏<<俄罗斯方块>>下架. 作为全球最畅销的游戏, 其移植版本遍布各个平台. 下面这个是我去年在5 ...

  3. PAT (Advanced Level) 1015. Reversible Primes (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. PAT (Advanced Level) 1009. Product of Polynomials (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  5. nginx 设置进程title

    刚好看到nginx设置进程title的源码,因此做一些总结. linux进程实际是以argv[0]处的值来作为进程的title的,因此若需要修改进程的title只需要修改argv[0]处的值即可. 简 ...

  6. JS操作DOM对象——JS基础知识(四)

    一.JavaScript的三个重要组成部分 (1)ECMAScript(欧洲计算机制造商协会) 制定JS的规范 (2)DOM(文档对象模型)重点学习对象 处理网页内容的方法和接口 (3)BOM(浏览器 ...

  7. The 2014 ACMICPC Asia Regional Beijing Online

    [A]极角排序+树状数组 [B]计算几何,凸包(队友已出) [C]-_-///不懂 [D]数论,概率密度 [E]图的连通性+Floyed传递闭包+bitset [F]贪心 [G]签到题 [H]区间维护 ...

  8. PHP 删除非法UTF-8字符

    //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ? $some_ ...

  9. hadoop第一篇

    1 hadoop整体架构 2 各组件关系 hdfs只是一个存储空间,他的完整名字是分布式文件系统.有名可知他的作用了. hbase是一个内存数据库,简单点说hbase把表啊什么的存在hdfs上.

  10. IIS7无后缀URL部署问题 MVC4 MVC URL映射 windows server 2008

    前言和中间一段都是我找到问题的过程和思维方法.没兴趣的可以直接跳过看后面的问题和解决. 前言: 问题发生在站点完成后,部署到服务器上.以为这个是最轻松的工作.结果悲剧了.windows server ...