現(xiàn)在說最后一種裝配Bean的方式, 利用XML裝配Bean, 當(dāng)然只是裝配,不說多的,在后續(xù)的介紹中可能會隨著知道東西多了會說的多一點(diǎn), 但是現(xiàn)在我自己也只是在學(xué)習(xí)中, 可能也就只是把自己做的demo分享下來吧,有不到之還希望多指教,下面直接來干的
package xmlComponent;
public interface HelloWorldApi {
public void sayHello();
}
package xmlComponent;
public class PersonHelloWorld implements HelloWorldApi {
@Override
public void sayHello() {
System.out.println("Hello World!");
}
}
新建一個(gè)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();
}
}
運(yùn)行之后得到結(jié)果