### [](
)3.spring.xml配置
标签内 name表示传入参数的名字
标签内 ref表示中的id
<?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.xsd">
<bean id="accountDao" class="com.lcySpring.dao.AccountDao"></bean>
<bean id="accountService" class="com.lcySpring.service.AccountService">
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
</bean>
[](
)三.p标签和c标签
--------------------------------------------------------------------
### [](
)1.简化< property >使用p 标签
这里我们创建一个类存在set方法
public class UserService {
private String username;
private int age;
public void show(){
System.out.println("姓名:"+username+" 年龄:"+age);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置spring.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.xsd">
<property name="username" value="mlrcj"></property>
<property name="age" value="21"></property>
通过设置value 属性成功传入参数
![在这里插入图片描述](https://img-blog.csdnimg.cn/0fecef24093b4f74ae6a11065136e4f3.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JpbmZsY3k=,size_16,color_FFFFFF,t_70)
但是这样写太麻烦了我们可以使用p标签
在spring.xml中配置命名空间加入p插件功能(p:是写在< bean >里面的)
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id=“userService” class=“com.lcySpring.service.UserService”
p:username="lmrcj"
p:age="21"
### [](
)2.简化< constructor-arg >使用c 标签
和p标签的功能差不多,但是它还可以提供索引赋值
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id=“accountService” class=“com.lcySpring.service.AccountService”
c:aname="aaaaaaaaaaa"
c:money="100000000000"
<bean id="accountService02" class="com.lcySpring.service.AccountService"
c:_0="bbbbbbbbbbbbbb"
c:_1="10000000000000"
>
关于Unable to locate Spring NamespaceHandler for XML schema namespace \[http://www.springframework.org/schema/c\]报错
可以参考这篇文章
[文章链接](
)
[](
)四.集合注入
-----------------------------------------------------------------
### [](
)1.集合list注入
构建一个含集合的java类
public class CollectionService {
private List list;
public void setList(List list) {
this.list = list;
}
public void show(){
System.out.println(list);
}
}
配置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.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
<property name="list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</list>
</property>
</bean>
编写测试类,检测成功注入
![在这里插入图片描述](https://img-blog.csdnimg.cn/9d99023aa2e24021a839c551709169b7.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JpbmZsY3k=,size_16,color_FFFFFF,t_70)
### [](
)2.集合set注入
直接将xml中list配置改为set就好了
<?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.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
<property name="set">
<set>
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</set>
</property>
</bean>
### [](
)3.map类型属性注入
在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.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
<property name="map">
<map>
<entry>
<key><value>lmr</value></key>
<value>菜鸡</value>
</entry>
<entry>
<key><value>lcy</value></key>
<value>超厉害</value>
</entry>
</map>
</property>
</bean>
编写测试类查看
![在这里插入图片描述](https://img-blog.csdnimg.cn/1b79fb666dbf46fcaa12e44967c48097.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JpbmZsY3k=,size_16,color_FFFFFF,t_70)
### [](
)4.properties属性注入
在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.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
本次面试答案,以及收集到的大厂必问面试题分享:
ml 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.xsd">
<bean id="collectionService" class="com.lcySpring.service.CollectionService">
本次面试答案,以及收集到的大厂必问面试题分享:
[外链图片转存中…(img-Q2QZdl3C-1630666516612)]