下载工具
(1)在eclipse上增加STS插件(spring tool suite)
(2)下载安装STS工具(可在https://spring.io/tools/sts/下载)
(使用idea工具也完全ok)
搭建spring环境
使用spring所需至少使用6的jar包
开发AOP特性时需要的JAR
spring-aop.jar
处理Bean的jar
spring-beans.jar
处理spring上下文的jar
spring-context.jar
spring核心jar
spring-core.jar
spring表达式
spring-expression.jar
三方提供的日志jar
commons-logging.jar
编写配置文件
(1)在项目中新建文件bean configuration 命名为applicationContext.xml
(2)在applicationContext.xml进行编写
例:
编写一个student类(需要有set方法)
public class Student {
private int sno;
private String sname;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
在配置文件中编写
<bean id="student" class="com.Spring.Student">
<property name="sno" value="1"></property>
<property name="sname" value="zs"></property>
</bean>
测试
在main方法中编写
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student =(Student)context.getBean("student");
然后使用student进行测试