标签:XML system info nfa bubuko utf-8 工厂 getbean 1.0
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>
public class Coder {
private String name;
private int age;
public void print(){
System.out.println("Hello World");
}
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;
}
}
<?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="coder" class="dulk.learn.spring.Coder"></bean>
</beans>
public class Test {
public static void main(String[] args) {
- //BeanFactory
ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
Resource resource = rpt.getResource("/applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(resource);
Coder coder = (Coder) bf.getBean("coder");
coder.print();
}
}
public class Test {
public static void main(String[] args) {
- //ApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
Coder coder = (Coder) ac.getBean("coder");
coder.print();
}
}
标签:XML system info nfa bubuko utf-8 工厂 getbean 1.0
原文地址:https://www.cnblogs.com/deng-cc/p/8847269.html