1、导入依赖包
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.baichi</groupId>
<artifactId>spring_day1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring_day1 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring_day1</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、根据源码包在resources目录下创建相同的包并创建spring.xml
setdi/spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 必须在管理的类里边提供setter方法才可以设置值 -->
<bean class="setdi.StudentDAOImpl" id="studentDAO">
<property name="name" value="小陈"/>
<property name="list">
<list>
<value>1</value>
<value>12</value>
<value>123</value>
</list>
</property>
<property name="map">
<map>
<entry key="xxx" value="123"/>
<entry key="www" value="456"/>
</map>
</property>
<property name="properties">
<props>
<prop key="url">http://xxx.xxx.xxx</prop>
<prop key="name">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
StudentDAO
package setdi;
public interface StudentDAO {
void show();
}
StudentDAOImpl
package setdi;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class StudentDAOImpl implements StudentDAO {
private String name;
private List<Integer> list;
private Map<String, String> map;
private Properties properties;
public void setName(String name) {
this.name = name;
}
public void setList(List<Integer> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public void show() {
System.out.println("name = " + name);
for (Integer i:
this.list){
System.out.println("i = " + i);
}
map.forEach((key,value)-> System.out.println("key : " + key+" value: "+value));
System.out.println("==========遍历properties=============");
properties.forEach((key,value)-> System.out.println("key:" + key+" value:"+value));
}
}
TestStudentDAO
package setdi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudentDAO {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("setdi/spring.xml");
StudentDAO studentDAO = (StudentDAO) context.getBean("studentDAO");
studentDAO.show();
}
}