spring框架学习(7)
拓展方式注入:
我们可以使用p注入和c注入
第一步:引入命令空间,注意c引入,实体类必须有无参构造和有参构造
实体类:
package com.yms.entity;
/**
* @Author 杨明书
* @PackageName: com.yms.entity
* @ClassName: User
* @Description:
* @Date: 2021/12/29 15:46
*/
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
配置文件:
<?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"
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">
<!-- p命令空间的注入的使用-->
<bean id="user" class="com.yms.entity.User" p:name="迁扩建" p:age="12">
</bean>
<!-- c命令空间的注入的使用-->
<bean id="user2" class="com.yms.entity.User" c:age="18" c:name="小票">
</bean>
</beans>