这篇随笔搭建的工程是普通的Spring工程,用于学习Spring框架,如果要搭建SpringMVC工程
第一步:在IDEA点击new -> project
左侧选择Maven,直接点击Next。第一次使用IDEA的朋友,顶部还要选择Project SDK路径,就是Java的安装路径。
这里随便填一下之后点击Next
选择项目存放路径,或者保持默认,点击Finish,来到工程页面之后,在项目文件夹上右键并选择Add Framework Support
在这个页面找一下Spring,打钩。点击OK
完成后,会下载Spring的jar包,并存放在项目的lib目录下。
下载完成之后创建一个简单的项目。
四个Java文件一个appContext.xml我贴在这里
public class Student {
private String name;
private Homework homework;
public Student(){}
public Student(String name, Homework homework) {
this.name = name;
this.homework = homework;
}
public void doHomeWork(){
System.out.println(homework.getContent());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Homework getHomework() {
return homework;
}
public void setHomework(Homework homework) {
this.homework = homework;
}
}
View Code
public class Homework {
private String content;
public Homework(){}
public Homework(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
View Code
import java.util.Date;
public class CheckNowTime {
public void beforDoHomework(){
System.out.println(new Date(System.currentTimeMillis()));
}
}
View Code
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
Student student = context.getBean(Student.class);
System.out.println(student.getName()+"准备做作业了");
student.doHomeWork();
context.close();
}
}
View Code
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 让Spring管理 Student bean -->
<bean id="student" class="demo1.Student">
<property name="name" value="小明"></property>
<property name="homework" ref="homework"></property>
</bean>
<!-- 让Spring管理Homework bean-->
<bean id="homework" class="demo1.Homework">
<property name="content" value="how to calc 3+2 ?"></property>
</bean>
<!-- 切面定义-->
<bean id="checktime" class="demo1.CheckNowTime"></bean>
<aop:config>
<aop:aspect ref="checktime">
<aop:pointcut id="dohomework" expression="execution(* *.doHomeWork(..))"/>
<aop:before pointcut-ref="dohomework" method="beforDoHomework"></aop:before>
</aop:aspect>
</aop:config>
</beans>
View Code
因为例子中使用Spring AOP,所以还需要引入一个jar包。我的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</project>
View Code
然后你就可以运行自己的Spring项目啦,main方法在App类里面。