zoukankan      html  css  js  c++  java
  • bean装配--注解

    1,Dao层

    package com.songyan.zhujie;
    
    public interface UserDao {
        public void say();
    
    }
    package com.songyan.zhujie;
    
    import org.springframework.stereotype.Repository;
    
    @Repository("userDao")
    public class UserDaoImpl implements UserDao {
    public  void say() {
        System.out.println("dao say   ");
    
    }
    }

    2,Service层

    package com.songyan.zhujie;
    
    public interface UserService {
        public void say();
    }
    package com.songyan.zhujie;
    
    import org.springframework.stereotype.Repository;
    
    @Repository("userDao")
    public class UserDaoImpl implements UserDao {
    public  void say() {
        System.out.println("dao say   ");
    
    }
    }

    3,Controller层

    package com.songyan.zhujie;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    
    @Controller("contraller")
    public class UserController {
        @Resource(name="userService")
        private UserService userService;
        public void say(){
            userService.say();
            System.out.println("controller say");
        }
        
    
    }

    4,配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        <context:component-scan base-package="com.songyan.zhujie" />
    </beans>

    5,测试

    package com.songyan.zhujie;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestZhujie {
        public static void main(String[] args) {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/zhujie/beans6.xml");
            UserController userController=(UserController)applicationContext.getBean("contraller");
            userController.say();
            
        }
    
    }

    6,结果

  • 相关阅读:
    牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解
    2019牛客多校第九场B Quadratic equation(二次剩余定理)题解
    URAL 1132 Square Root(二次剩余定理)题解
    牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解
    配置android.support.v7.widget.Toolbar 搜索框样式
    Google之路
    Editplus 竖选,竖插入技巧
    JNDI
    Spring Hibernate Transaction示例
    AOP 入门
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9109229.html
Copyright © 2011-2022 走看看