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 ...
随机推荐
- POJ Sudoku 数独填数 DFS
题目链接:Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18105 Accepted: 8772 Sp ...
- zookeeper系列之异步通知模式-Watcher
1 watcher种类和事件种类 Watcher种类 1. zookeeper实例化时注入的默认Watcher 2. dataWatchers 一个Map<string Set<Watch ...
- MC34063中文资料及应用实例(转)
源:http://blog.chinaunix.net/uid-26199686-id-3207838.html MC34063A(MC33063)芯片器件简介 该器件本身包含了DC/DC变换器所需要 ...
- 在线生成二叉树(基于EaselJS(canvas))
学习二叉树的时候,老在本子上画二叉树好麻烦.其实就想看下树结构.最近html5蛮火的,就用canvas和EaselJS.js(开发flash公司开发的插件)插件实现了个.大家随便用吧. 这是个什么东西 ...
- 【翻译】go语言中的map实战
业余时间翻译,水平很差,如有瑕疵,纯属无能. 原文链接 http://blog.golang.org/go-maps-in-action go语言中的map实战 1. 简介 哈希表是计算机科学中最重要 ...
- [算法] dijkstra单源无负权最小路径算法
#include <stdio.h>#include <stdlib.h>#include <string.h> #define INF 1000000#defin ...
- [zoj解题] 1203
#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXN 100 #define ...
- 在XAMPP上建立多个域名的站点
XAMPP默认安装完毕后,站点文件默认放在/xampp/htdocs/ 文件下,并且可以通过http://localhost 进行访问.先前在测试各种程序的时候均是在/xampp/htdocs/ 文件 ...
- SQL复习五(索引)
SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开他的神秘面纱. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引 ...
- 利用rem实现webapp布局
rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了. 但是我对rem综合评价是用来做web app它绝对是最合适的人选之一 ...