这段代码还是我刚学习spring时候得。
这些书上的概念很多,但是没太记住。
程序太累,还是忘记那些概念吧,也没太大意义,把大脑那有限的内存记住生活的美好吧。
上代码:
bean : 一个类 + 一个配置XML
注入: 太难理解了,而且我觉得英文翻译过来怎么会叫注入呢。
反正就是去读取配置文件里面的值。
为什么呢,因为有些值可能需要修改,而程序编译成.CLASS,要修改代码重新编译,所以拉出来。
package spring.helloworld;
public class HelloWorld
{
private String msg;
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hw" class="spring.helloworld.HelloWorld">
<property name="msg">
<value>hello world</value>
</property>
</bean>
</beans>
package spring.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloWorldTest01
{
public static void main(String[] args)
{
/*
* J:\COMPAQ_E\PRJ_J2EE\PrjFlexJava\spring-01.xml
*
* 直接访问工程下的spring-01.xml
*/
ApplicationContext ac = new FileSystemXmlApplicationContext("spring-01.xml");
HelloWorld hw = (HelloWorld)ac.getBean("hw");
System.out.println(hw.getMsg());
}
}