web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>IocXmlExplain</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.et</groupId>
<artifactId>IocXmlExplain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<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>
<!-- JUnit Java语言的单元测试框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 指定JDK版本和编码的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
package cn.et.entity;
public class Administrator {
private String name;
private Integer age;
private String gender;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Administrator other = (Administrator) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
@Override
public String toString() {
return "Administrator [name=" + name + ", age=" + age + ", gender=" + gender + ", password=" + password + "]";
}
}
package cn.et.entity;
public class Author {
private String name;
private Integer age;
private String gender;
private String blog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBlog() {
return blog;
}
public void setBlog(String blog) {
this.blog = blog;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((blog == null) ? 0 : blog.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Author other = (Author) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (blog == null) {
if (other.blog != null)
return false;
} else if (!blog.equals(other.blog))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Author [name=" + name + ", age=" + age + ", gender=" + gender + ", blog=" + blog + "]";
}
}
package cn.et.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class User {
private String name;
private Integer age;
private String gender;
private List<String> list;
private Map<String,String> map;
private Properties properties;
public User(){}
public User(String name,Integer age,String gender){
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((list == null) ? 0 : list.hashCode());
result = prime * result + ((map == null) ? 0 : map.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((properties == null) ? 0 : properties.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (list == null) {
if (other.list != null)
return false;
} else if (!list.equals(other.list))
return false;
if (map == null) {
if (other.map != null)
return false;
} else if (!map.equals(other.map))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (properties == null) {
if (other.properties != null)
return false;
} else if (!properties.equals(other.properties))
return false;
return true;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", gender=" + gender + ", list=" + list + ", map=" + map
+ ", properties=" + properties + "]";
}
}
package cn.et.utils;
import cn.et.entity.Administrator;
import cn.et.entity.Author;
import cn.et.entity.User;
public class MyBeanFactory {
public static User getUser(){
return new User();
}
public static Administrator getAdministrator() {
return new Administrator();
}
public Author getAuthor(){
return new Author();
}
}
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入schema约束 -->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
"
>
<!--Bean标签属性
id:对象变量名(不能包含特殊符号)
name:对象变量名(可以包含特殊符号)
class:类的全路径
scope:Bean的作用范围
singleton 单例(默认)
、prototype 多例
。。。。。。。。。。。。。。。。。。。。。。。
、request WEB项目中,将项目存放在request域中
、session WEB项目中,将项目存放在session域中
、globalSession WEB项目中,应用在Porlet环境,如果没有Porlet环境那么globalSession相当于session
-->
<!-- Spring XML配置 类实例化 方式一 -->
<bean id="user" class="cn.et.entity.User" p:age="21"><!-- p命名空间设置属性值 -->
<!-- XML配置可以使用类的带参构造设置属性值 name:属性名 value:属性值 ref:其它配置bean标签中的id值(引入对象类型值) -->
<constructor-arg name="name" value="lucheng"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="gender" value="男"></constructor-arg>
<!-- XML配置可使用类的setName、setAge、setGender方法设置值 name:属性名 value:属性值(基本类型) ref:其它配置bean标签中的id值(引入对象类型值)-->
<property name="name" value="lu"/>
<!-- List、Map、Properties集合类型属性值设置 -->
<property name="list">
<list>
<value>JavaSE</value>
<value>JavaSE</value>
</list>
</property>
<property name="map">
<map>
<entry key="1" value="JavaSE"></entry>
<entry key="2" value="Spring"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
<bean id="myBeanFactory" class="cn.et.utils.MyBeanFactory" />
<!-- Spring XML配置 类实例化 public static Administrator getAdministrator() 方式二 -->
<bean id="administrator" class="cn.et.utils.MyBeanFactory" factory-method="getAdministrator"/>
<!-- Spring XML配置 类实例化 public Author getAuthor() 方式三 -->
<bean id="author" factory-bean="myBeanFactory" factory-method="getAuthor"></bean>
</beans>
package cn.et.utils;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import cn.et.entity.Administrator;
import cn.et.entity.Author;
import cn.et.entity.User;
public class Checkout {
@Test
public void testing() {
ApplicationContext context = new GenericXmlApplicationContext("classpath:Spring.xml");
ApplicationContext authContext1 = new ClassPathXmlApplicationContext("Spring.xml");
ApplicationContext authContext2 = new FileSystemXmlApplicationContext("classpath:Spring.xml");
System.out.println(authContext1);
System.out.println(authContext2);
System.out.println(context);
User user = (User)context.getBean("user");
System.out.println(user);
MyBeanFactory myBeanFactory = (MyBeanFactory)context.getBean("myBeanFactory");
System.out.println(myBeanFactory);
Administrator administrator = (Administrator)context.getBean("administrator");
System.out.println(administrator);
Author author = new Author();
System.out.println(author);
}
}