spring基础——普通bean xml注入

本文详细介绍了Spring框架中bean的XML注入,包括基本类型、对象类型、数组、list、set、map等类型的属性注入方式,并展示了如何通过ClassPathXmlApplicationContext获取bean。

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

一、基本类型注入

1、set注入,前提是类中声明了属性的set方法 使用\
2、有参构造器注入,前提是类中声明了有参构造器 使用<constructor-arg name=“参数名” value=“参数值”>
3、注入含特殊符号值,使用<![CDATA[<<南京>>]]> 实际值为<<南京>>
4、p标签注入,需引入p名称空间 xmlns:p=“http://www.springframework.org/schema/p”

<!--配置User对象创建-->
<bean id="user" class="spring5.User"></bean>

<!--set方法注入属性-->
<bean id="book" class="spring5.Book">
	<!--使用property完成属性注入-->
	<property name="name" value="易筋经"></property>
	<property name="author" value="达摩老祖"></property>
</bean>

<!--有参构造器注入属性-->
<bean id="orders" class="spring5.Orders">
	<constructor-arg name="name" value="北京"></constructor-arg>
	<constructor-arg name="address" value="中国"></constructor-arg>
</bean>

<!--p名称空间注入-->
<bean id="book" class="spring5.Book" 
	p:name="九阳生工" 
	p:author="无名氏">
</bean>

<!--null值注入-->
<bean id="book" class="spring5.Book">
	<property name="name" value="helloWord"></property>
	<!--注入null值-->
	<property name="author">
		<null/>
	</property>
</bean>

<!--属性值包含特殊符号-->
<bean id="book" class="spring5.Book">
	<property name="author">
		<value><![CDATA[<<南京>>]]></value>
	</property>
	<property name="name" value="asd"></property>
</bean>

二、对象类型注入

方式一:外部bean。创建对象,然后在property 标签中使用ref属性将其注入:<property name=“userDao” ref=“userDaoImpl”>
例如:

<!--1、dao对象-->
    <bean id="userDaoImpl" class="spring5.dao.UserDaoImpl"></bean>
    
    <bean id="userService" class="spring5.service.UserService">
<!-- 注入userDao对象-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>

方式二:内部bean,创建对象时在内部指定其属性对应的对象

<!--内部bean-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
	<!--普通类型属性-->
	<property name="ename" value="lucy"></property>
	<property name="gender" value=""></property>
	<!--对象类型属性-->
	<property name="dept.dname" value="技术"></property>
</bean>

三、数组、list、set、map、类型的属性注入,分别对应<array>或<list>、<list>或<array>、<map>、<set>

1、基础类型

<bean id="stu" class="spring5.collectiontype.Stu">
	<!--数组类型属性注入-->
	<property name="courses">
		<array>
			<value>java</value>
			<value>mysql</value>
		</array>
	</property>
	<!--list类型属性注入-->
	<property name="list">
		<list>
			<value>vue</value>
			<value>js</value>
		</list>
	</property>
	<!--map类型属性注入-->
	<property name="maps">
		<map>
			<entry key="one" value="python"></entry>
			<entry key="two" value="java"></entry>
		</map>
	</property>
	<!--set类型属性注入-->
	<property name="sets">
		<set>
			<value>html</value>
			<value>css</value>
		</set>
	</property>
</bean>

2、对象类型
首先声明其属性对应的对象,再将其进行注入

<bean id="course01" class=".spring5.collectiontype.Course">
    <property name="cname" value="java"></property>
</bean>
<bean id="course02" class="spring5.collectiontype.Course">
    <property name="cname" value="vue"></property>
</bean>
<bean id="course03" class="spring5.collectiontype.Course">
    <property name="cname" value="mysql"></property>
</bean>
<bean id="stu" class="spring5.collectiontype.Stu01">
<!--        数组类型属性注入-->
    <property name="courses">
        <array>
            <ref bean="course01"></ref>
            <ref bean="course02"></ref>
            <ref bean="course03"></ref>
        </array>
    </property>
<!--        list类型属性注入-->
    <property name="list">
        <list>
            <ref bean="course02"></ref>
            <ref bean="course03"></ref>
            <ref bean="course01"></ref>
        </list>
    </property>
<!--        map类型属性注入-->
    <property name="maps">
        <map>
            <entry key="1" value-ref="course01"></entry>
            <entry key="1" value-ref="course03"></entry>
            <entry key="1" value-ref="course02"></entry>
        </map>
    </property>
<!--        set类型属性注入-->
    <property name="sets">
        <set>
            <ref bean="course02"></ref>
            <ref bean="course03"></ref>
            <ref bean="course01"></ref>
        </set>
    </property>
</bean>

3、util标签注入
首先引入util名称空间
xmlns:util=“http://www.springframework.org/schema/util”
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

<!--基本数据类型 使用value-->
<util:list id="course">
    <value>vue</value>
    <value>mysql</value>
    <value>java</value>
</util:list>
<!--注入-->
<bean id="stu" class="spring5.collectiontype.Stu">
    <property name="list" ref="course"></property>
</bean>
<bean id="course1" class="spring5.collectiontype.Course">
    <property name="cname" value="java"></property>
</bean>
<bean id="course2" class="spring5.collectiontype.Course">
    <property name="cname" value="mysql"></property>
</bean>
<bean id="course3" class="spring5.collectiontype.Course">
    <property name="cname" value="vue"></property>
</bean>
<!--基本数据类型 使用ref将数组或集合中-->
<util:list id="coures1">
    <ref bean="course1"></ref>
    <ref bean="course2"></ref>
    <ref bean="course3"></ref>
</util:list>
<!--注入-->
<bean id="stu1" class="spring5.collectiontype.Stu01">
    <property name="list" ref="coures1"></property>
</bean>

获取bean

ApplicationContext context = new ClassPathXmlApplicationContext(“对应的xml文件”);
对象 名称 = beanFactory.getBean(“bean id”, 对象.class);

BeanFactory beanFactory = new ClassPathXmlApplicationContext(“对应的xml文件”);
对象 名称= beanFactory.getBean(“bean id”, 对象.class);
例如

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context .getBean("user", User.class);

BeanFactory beanFactory = new ClassPathXmlApplicationContext("bean1.xml");
User user = beanFactory.getBean("user", User.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值