Spring容器管理java中的对象。
HelloSpring入门
1、创建HelloSpring.java
public class HelloSpring {
//创建成员变量
private static String message="HelloSpring";
//创建成员方法
public void say(){
System.out.println(message);
}
}2、创建Spring的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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!--在Spring配置文件中通过bean来创建HelloSpring对象--> <bean id="helloSpring" class="com.imooc.spring.HelloSpring"></bean> </beans>
3、测试
public class TestHelloSpring {
@Test
public void testHelloSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring/spring-config.xml");
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
helloSpring.say();
}
}點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
