# Global logging configuration log4j.rootLogger=INFO, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<?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"> ***********此处是手写代码************ </beans>
package cn.aynu.pojo; public class Person { private int id; private String name; private String phone; private int age; public Person() { super(); } public Person(int id, String name, String phone, int age) { super(); this.id = id; this.name = name; this.phone = phone; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", phone=" + phone + ", 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--id值(p1)不可重复,如果重复会报错,编译不通过 --> <bean id="p1" class="cn.aynu.pojo.Person"> <property name="id" value="1001"/> <property name="name" value="琥珀"/> <property name="phone" value="18337280000"/> <property name="age" value="18"/> </bean> </beans>
public class PersonTest { @Test public void Test01() { //获取spring容器对象(classpath:可略) ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application.xml"); //根据id获取bean对象 Person person = (Person) applicationContext.getBean("p1"); System.out.println(person); } }
02-Spring的第一个IOC示例程序(通过id获取对象)
原文:https://www.cnblogs.com/zly123/p/11637470.html