Spring-简单bean的配置(使用bean.xml手动完成)
2016-09-22 18:47
453 查看
简单实例练习:
(1)接口
PersonServiceBean.java:
(3)新建beans.xml配置文件
bean.xml
(1)接口
PersonService.java: package com.sw.servc; public interface PersonService { void save(); }(2)新建bean
PersonServiceBean.java:
package com.sw.service.impl; import com.sw.servc.PersonService; public class PersonServiceBean implements PersonService { /* (non-Javadoc) * @see com.sw.service.impl.PersonService#save() */ @Override public void save(){ System.out.println("save()"); } }
(3)新建beans.xml配置文件
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-2.5.xsd"> <!-- id值第一个字母使用小写 --> <bean id="personService" class="com.sw.service.impl.PersonServiceBean"></bean> </beans>(4)新建测试单元进行测试
package com.sw.junit.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sw.servc.PersonService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring () { //实例化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); //获取bean PersonService personService=(PersonService) ctx.getBean("personService");//接口引用 //调用业务方法 personService.save(); } }
相关文章推荐
- 使用XPath解析xml实现简单的Spring IOC完成bean的依赖注入
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- Spring框架——使用spring的特殊bean完成分散配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Struts2.0.11+ Hibernate3.6.0+Spring2.5.6的简单使用与配置
- 扩展Spring——使用 Annotation将配置资源注入到Bean中
- Spring aop 使用aspectjrt配置切面+ehcache简单配置
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- spring 2.5 中配置bean 使用annotation
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- 使用spring的特殊bean --- 对bean进行后处理和对bean工厂进行后处理简单了解
- 非使用hibernate配置实体类关联数据库表,简单javaBean开发时读取一条数据解决方案
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Spring学习笔记 使用annotation配置实现Bean的auto-wiring (自动绑定)
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题
- [注解的力量]Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- 使用Spring 2.5注解自动注册Bean的配置
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象