Spring安装配置(2021)
Sping 安装配置
1、Eclipse下安装配置(默认配置JDK环境)
- jar包下载 spring下载路径:
- 导入基本的jar包 创建一个web 项目,在下载的spring文件中的lib目录下复制jar包导入eclipse,spring-aop-4.1.6.RELEASE.jar,x-beans-x,x-context-x,x-core-x,x-context-support-x(x代表相同的前后缀) (Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 出现上述报错:下载apache commons logging 包 ,导入即可)
- 编写代码
-
1、bean配置文件
<bean id="person(唯一标识符)" class="bean类的全类名(如:java.lang.String)"> <property name="name" value="pp"></property> <property name="age" value="44"></property> </bean>创建的.xml文件记得在/路径下 具体文件头参照:..spring-framework-4.1.6.RELEASEdocsspring-framework-referencehtmleans.html (下载的spring-framework-4.1.6.RELEASE中) - 2、获取IoC容器中的Bean实例
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Object person = context.getBean("person");
Person person2 = context.getBean("person",Person.class);
System.out.println(person); //通过beans.xml 中配置的bean的id
System.out.println(person2); //指定类
2、IDEA-Maven配置(默认已安装IDEA)
- 创建Maven项目 ;
Object user = context.getBean("user");
System.out.println(user);
本人新手,文章有误还请大佬斧正,万分感谢!
