本文共 2166 字,大约阅读时间需要 7 分钟。
文件目录
org.springframework spring-webmvc 5.2.0.RELEASE junit junit 4.12 org.aspectj aspectjweaver 1.9.4 src/main/resources **/*.properties **/*.xml false src/main/java **/*.properties **/*.xml false
package com.qin.pojo;public class HelloSpring { private String msg; //使用set方法 public void setMsg(String msg) { this.msg = msg; } public String getMsg() { System.out.println("my msg:"+msg); return msg; }}
package com.qin.pojo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloSpringTest { public static void main(String[] args) { //我们需要使用ClassPathXmlApplicationContext类,来帮我们找到Spring的核心配置文件 ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("application-spring.xml"); //通过getBean方法来获取bean的id 来找到具体的类 helloSpring就是bean的id HelloSpring helloSpring = cpx.getBean("helloSpring", HelloSpring.class); //helloSpring就相当于是被HelloSpring类new 出来的对象,只不过spring帮我们做了 //所以我们可以使用HelloSpring类的getMsg()方法 helloSpring.getMsg(); }}
结果
转载地址:http://qhnki.baihongyu.com/