可以在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. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...

  2. linux command ---1

    查看Linux的内核版本 当前系统的发行版信息(distribution):lsb_release -a , lsb(linux standard Base) and distribution inf ...

  3. Counting Intersections

    Counting Intersections Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. CSS Font-family常用设置

    font-family: "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Lantinghe ...

  5. java中堆和堆栈的区别

    java中堆和堆栈的区别(一) 1.栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取 ...

  6. df换行问题的设置

    df是linux下用来查磁盘空间的命令,而在使用了LVM分区或网络挂载的情况下,再用df取分区的使用率时,发现有些分区显示换行了,这样会导致通过脚本取的数据不对. [root@ ]# df -h Fi ...

  7. Tomcat 静态部署 二步特别注意

    一.修改server.xml 在Host 节点添加如下配置 <!-- path 为请求url地址 docBase 为项目文件绝对地址制定到WebContent根目录下 --> <Co ...

  8. 17、手把手教你Extjs5(十七)模块的新增、修改、删除操作

    上节在Grid展示时做了一个金额单位可以手工选择的功能,如果你要加入其他功能,也只要按照这个模式来操作就行了,比如说你想改变金额字段的颜色.小数位数.零值是否显示.货币符号.单位显示在标题栏或者跟在金 ...

  9. JNI 中文字符串传递(转)

    源:JNI 中文字符串传递 因为项目编码中通过JNI传递中文字符时出现乱码问题,特搜集了相关资料,整理如下: java内部是使用16bit的unicode编码(UTF-16)来表示字符串的,无论中文英 ...

  10. VR元年,VR虚拟现实这只风口上的猪有怎样的变化?

    走过了2016年,无论我们承认不承认,这一年到底是不是VR元年,我们都很难否定,在这一年,VR虚拟现实生态圈有很大的变化,那么,这一年VR虚拟现实到底有怎样的改变呢?我们的VR虚拟现实生态圈,发生了什 ...