目录
idea建maven项目
项目结构
pom.xml
jar包:四个spring包,俩个日志包。spring-core,beans,context,expression,log4j,common-logging
<properties>
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
ICustomerService.java
public interface ICustomerService {
public void save();
}
CustomerServiceImpl.java
public class CustomerServiceImpl implements ICustomerService {
// private ICustomerDao customerDao = null;
// 测试延迟加载,以及创建方式(加参数报错)
public CustomerServiceImpl() {
System.out.println("bean创建");
}
public void save() {
System.out.println("业务层");
// customerDao.save();
}
}
InstanceFactory.java
public class InstanceFactory {
public ICustomerService getCustomerService() {
return new CustomerServiceImpl();
}
}
StaticFactory.java
public class StaticFactory {
public static ICustomerService getCustomerService() {
return new CustomerServiceImpl();
}
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--下载spring-framework-4.3.3.RELEASE-dist.zip ,spring-framework-4.3.3.RELEASE-dist/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html里-->
<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">
<!-- bean definitions here,对象创建交由spring -->
<!--class.forName().newInstance-->
<bean id="customerService" class="com.luobo.service.impl.CustomerServiceImpl"></bean>
<bean id="customerDao" class="com.luobo.dao.impl.CustomerDaoImpl"></bean>
<!--配置使用静态工厂创建bean对象-->
<bean id = "staticCustomerService" class="com.luobo.factory.StaticFactory" factory-method="getCustomerService"></bean>
<!--配置使用实例工厂创建bean对象-->
<bean id="instanceFactory" class="com.luobo.factory.InstanceFactory"></bean>
<bean id = "instanceCustomerService" factory-bean="instanceFactory" factory-method="getCustomerService"></bean>
</beans>
CustomerTest.java
public class CustomerTest {
/**
* bean创建俩种规则:
* BeanFactory:提供延迟加载来创建bean对象。什么时候用什么时候创建
* ApplicationContext:提供立即加载来创建bean对象。
*
* Ctrl + H:查看该接口的层级结构
*
* bean创建的三种方式:
* 1.调用无参构造函数创建 若无无参构造,则创建失败报异常
* 2.使用静态工厂中的方法创建对象
* 3.使用实例工厂创建对象
*/
@Test
public void test() {
//1.获取容器
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Resource rs = new ClassPathResource("bean.xml");
BeanFactory ac = new XmlBeanFactory(rs);
//2.根据bean的id获取对象
// ICustomerService cs = (ICustomerService) ac.getBean("customerService");
// ICustomerService cs = (ICustomerService) ac.getBean("staticCustomerService");
ICustomerService cs = (ICustomerService) ac.getBean("instanceCustomerService");
ICustomerDao cd = (ICustomerDao) ac.getBean("customerDao");
System.out.println(cs);
System.out.println(cd);
}
}