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){4个核心bean,core,context,expression1个依赖common.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. 配置文件⎩⎨⎧​位置:任意,常在classpath下(src下)名称:任意,如beans.xml常用applicationcontext.xml内容:schema约束<beanid="ServiceId"class=" .ServiceImpl"></bean>​

上一篇:【CF449D】Jzzhu and Numbers


下一篇:【洛谷P3185】分裂游戏