第一步:jar包。maven的pom文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
大概这么多。下面是基本的代码
实体类
package com.demo.student;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
public class Student {
private String name;
private List<String> list;
private Map<String,Object> map;
private Teacher teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student [name=" + name + ", list=" + list + ", map=" + map
+ ", teacher=" + teacher + "]";
}
}
package com.demo.student;
import org.springframework.stereotype.Repository;
public class Teacher {
public void stu(){
System.out.println("Teacher");
}
}
<?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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"
xmlns:context="http://www.springframework.org/schema/context"
>
<!-- 扫描包 -->
<context:component-scan base-package="com.demo.listaotowired"/>
<bean id="teacher" class="com.demo.student.Teacher"></bean>
<bean id="student" class="com.demo.student.Student">
<property name="name" value ="hello"></property>
<property name="teacher" ref="teacher"></property>
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<!-- map注入-->
<property name="map">
<map>
<entry key="first" value="first"></entry>
</map>
</property>
</bean>
</beans>
Test代码
package springDemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.demo.student.Student;
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class TestAutoired {
@Autowired
private Student student;
@Test
public void test(){
System.out.println(student);
}
}
输入内容
Student [name=hello, list=[1, 2], map={first=first}, teacher=com.demo.student.Teacher@1d387200]