创建简单的spring

1.新建maven工程
2.加入依赖spring,添加插件,据说这个插件能提高速度,但是实际没有感觉


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>

</dependencies>


<build>中加入插件 解决maven项目中 无法打包生成空文件夹的问题
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>

3.创建类(接口和实现类,也可没有接口)和没有框架一样
idea创建比较简单,首先创建接口,在接口中写一个方法,实现类直接alt+enter选择implement直接回车,输入相应的内容直接创建。

 

 

 创建实体类

创建接口

 

 

 

package link;

public interface Linkme {

public void select();
}
创建接口实现类
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<bean id="student" class="student.Student">
<property name="name" value="zs"></property>
<property name="sex" value="boy"></property>
<property name="age" value="20"></property>
</bean>
</beans>

 

 创建resources的spring配置文件applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<bean id="student" class="student.Student">
<property name="name" value="zs"></property>
<property name="sex" value="boy"></property>
<property name="age" value="20"></property>
</bean>
</beans>

 

 

 

 

 

 


4.创建spring的配置文件(XML),配置文件必须是XML格式,一般放在根目录。spring使用BEAN来保存数据。用getbean来获取数据

 

 

 

 

 

 



5.新建测试类,验证spring
创建表示spring的对象applicationContext从spring容器中获取对象,
使用getBean()

 

posted @ 2020-10-28 15:54  LoveYouLoveMe  阅读(137)  评论(0编辑  收藏  举报