Spring Ioc测试暂记(用工厂取代new方法)(Spring笔记003)

j a r 包 ( 4 + 1 ) { 4 个 核 心 b e a n , c o r e , c o n t e x t , e x p r e s s i o n 1 个 依 赖 c o m m o n . l o g g i n g . j a r 日 志 jar包(4+1) \left\{\begin{array}{l}4个核心 bean ,core,context,expression\\1个依赖 common.logging.jar日志\end{array}\right. jar(4+1){4bean,core,context,expression1common.logging.jar

public interface UserService{
public void addUsre();
}
public class UserServiceImpl implments UserService {
public void addUsre(){输出语句};
}

测试类

public class TestIoc{
//原来的方式
UserService userService =new UserSviceImpl();
userService .addUser();
//现在的方式

//容器
String xmlpath ="com.···.beans.xml"
ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlpath);
//对象
UserSersvice userSvice =(UserSvice)applicationContext.getBean("UsersvicesID")
userSvice .adduser();
}

反射机制创建对象

配置文件.xml(可用注解取代)

配 置 文 件 { 位 置    :    任 意 , 常 在 c l a s s p a t h 下 ( s r c 下 ) 名 称    :    任 意 , 如 b e a n s . x m l    常 用 a p p l i c a t i o n c o n t e x t . x m l 内 容    :    s c h e m a 约 束 < b e a n   i d = " S e r v i c e I d "   c l a s s = "   . S e r v i c e I m p l " > < / b e a n > \mathrm{配置文件}\left\{\begin{array}{l}\mathrm{位置}\;:\;\mathrm{任意,常在classpath下(src下)}\\\mathrm{名称}\;:\;\mathrm{任意,如}beans.xml\;\mathrm{常用}applicationcontext.xml\\\mathrm{内容}\;:\;schema\mathrm{约束 <bean \ id="ServiceId" \ class=" ~.ServiceImpl"> </bean>}\end{array}\right. ,classpathsrc,beans.xmlapplicationcontext.xml:schema<bean id="ServiceId" class=" .ServiceImpl"></bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值