1.首先导入依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
2.bean.xml文件编写:
获取文件头:Core Technologies
访问官网路径进行复制,这里已经帮你整理好了
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
文件全部内容:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 将对象的创建交给Spring-->
<bean id="AccountDao" class="com.demo.dao.impl.AccountDaoImpl"></bean>
<bean id="IAccountService" class="com.demo.service.impl.IAccountServiceImpl"></bean>
</beans>
3.两者不同方式读取bean.xml文件进行获取bean对象:
public class Client {
/**
* ClassPathXmlApplicationContext : 它可以加载类路径下的配置文件,要求配置文件在类路径下,不在的话加载不了
* FileSystemXmlApplicationContext : 他可以加载磁盘任意路径下的配置文件(必须有访问权限才行)
*/
* 核心容器的接口引发出的问题:
* ApplicationContext: 单例对象适用 (常用)
* 它在创建容器时,创建对象采取的策略是采用立即加载的方式,也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
* BeanFactory: 多例对象适用
* 它在创建容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
public static void main(String[] args) {
//第一种加载方式:加载类任意路径下的配置文件
System.out.println("第一种加载方式:加载类任意路径下的配置文件");
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("E:\\Demo\\demo02\\demo01\\src\\main\\resources\\bean.xml");
//根据id获取Bean对象
AccountDao accountDao = (AccountDao) applicationContext.getBean("AccountDao");
// //.class获取AccountDao对象
AccountDao accountDaoClass = applicationContext.getBean("AccountDao", AccountDao.class);
IAccountService iAccountService = (IAccountService) applicationContext.getBean("IAccountService");
System.out.println("AccountDao=" + accountDao + "\n" + "IAccountService=" + iAccountService + "\n" + "accountDaoClass=" + accountDaoClass);
System.out.println("第二种加载方式:加载类路径下的配置文件");
//第二种加载方式:加载类路径下的配置文件
ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("bean.xml");
//根据id获取Bean对象
AccountDao accountDao1 = (AccountDao) applicationContext1.getBean("AccountDao");
IAccountService iAccountService1 = (IAccountService) applicationContext.getBean("IAccountService");
//.class获取IAccountService对象
IAccountService IAccountServiceClass = applicationContext1.getBean("IAccountService", IAccountService.class);
System.out.println("AccountDao1=" + accountDao1 + "\n" + "iAccountService1=" + iAccountService1 + "\n" + "IAccountServiceClass=" + IAccountServiceClass);
System.out.println("--------------BeanFactory----------------");
Resource resource = new ClassPathResource("bean.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
IAccountService iAccountService = (IAccountService) beanFactory.getBean("IAccountService");
System.out.println(iAccountService);
}
}
到这里bean.xml文件装配bean获取对象到此完成,看看运行结果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/105094.html