-
导包
-
spring要管理的类信息
package com.liuyong666.service.impl;
import com.liuyong666.service.PersonService; public class PersonServiceBean implements PersonService { @Override public void save(){ System.out.println("我是save()方法"); } }
-
spring面向接口编程,该类对应的接口
public interface PersonService {
public abstract void save();
} -
配置beans.xml文件,文件在src目录下
<bean id="personService" class="com.liuyong666.service.impl.PersonServiceBean"></bean>
-
使用spring获取bean
@Test
public void getBeanBySpring(){
//参数为数组表明可以加载多个配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{“beans.xml”});
//面向接口编程,用接口引用实现它的实例对象
PersonService bean = (PersonService) context.getBean(“personService”);
【Spring 系列】一、如何从spring中获取bean
最新推荐文章于 2024-06-11 17:14:22 发布