• 依赖注入


    1.

    2.Test.java

    package com.inspur;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
            Userdao userDao =(Userdao) application.getBean("userId");
            userDao.say();
            
            UserService userService = (UserService)application.getBean("userService");
            userService.say();
            
    
        }
    
    }

    3.Userdao.java

    package com.inspur;
    
    public interface Userdao {
        public void say();
    
        
    
    }

    4.UserDaoImpI.java

    package com.inspur;
    
    public class UserDaoImpI implements Userdao {
    
        public void say() {
            // TODO Auto-generated method stub
            System.out.println("Spring say hello world");
        }
    
    }

    5.UserService.java

    package com.inspur;
    
    public interface UserService {
        public void say();
    
    }

    5.UserServiceImpl.java

    package com.inspur;
    
    public class UserServiceImpL implements UserService {
        private Userdao userDao;
    
        public void setUserDao(Userdao userDao) {
            this.userDao = userDao;
        }
    
        public void say(){
            System.out.println("UserServiceImpL say Hello world");
        }
    
    }

    6.applicationContext.xml

    <?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="userId" class="com.inspur.UserDaoImpI">
        </bean>
        <bean id="userService" class="com.inspur.UserServiceImpL">
        <property name="userDao" ref="userId"></property>
        </bean>
    </beans>

    name=“userDao"是UserServiceImpl的

    ref="userId"是

  • 相关阅读:
    javascript编程解决黑化的牛牛问题
    Eclipse中给jar包导入JavaDoc注释文档的方法
    Android网络连接处理学习笔记
    Android Animation学习笔记
    Android中动画学习
    Android 动画效果 --Animation 动画专题研究 一
    Android动画学习笔记-Android Animation
    Android 动画之RotateAnimation应用详解
    Android 动画之ScaleAnimation应用详解
    Android 动画之AlphaAnimation应用详解
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9100386.html
Copyright © 2020-2023  润新知