zoukankan      html  css  js  c++  java
  • Spring_通过注解实现MVC案例

    配置好aop beans context 4.2的xsl

    @Scope:控制是否以单例的形式产生对象

    @Component(修饰控制层)下分为@Controller、@Service、@Repository,效果一样

    @Controller:修饰控制层

    @Service:修饰业务层,将业务层对象的创建交给spring容器

    @Repository:修饰DAO层,将DAO层对象的创建交给spring容器

    用了这几个,后面的括号都不用写了。

    DAO层:

    package cn.java.annotationioc;
    
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class FrontDaoImpl {
        
        public void addUser() {
            System.out.println("添加成功");
        }
    }

    Service层:

    package cn.java.annotationioc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class FrontServiceImpl {
        
        @Autowired
        private FrontDaoImpl fdi;
        public void userAdd() {
            fdi.addUser();
        }
    }

    Servlet层:

    package cn.java.annotationioc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component("frontServlet")
    public class FrontServlet {
        
        @Autowired
        private FrontServiceImpl fsi;
    
        public void doGet() {
            fsi.userAdd();
        }
    }

    主层:

    package cn.java.annotationioc;
    
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Window {
    
        public static void main(String[] args) {
    
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            FrontServlet fs = (FrontServlet) context.getBean("frontServlet");
            fs.doGet();
        }
    
    }

    主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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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.2.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <context:component-scan base-package="cn.java."></context:component-scan>
    
    </beans>
  • 相关阅读:
    网络流之转换为对偶图
    BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)
    BZOJ[HNOI2005]狡猾的商人(差分约束)
    BZOJ [ZJOI2007]矩阵游戏(二分图匹配)
    BZOJ 1191: [HNOI2006]超级英雄Hero(二分图匹配)
    BZOJ 1270: [BeijingWc2008]雷涛的小猫(DP)
    BZOJ 1303: [CQOI2009]中位数图
    BZOJ [HNOI2006]鬼谷子的钱袋
    BZOJ1002 [FJOI2007]轮状病毒(最小生成树计数)
    A* 算法讲解
  • 原文地址:https://www.cnblogs.com/lonske/p/9094160.html
Copyright © 2011-2022 走看看