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 ...
随机推荐
- IFields Interface 定义一个字段集合对象
Description The Fields object represents a collection of columns in a table. The term field is synon ...
- EXCEL读写NPOI--导出功能
第一步:将NPOI中的一下三个文件复制到项目中
- ural1003 Parity
Parity Time limit: 2.0 secondMemory limit: 64 MB Now and then you play the following game with your ...
- openstack创建实例测试步骤
source admin-openrc.shkeystone user-create --name=demo --pass=123456keystone tenant-create --name=de ...
- [Big Data]从Hadoop到Spark的架构实践
摘要:本文则主要介绍TalkingData在大数据平台建设过程中,逐渐引入Spark,并且以Hadoop YARN和Spark为基础来构建移动大数据平台的过程. 当下,Spark已经在国内得到了广泛的 ...
- Spring整合Hibernate--声明式事务管理
Spring指定datasource 1. 新建jdbc.properties文件: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc: ...
- 51nod1138(math)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1138 题意:中文题诶- 思路:假设 x=a1+(a1+1)+ ...
- mongodb 分片群集(sharding cluster)
实际环境架构 分别在3台机器运行一个mongod实例(称为mongod shard11,mongod shard12,mongod shard13)组织replica set1,作为cluster的s ...
- Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
面向对象:曾经使用过SVN的同学. (因为Git 它 可以说是双重的SVN (本地一个服务器,远程一个服务器)),提交代码要有两次步骤,先提交到本地服务器,再把本地服务器在提交到远程服务器. 所以连S ...
- precision、recall、accuracy的概念
机器学习中涉及到几个关于错误的概念: precision:(精确度) precision = TP/(TP+FP) recall:(召回率) recall = TP/(TP+FN) accuracy: ...