Spring基于XML配置的装配实验(构造、seter方法注入)及bean 中 constructor-arg、property标签使用知识整理

1.Spring基于XML配置的装配实验

(1)wrh_student.java类包含了Map、Set、List、数组、集合等这些容器,主要用于Spring基于XML配置的装配演示配置

import java.util.List;
import java.util.Map;
import java.util.Set;

public class wrh_Student {
	private String username; 
	private List<String> hobbies;//list集合
	private Map<String, String> address;//map集合
	private Set<String> aliases;//set集合
	private String[] array;//数组
	/**
	 * 1.使用构造注入
	 * 1.1提供带有参数的有参构造方法。
	 */
	public wrh_Student(String username,List<String> hobbies,Map<String, String> address,Set<String> aliases,String[] array) {
		super();
		this.username=username;
		this.hobbies=hobbies;
		this.address=address;
		this.aliases=aliases;
		this.array=array;
	}
	/**
	 * 2.使用设置注入
	 * 2.1提供默认空参构造方法
	 * 2.2为所有属性提供setter方法
	 */
	public wrh_Student() {
		super(); 
	}
	public void setUsername(String username) {
		this.username=username;
		}
	public void setHobbies(List<String> hobbies) {
		this.hobbies=hobbies;
	}
	public void setAddress(Map<String,String> address) {
		this.address=address;
	}
	public void setAliases(Set <String> aliases) {
		this.aliases=aliases;
	}
	public void setArray(String[] array) {
		this.array=array;
	}
	public String toString() {
		return "Student[username="+username+",hobbies="+hobbies+",address="+address+",aliases="+aliases+",array="+Array.toString(array)+"]";
	}
}

(2)Spring配置文件beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
        
        <!-- 使用构造注入方式装配Student实例 -->
    <bean id="student1" class="wrh_homework.wrh_Student">
    	<constructor-arg index="0" value="xiaoming"/>
    	<!-- 注入list集合 -->
    	<constructor-arg index="1">
    		<list>
    			<value>"唱歌"</value>
    			<value>"跳舞"</value>
    			<value>"远足"</value>
    		</list>
    	</constructor-arg>
    	<!-- 注入map集合 -->
        <constructor-arg index="2">
        	<map>
        		<entry key="chouzhou" value="潮州"/>
            	<entry key="guangdong" value="广东"/>
        	</map>   
        </constructor-arg>
        <!-- 注入set集合 -->
        <constructor-arg index="3">
         	<set>
         		<value>xiaoming1</value>
         		<value>xiaoming2</value>
         	</set>
        </constructor-arg>
        <!-- 注入array数组 -->
        <constructor-arg index="4">
         	<array>
         		<value>数学</value>
         		<value>英语</value>
         	</array>
        </constructor-arg>
    </bean>
 <!-- 使用设置注入方式装配Student实例 -->
    <bean id="student2" class="wrh_homework.wrh_Student">
       <property name="username" value="xiaohong"></property>
       <!-- 注入list集合 -->
       <property name="hobbies">
       		<list>
       			<value>"游泳"</value>
       			<value>"学习"</value>
       			<value>"开车"</value>
       		</list>
       </property>
       <!-- 注入map集合 -->
       <property name="address">
      	 	<map>
       			<entry key="chouzhou" value="潮州"/>
            	<entry key="guangdong" value="广东"/>
       	 	</map>
       </property>
       <!-- 注入set集合 -->
       <property name="aliases">
       		<set>
       			<value>xiaohong1,xiaohong2</value>
       		</set>
       </property>
       <!-- 注入array数组 -->
       <property name="array">
       		<array>
       			<value>"语文"</value>
       			<value>"英语"</value>
       			<value>"历史"</value>
       		</array>
       </property>
    </bean>


</beans>

(3)编写测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class wrh_XmlBeanAssembleTest {
	public static void main (String [] args) {
		//定义配置文件路径
		String xmlPath="wrh_homework/wrh_beans.xml";
		//加载配置文件
		ApplicationContext applicatContext=new ClassPathXmlApplicationContext(xmlPath);
		//构造方式输出结果
		System.out.println(applicatContext.getBean("student1"));
		//设置方式输出结果
		System.out.println(applicatContext.getBean("student2"));
	}
}

输出结果:
在这里插入图片描述

实验中出现的问题:数组的注入打印出错,seter注入方法和构造注入方法同时打印array的地址
在这里插入图片描述
因为是刚入门Spring当看到这个问题时还以为是自己的标签属性使用错误了,接连修改了几次String[ ]array数组的配置文件。例如:将数组的属性标签改为,但是修改后调试结果仍然是错误(如下图)
在这里插入图片描述
经过一段盲改之后问题终究没有解决,冷静下来分析了问题
1.创建的两种输入方法都有输出且只有数组输出有误
2.数组的输出为数组地址
判断:数组的输出的数据类型配置错误
然后去检查了student类和xml配置文件,发现student类中的array的数据类型没有指明
修改前:
在这里插入图片描述
修改后:
在这里插入图片描述
结果正确显示:
在这里插入图片描述

2.Spring bean 中 constructor-arg、property标签及其子标签使用分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用`@Autowired`注解自动装配时,需要确保以下三个条件满足: 1. Spring容器必须能够扫描到该bean,即该bean必须已经被实例化并注册到Spring容器。 2. 该bean必须具有默认的构造函数。 3. 该bean必须被标注为`@Component`或其它相关注解。 如果使用XML配置生成的bean,可以使用`<context:component-scan>`元素扫描包路径,将标注为`@Component`或其他相关注解的类自动纳入Spring容器,如下所示: ```xml <context:component-scan base-package="com.example"/> ``` 如果使用注解生成的bean,需要在配置类上添加`@ComponentScan`注解,指定需要扫描的包路径,如下所示: ```java @Configuration @ComponentScan(basePackages = "com.example") public class AppConfig { // ... } ``` 在使用`@Autowired`注解自动装配时,可以在成员变量、构造函数或方法参数上使用该注解,如下所示: ```java @Service public class MyService { @Autowired private MyDao myDao; public MyService(MyDao myDao) { this.myDao = myDao; } @Autowired public void setMyDao(MyDao myDao) { this.myDao = myDao; } } ``` 需要注意的是,`@Autowired`注解默认情况下是按照类型进行自动装配的,如果存在多个同类型的bean,需要使用`@Qualifier`注解指定具体的bean名称,如下所示: ```java @Service public class MyService { @Autowired @Qualifier("myDaoImpl") private MyDao myDao; } ``` 以上就是使用`@Autowired`注解自动装配XML和注解生成的bean的方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值