首先导入依赖:
<?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.liaoxiang</groupId>
<artifactId>spring_1-3_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
在resource下创建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">
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.liaoxiang.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.liaoxiang.dao.impl.AccountDaoImpl"></bean>
</beans>
获取对象:
package com.liaoxiang.controller;
import com.liaoxiang.dao.IAccountDao;
import com.liaoxiang.service.IAccountService;
import com.liaoxiang.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
/**
* 模拟表现层,调用业务层
*/
public class AccountController {
/**
* 获取spring的Ioc核心容器(Map),并根据id 获取对象
*
* ApplicationContext的三个常用实现类:
* ClassPathXmlApplicationContext: 用于加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了(更常用)
* FileSystemXmlApplicationContext: 用于以加载磁盘任意路径下的配置文件(必须有访问权限)
* AnnotationConfigApplicationContext: 用于读取注解创建容器
*
* 核心容器的两个接口引发出的问题:
* 1、ApplicationContext: 单例对象适用 采用此接口
* 它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。只要一读取完配置文件马上就创建配置文件中配置的对象。
* 2、BeanFactory: 多例对象使用
* 它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。读取配置文件不立即创建对象
* 也就是说,什么时候用到对象了,什么时候才真正的创建对象。
*/
public static void main(String[] args) {
//1、获取核心容器,读取配置文件的时候就会创建好对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id 获取Bean对象
IAccountService accountService = (IAccountService)context.getBean("accountService");// 使用强转
IAccountDao accountDao = context.getBean("accountDao",IAccountDao.class);// 使用字节码强转
System.out.println(accountService);
System.out.println(accountDao);
// 使用BeanFactory的实现类XmlBeanFactory创建对象,当加载配置文件时,不会创建对象
ClassPathResource resource = new ClassPathResource("bean.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
IAccountService accountService1 = (IAccountService) factory.getBean("accountService");
// 此时才会反射创建对象
System.out.println(accountService1);
}
}
上面只是在Controller中获取了accountService和accountDao对象,并没有完成业务逻辑,在accountService中需要用到accountDao
public class AccountServiceImpl implements IAccountService {
// 直接使用new 获取实例 耦合性高
//private IAccountDao accountDao = new AccountDaoImpl();
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("调用了AccountServiceImpl的空参构造器创建对象!");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
修改Controller,并运行main方法
public class AccountController {
public static void main(String[] args) {
//1、获取核心容器,读取配置文件的时候就会创建好对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id 获取Bean对象
IAccountService accountService = (IAccountService)context.getBean("accountService");// 使用强转
accountService.saveAccount();
}
}
报空指针异常:
调用了AccountServiceImpl的空参构造器创建对象!
调用了AccountDaoImpl的空参构造器创建了对象!
Exception in thread "main" java.lang.NullPointerException
at com.liaoxiang.service.impl.AccountServiceImpl.saveAccount(AccountServiceImpl.java:24)
at com.liaoxiang.controller.AccountController.main(AccountController.java:42)
Process finished with exit code 1
原因:虽然在AccountServiceImpl 中定义了accountDao,也在配置文件中配置了accountDao的实现类AccountDaoImpl,将accountDao的实现类对象加入到了IOC容器,但是并没有在AccountServiceImpl 中进行初始化。此时AccountDaoImpl中的accountDao为null,解决此问题的方法就是在创建AccountServiceImpl的实例对象的时候,对其进行初始化,可以使用依赖注入的方式,修改bean.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">
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.liaoxiang.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.liaoxiang.dao.impl.AccountDaoImpl"></bean>
</beans>
并在AccountDaoImpl中提供accountDao的set方法:
public class AccountServiceImpl implements IAccountService {
// 直接使用new 获取实例 耦合性高
//private IAccountDao accountDao = new AccountDaoImpl();
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public AccountServiceImpl(){
System.out.println("调用了AccountServiceImpl的空参构造器创建对象!");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
再次运行main方法:
六月 20, 2019 11:36:02 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ccabbaa: startup date [Thu Jun 20 11:36:02 CST 2019]; root of context hierarchy
六月 20, 2019 11:36:02 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
调用了AccountServiceImpl的空参构造器创建对象!
调用了AccountDaoImpl的空参构造器创建了对象!
保存成功!
Process finished with exit code 0