IntelliJ IDEA中创建Spring项目
1.打开IDEA,+create new project;
2.选择Spring,然后next;
3.项目命名。
4.点击finish之后,就自动下载Spring所需jars,等待即可;
5.下载好之后,Spring的jars和配置文件都准备好了。
总体来看,IntelliJ IDEA较eclipse方便,所有需要的jars包不需要自己导入。项目创建好就可以进行代码编写了。
第一个HelloWorld
创建一个bean以及简单的测试
测试HelloWorld
使用spring容器创建对象和属性赋值
测试类中
运行结果
红色内容是日志。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
上述代码指的是创建Spring IOC容器,做的工作就是:调用构造器对spring-config.xml中配置的bean进行初始化,同时调用set()方法对属性进行赋值。
需要AspectJ注解时,在IntelliJ IDEA中需要导入jars包aspectjrt.jar包。
出现以下这个异常是由于缺少aspectjweaver.jar包
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.event.internalEventListenerProcessor': BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException