3.Spring bean常见属性的注入:applicationContext-beans.xml

本文通过一个具体的XML配置文件示例介绍了如何使用Spring框架来定义和管理Bean,包括基本属性设置、集合类型注入等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<bean id="bean1" class="com.bjpowernode.spring.Bean1">
		<property name="strValue" value="Hello_Spring"/>
		
		<!-- 
		<property name="intValue" value="123"/>
		 -->
		<property name="intValue">
			<value>123</value>
		</property>
		
		<property name="listValue"><!-- 注入ArrayList -->
			<list>
				<value>1</value>
				<value>2</value>
			</list>
		</property>
		<property name="setValue"><!-- 注入LinkedHashSet -->
			<set>
				<value>1.58</value>
				<value>66.8</value>
			</set>
		</property>
		<property name="arrayValue">
			<list>
				<value>array1</value>
				<value>array2</value>
			</list>
		</property>
		<property name="mapValue"><!-- 注入LinkedHashMap -->
			<map>
				<entry key="k1" value="1"/>
				<entry key="k2" value="2"/>
			</map>
		</property>
		<property name="dateValue" value="2009-12-14" />
	</bean>
</beans>

ERROR [main] org.springframework.web.context.ContextLoader Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name &#39;commentService&#39; for bean class [com.xhxy.eshop.service.impl.mybatis.CommentServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.xhxy.eshop.service.impl.jdbc.CommentServiceImpl] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:417) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:338) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:196) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:232) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:203) ~[spring-beans-5.3.15.jar:5.3.15] at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) ~[spring-web-5.3.15.jar:5.3.15] at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) ~[spring-web-5.3.15.jar:5.3.15] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshab
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值