首先配置spring的pom.xml文件
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springtest</groupId> <artifactId>springtest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springtest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.7.RELEASE</spring.version> </properties> <dependencies> <!-- 添加Spring支持 --> <!-- 核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- pring IOC的基础实现,包含访问配置文件、创建和管理bean等 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>springtest</finalName> </build> </project>
注入类型是String的写法
1. 新建bean类
package com.test.bean; public class User { private String name; private String sex; public String getName() { return name; } public String getSex() { return sex; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void sayHello() { System.out.println("我的名字:"+name+",性别:"+sex); } }
2. 配置bean.xml,见id="user"的bean
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- 简单属性配置 --> <bean id="user" class="com.test.bean.User"> <property name="name" value="乔佳飞"></property> <property name="sex" value="男"></property> </bean> <!-- 对象属性配置 --> <bean id="userservice" class="com.test.bean.UserService"> <property name="user" ref="user"></property> </bean> <!-- 复杂类型配置 --> <bean id="userfz" class="com.test.bean.UserFz"> <!-- 数组配置 --> <property name="array"> <list> <value>arrayValue1</value> <value>arrayValue2</value> </list> </property> <!-- list配置 --> <property name="set"> <set> <value>setValue1</value> <value>setValue2</value> </set> </property> <!-- list配置 --> <property name="list"> <list> <value>listValue1</value> <value>listValue2</value> </list> </property> <!-- MAP配置 --> <property name="map"> <map> <entry key="mapkey1" value="mapvalue1"></entry> <entry key="mapkey2" value="mapvalue2"></entry> </map> </property> <!-- MAP配置 --> <property name="proper"> <props> <prop key="name">qiaojiafei</prop> <prop key="sex">nan</prop> </props> </property> </bean> </beans>
3.测试类
@Test public void bean1Test() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml"); User user = (User) ac.getBean("user"); user.sayHello(); }
输出:
我的名字:乔佳飞,性别:男
注入类型是引用对象的写法
1.新建类UserService
package com.test.bean; public class UserService { private User user; public void setUser(User user) { this.user = user; } public void sayHello() { System.out.println("UserService-----------"); System.out.println("UserService:"+user.getName()+user.getSex()); } }
2. 配置bean.xml,见上述xml的id="userservice"的bean
3.测试类
@Test public void bean2Test() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml"); UserService us = (UserService) ac.getBean("userservice"); us.sayHello(); }
输出:
UserService-----------
UserService:乔佳飞男
注入类型是复杂类型的写法
1.新建类UserFz
package com.test.bean; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class UserFz { private Set set; private List list; private Map<String,String> map; private String array[]; private Properties proper; public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public Properties getProper() { return proper; } public void setProper(Properties proper) { this.proper = proper; } public void sayHello() { System.out.println("UserFz----array:"+array+",set:"+set+",list:"+list+",map:"+map +",proper:"+proper); } }
2.配置bean.xml,见上述xml的id="userfz"的bean
3.测试类
@Test public void bean3Test() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml"); UserFz usf = (UserFz) ac.getBean("userfz"); usf.sayHello(); }
输出:
UserFz----array:[Ljava.lang.String;@670b40af,set:[setValue1, setValue2],list:[listValue1, listValue2],map:{mapkey1=mapvalue1, mapkey2=mapvalue2},proper:{name=qiaojiafei, sex=nan}