spring学习22-分层架构

 

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.geyao</groupId>
        <artifactId>spring01geyao</artifactId>
        <version>1.0-SNAPSHOT</version>
     
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.13.RELEASE</version>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>3.2.0.RELEASE</version>
            </dependency>
     
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.4.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.4.RELEASE</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

UserDao类

    package com.geyao.demo.dao;
     
    public interface UserDao {
     
        void add();
    }

UserService类

    package com.geyao.demo.service;
     
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
     
    public interface UserService {
        void add();
     
        @Configuration
        @ComponentScan
        class Appconfig {
        }
    }

userController

    package com.geyao.demo.web;
     
    import com.geyao.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
     
    @Component("UserController")
    public class UserController {
        @Autowired
        @Qualifier("UserServiceNormal")
        private UserService userService;
     
        public void add(){
            userService.add();
        }
    }

Appconfig

    package com.geyao.demo;
     
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
     
    @Configuration
    @ComponentScan
    public class Appconfig {
    }

log4j.properties

    ### 设置###
    log4j.rootLogger = INFO,stdout
     
    ### 输出信息到控制抬 ###
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
    log4j.category.org.springframework.beans.factory=DEBUG

userServiceTest

    package com.geyao.demo.service;
     
    import com.geyao.demo.Appconfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import javax.annotation.Resource;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Appconfig.class)
        public class UserServiceTest {
        //@Autowired
        //@Qualifier("")
        @Resource(name="userServiceFestival")
     
       private UserService userService;
     
        @Test
        public void  testAdd(){
            userService.add();
        }
    }

usercontrollerTest

    package com.geyao.demo.web;
     
    import com.geyao.demo.Appconfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Appconfig.class)
     
    public class UserControllerTest {
        @Autowired
        @Qualifier("UserController")
        private UserController userController;
        @Test
     
        public void testAdd(){
            userController.add();
        }
    }

userservicefestival

    package com.geyao.demo.web;
     
    import com.geyao.demo.Appconfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Appconfig.class)
     
    public class UserControllerTest {
        @Autowired
        @Qualifier("UserController")
        private UserController userController;
        @Test
     
        public void testAdd(){
            userController.add();
        }
    }

userserviceNormal

    package com.geyao.demo.service.com.geyao.demo.service.impl;
     
    import com.geyao.demo.dao.UserDao;
    import com.geyao.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
     
    @Component
     
    public class UserServiceNormal implements UserService
    {
        @Autowired
    private UserDao userDao;
        public void add()
        {
            userDao.add();
        }

posted @   前端导师歌谣  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示