Spring常用注解的使用------Spring学习日志2

一:Spring的常用注解

@Controller //用于注解Contorl层,识别为Contorl层的bean
@ Service //用于注解service层,识别为Service层的Bean
@ Repository //主要和dao层打交道,识别为dao层的Bean
@Component//这个不用,了解即可,因为他可以放在类的头上
@Autowired //这个是根据类型注入,如果根据类型找不到,他会根据name找,如果还找不到就报异常
@Resource//默认按照名称去找,比如@Resource(name=“zhangsan”)

二:基本使用Demo

还是先创建dao层

public interface User {
    //创建了一个名为User的接口.并且定义了一个接口方法shou(ps:其实我打错了,应该是show)
    public void shou();
}


紧接着要创建他的实现类,并且实现接口的方法

 //这里使用dao层的注解@Repository指向User接口
@Repository("User")
public class UserImp implements User {
    @Override
    //实现了接口中的方法
    public void shou() {
        System.out.println("Dao");
    }
}

然后创建service层的接口,并且船家女一个接口方法shou

public interface UserService {

    public void show();
}

之后创建他的实现类,并且实现接口中的方法

//这里使用Service的注解,并且指向他的接口
@Service("UserService")
public class UserServiceImp implements UserService {
	//创建User接口,并且使用@Autowired根据类型进行自动装配
    @Autowired
    private User user;

    @Override
    public void show() {
        user.shou();
        System.out.println("Service");
    }
}

然后再创建Contorl层,并且使用@Contorller标记

@Controller("ThisContorller")
public class ThisContorller {

    //创建UserService的接口并且使用@Autowired注解自动装配
    @Autowired
    private UserService userService;

    public void show() {
        userService.show();
        System.out.println("Controller");
    }
}

在之后就可以常见测试类了

public class test04 {

    private UserService userService;

    @Test
    public void test04(){
        String xmlPath="applocationContextAuto.xml";
        ApplicationContext applicationContext= new  ClassPathXmlApplicationContext(xmlPath);
        ThisContorller thisContorller= (ThisContorller) applicationContext.getBean("ThisContorller");
        thisContorller.show();
    }

}

开启注解需要配置配置文件约束信息,并且再base-package属性中指明要扫描哪些包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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:annotation-config />
    <context:component-scan base-package="dao,factiory,Service,test,controller" />

</beans>

运行之后我们就可以发现如下信息

三月 10, 2019 4:56:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3419866c: startup date [Sun Mar 10 16:56:52 CST 2019]; root of context hierarchy
三月 10, 2019 4:56:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applocationContextAuto.xml]
Dao
Service
Controller

证明他首先经过了数据库访问层Dao,然后经过了服务层Service,最后才经过了控制层Contorl

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值