配置文件需要导入的配置
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
创建实体类
public class user {
private String name;
private int age;
public user(String name, int age) {
this.name = name;
this.age = age;
}
public user() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "user{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
配置文件
<!--p命名空间,用于普通的一般属性注入-->
<bean id="user" class="com.Google.pojo.user" p:age="19" p:name="Spring"/>
<!--c命名空间,用于构造函数注入-->
<bean id="user1" class="com.Google.pojo.user" c:age="20" c:name="Spring1"/>
实现
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
user user = context.getBean("user", user.class);
System.out.println(user);
}