• 依赖注入


    1,接口类

    package com.songyan.demo1;
    
    public interface UserDao {
    public void say();
    }
    package com.songyan.demo1;
    
    public interface UserService {
    public void say();
    }

    2,实现类

    package com.songyan.demo1;
    
    
    public class UserDaoImpl implements UserDao{
        public void say()
        {
            System.out.println("UsdeDao say helloWord");
        }
    
    }
    package com.songyan.demo1;
    
    
    public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public void setUserDao(UserDao userDao)
    {
        this.userDao=userDao;
    }
    public void say()
    {
        this.userDao.say();
        System.out.println("hello");
    }
    }

    3,配置文档

    <?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.xsd">
    
    <bean id="userDao" class="com.songyan.demo1.UserDaoImpl" ></bean>
    <bean id="userService" class="com.songyan.demo1.UserServiceImpl">
    <property name="userDao" ref="userDao"></property></bean>
    </beans>

    3,测试类

    package com.songyan.demo1;
    
    import javax.xml.registry.infomodel.User;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    public static void main(String[] args) {
    	//初始化容器,加载xml
    	ApplicationContext application =new ClassPathXmlApplicationContext("applicationContext.xml");
    	//通过容器获取javabean对象实例
    	UserDao userDao=(UserDao)application.getBean("userDao");
    	//调用实例的方法
    	userDao.say();
    	//通过容器获取UserService对象
    	UserService userService=(UserService)application.getBean("userService");
    	userService.say();
    }
    }
    

      

  • 相关阅读:
    常用正则表达式
    C语言的指针与二维数组
    【原创】datalist实现简单分页功能
    【原创】datalist的页脚访问和控制
    [原创]手动删除顽固病毒总结
    [zz]复杂指针解析
    极度郁闷的一次电脑维修经历
    武汉城市地铁规划图
    [转]objc_msgSend 的 ARM 汇编分析
    [转]Cydia and XCode Local App Testing
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9100371.html
Copyright © 2020-2023  润新知