1.搭建Spring环境
下载jar
https://maven.springframework.org/release/org/springframework/spring/
spring-framework-4.3.9.RELEASE-dist.zip
开发spring至少需要使用的jar(5个+1个):
spring-aop.jar //开发AOP特性时需要的JAR
spring-beans.jar //处理Bean的jar <bean>
spring-context.jar //处理spring上下文的jar <context>
spring-core.jar //spring核心jar
spring-expression.jar //spring表达式
第三方提供的日志jar
commons-logging.jar //日志
2.编写配置文件工具(如果使用eclipse)
方法一:
为了编写时有一些提示、自动生成一些配置信息,可以给eclipse增加支持spring的插件:
(STS)spring tool suite(https://spring.io/tools/sts/all)
方法二:
直接下载sts工具(相当于集成了spring的eclipse)
新建: bean configuration…—applicationContext.xml //Spring 配置文件
[New–>XML Configuration file–>Spring Config]
3.SpringIOC容器:
创建对象、给对象的属性赋值
前提是:该bean类必须存在无参构造。
开发Spring程序:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//直接从springIOC容器中获取一个id为student的对象
Student student = (Student)context.getBean("student");
可以发现,SpringIOC容器:帮我们new了对象,并且给对象赋了值。