linux怎么查看本机内存大小
228
2022-09-27
Spring - IOC(控制反转) & DI(依赖注入)
代码:
package com.imooc.ioc.demo1;/** * Created by jt on 2017/10/8. */public interface UserService { public void sayHello();}
package com.imooc.ioc.demo1;/** * Created by jt on 2017/10/8. */public class UserServiceImpl implements UserService{ // 添加属性: private String name; public void sayHello() { System.out.println("Hello Spring" + name); } public String getName() { return name; } public void setName(String name) { this.name = name; }}
package com.imooc.ioc.demo1;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.FileSystemResource;/** * Created by jt on 2017/10/8. */public class SpringDemo1 { @Test /** * 传统方式开发 */ public void demo1(){ // UserService userService = new UserServiceImpl(); UserServiceImpl userService = new UserServiceImpl(); // 设置属性: userService.setName("张三"); userService.sayHello(); } @Test /** * Spring的方式实现 */ public void demo2(){ // 创建Spring的工厂 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过工厂获得类: UserService userService = (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test /** * 读取磁盘系统中的配置文件 */ public void demo3(){ // 创建Spring的工厂类: ApplicationContext applicationContext = new FileSystemXmlApplicationContext("c:\\applicationContext.xml"); // 通过工厂获得类: UserService userService = (UserService) applicationContext.getBean("userService"); userService.sayHello(); } @Test /** * 传统方式的工厂类:BeanFactory */ public void demo4(){ // 创建工厂类: BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); // 通过工厂获得类: UserService userService = (UserService) beanFactory.getBean("userService"); userService.sayHello(); } @Test /** * 传统方式的工厂类:BeanFactory */ public void demo5(){ // 创建工厂类: BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("c:\\applicationContext.xml")); // 通过工厂获得类: UserService userService = (UserService) beanFactory.getBean("userService"); userService.sayHello(); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~