main 主程序:
package sPRing;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathxmlApplicationContext;public class Main { public static void main(String[] args) { // TODO Auto-generated method stub// // // HelloWorld helloWorld=new HelloWorld();// helloWorld.setName("wzc");// //1.创建Spring 的IOC 容器对象 ApplicationContext ctx=new ClassPathXmlApplicationContext("appContext.xml"); //2.从IOC 容器中获取bean实例 // HelloWorld helloWorld=(HelloWorld) ctx.getBean("helloWorld"); //3. 使用 bean // helloWorld.hello(); }}helloWord代码:package spring;public class HelloWorld { private String name; public HelloWorld() { System.out.println("Hello wzc"); } public void setName(String name) { System.out.println("setname"+name); this.name = name; } public void hello() { System.out.println("hello"+name); }}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.xsd"> <bean id="helloWorld" class="spring.HelloWorld"> <property name="name" value="Spring"></property> </bean></beans>
新闻热点
疑难解答