现在说最后一种装配Bean的方式, 利用XML装配Bean, 当然只是装配,不说多的,在后续的介绍中可能会随着知道东西多了会说的多一点, 但是现在我自己也只是在学习中, 可能也就只是把自己做的demo分享下来吧,有不到之还希望多指教,下面直接来干的
image.png
package xmlComponent;
public interface HelloWorldApi {
public void sayHello();
}
package xmlComponent;
public class PersonHelloWorld implements HelloWorldApi {
@Override
public void sayHello() {
System.out.println("Hello World!");
}
}
新建一个bean.xml
<?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-3.0.xsd">
<bean id="PersonHelloWorld" class="xmlComponent.PersonHelloWorld"></bean>
</beans>
package xmlComponent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldTest {
private static ApplicationContext context;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("bean.xml"); //此处是xml的路劲
PersonHelloWorld objHelloWorld = (PersonHelloWorld) context.getBean("PersonHelloWorld");
objHelloWorld.sayHello();
}
}
运行之后得到结果
image.png
网友评论