刚刚开始学习Spring,记录一下Spring项目的创建,我使用的是IDEA
1、创建一个项目,加入Spring常用的jar包
2、创建spring的xml配置文件
3、hello小程序
1、创建一个HelloWork.java文件
public class HelloWord {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("Hello: " + name);
}
}
2、编写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="hello" class="it.hu.demo.HelloWord">
<property name="name" value="Spring"></property>
</bean>
</beans>
3、创建一个测试类Main
public class Main {
public static void main(String[] args) {
// 创建Spring的IOC容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
// 从容器中获取Bean
// 方式一:
HelloWord hello = (HelloWord) context.getBean("hello");
// 方式二:
HelloWord hello = context.getBean(HelloWord.Class)
hello.hello();
}
}