1.0属性注入
新建一个people类
package com.java.test3; /** * @author nidegui * @create 2019-06-22 14:45 */ public class People { private Integer id; private String name; private String age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "People{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", age=‘" + age + ‘\‘‘ + ‘}‘; } }
装配在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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloWorld" class="com.java.test.Helloworld"></bean> <bean id="people" class="com.java.test3.People"></bean> <!--属性注入--> <bean id="people2" class="com.java.test3.People"> <property name="name" value="nidegui"></property> <property name="id" value="1"></property> <property name="age" value="12"></property> </bean> </beans>
测试:
package com.java.test3; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author nidegui * @create 2019-06-22 14:47 */ public class Test { public static void main(String[] args) { /*属性注入*/ ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); People people =(People) ac.getBean("people2"); System.out.println(people); } }
原文:https://www.cnblogs.com/nidegui/p/11068898.html