如何写一个我们的demo?
首先,新建一个模块module,使用maven构建。
pom文件如下:
<?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.zzx.learn</groupId>
<artifactId>zzx.spring.test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
</project>
整个模块的目录结构如下:
代码块:
package com.zzx.spring.beans;
import lombok.Data;
@Data
public class Student {
private String name;
private int age;
}
package com.zzx.spring;
public class APPDemo {
public static void main(String[] args) {
}
}
package com.zzx.spring;
import com.zzx.spring.beans.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class APPDemoTest {
@Test
public void testStudent(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application.xml");
Student student = context.getBean("student",Student.class);
System.out.println(student.getName());
}
}
<?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="student" class="com.zzx.spring.beans.Student">
<property name="name" value="zzx"/>
<property name="age" value="1"/>
</bean>
</beans>
导入依赖:(只要导入核心的四个就可以,beans,core,context,Expression Language)不过,为了后续方便,我把所有的模块都依赖了,但是aop的有个问题,提示找不到target包,最后发现是aop模块源码下载的少了target包,重新下载导入就解决了。然后是aspects这个模块中提示找不到类AnnotationBeanConfigurerAspect等,但是源码中确实有这些类。为了不抛错,我的测试模块不依赖这个模块。。。以后有时间了,再看。估计是需要重新编译这个模块。
效果: