@Repository 、@Service、@Controller案例详解

web开发,提供3个@Component注解衍生注解(功能一样)取代
@Repository :dao层
@Service:service层
@Controller:web层

套路是这样的,service层调动dao层,web层调动service层,展示出来

下面就是案例演示了

dao层代码:

package com.fly.spring.annotation;

import org.springframework.stereotype.Repository;

@Repository("sudentDao")
public class SudentDao {

    public void save(){
        System.out.println("dao层");
    }
}

service层代码:

package com.fly.spring.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class PersonService {

    private SudentDao sudentDao;

    @Autowired
    @Qualifier("sudentDao")   // 按照名称注入
    public void setSudentDao(SudentDao sudentDao){
        this.sudentDao = sudentDao;
    }

    // 给web层调用的方法
    public void getString(){
        sudentDao.save();
    }

}

web层代码:

package com.fly.spring.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("teacherWeb")
public class TeacherWeb {

    @Autowired //默认按照类型
    private PersonService person;

    public void execute(){
        person.getString();
    }

}

配置文件代码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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 组件扫描,扫描含有注解的类 -->
    <context:component-scan base-package="com.fly.spring.annotation"></context:component-scan>
</beans>

测试代码:

package com.fly.spring.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {

    @Test
    public void demo(){
        String xmlpath = "com/fly/spring/annotation/applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);
        TeacherWeb bean = (TeacherWeb) context.getBean("teacherWeb");
        bean.execute();
    }
}

测试结果:

2017-3-2 10:14:04 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1b016632: startup date [Thu Mar 02 10:14:04 CST 2017]; root of context hierarchy
2017-3-2 10:14:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/fly/spring/annotation/applicationContext.xml]
2017-3-2 10:14:06 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@43ee148b: defining beans [personService,sudentDao,teacherWeb,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
dao层
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值