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,结果

  • 相关阅读:
    负载均衡session会话保持方法
    PHP分布式中Redis实现Session
    Nginx内置变量
    Nginx配置文件解析
    Nginx重写
    Nginx与Apache比较
    CGI概念
    Linux笔记(十四)
    Linux笔记(十三)
    hdu 4039
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9109229.html
Copyright © 2011-2022 走看看