加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【spring框架】beans.xml规范与ClassPathXmlApplicationContext

发布时间:2020-12-16 06:28:01 所属栏目:百科 来源:网络整理
导读:1.beans.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:schemaLoc

1.beans.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-2.5.xsd">

  <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl">
  </bean>
	
  <bean id="userService" class="com.bjsxt.service.UserService">
  	<property name="userDAO" ref="u" />
  		
  </bean>
  
</beans>


2.ClassPathXmlApplicationContext类介绍

ClassPathXmlApplicationContext这个类实现了BeanFactory、ApplicationContext等接口。

ApplicationContext的父接口是BeanFactory,所以,在下面的小例子中,BeanFactory可以改成ApplicationContext
@Test
public void testAdd() throws Exception{
BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml");

UserService userService=(UserService)ctx.getBean("userService");
User u=new User();
u.setUsername("u1");
u.setPassword("p1");
userService.add(u);
}


那么该用ApplicationContext还是BeanFactory呢?建议你用ApplicationContext,因为ApplicationContext是建立在BeanFactory之上的,ApplicationContext延伸出了更多的方法。BeanFactory只是完成了Bean工厂的一些功能,像Bean的一些生命周期它都处理不了。但是ApplicationContext除了能完成BeanFactory的所有功能之外,还可以完成一些其他的附加功能,比如说生命周期等待。

转载注明出处:http://blog.csdn.net/acmman

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读