SSH笔记-Spring的annotation配置注入关系(使用@Component、@Service、@Repository、@Controller)

1、通过annotation配置注入关系的方法,能简化配置bean的步骤,只需要对制定的bean类使用@Component、@Service、@Repository、@Controller标签,并且在配置文件中使用context:component-scan标签来一次性地扫描即可,使用的时候从IOC容器中直接使用bean名字(头字母小写)即可获取到bean

2、配置文件的命名空间需要勾选上context的选项

3、标签使用位置;
① @Component:主要用于定义组件,一般用于DAO上使用。
② @Service:主要用于定义组件,一般用于Service上用。
③ @Repository:主要用于定义组件,一般用于Action上使用。
③ @Controller:主要用于定义组件,一般用于Controller上使用。

4、context:component-scan相关属性:
①base-package:需要扫描的包
②resource-pattern:指定需要扫描包下的那个类(如:配置resource-pattern=”component/*.class”,即扫描包下以component开头的类)
③context:exclude-filter:不扫描使用指定注解的类(如:type=”annotation” expression=”org.springframework.stereotype.不扫描的注解类型”)
④context:include-filter:扫描使用指定注解的类(同上)
⑤第三第四点中expression,不写org.springframework.stereotype.XXX的话,可以写类的全类名,以此指定要对某一个类操作

5、使用步骤:
①创建bean类
②在bean类上添加@Component、@Service、@Repository、@Controller标签其中一个,在spring配置文件中使用context:component-scan标签,并在其base-package属性中配置需要扫描的bean的包名,如果需要可使用context:component-scan标签的其他属性配置
③在调用测试类中配置IOC容器,获取bean(bean名为Bean类名头字母小写)
④使用bean内的方法

6、文件
①TestAnnotation.java:测试类
②InfoComponent.java:测试@Component类
③InfoController.java:测试@Controller类
④InfoRepository.java:测试@Repository类
⑤InfoRepositoryImpl.java:InfoRepository的接口类
⑥InfoService.java:测试@Service类
⑦annotationContext.xml.java:配置文件

7、TestAnnotation.java

package com.demo.sshtest.annotation;

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

import com.demo.sshtest.annotation.component.InfoComponent;
import com.demo.sshtest.annotation.controller.InfoController;
import com.demo.sshtest.annotation.repository.InfoRepository;
import com.demo.sshtest.annotation.service.InfoService;

public class TestAnnotation {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("annotationContext.xml");
        InfoComponent infoComponent_appC = (InfoComponent)applicationContext.getBean("infoComponent");
        System.out.println(infoComponent_appC);
        InfoController infoController_appC = (InfoController)applicationContext.getBean("infoController");
        System.out.println(infoComponent_appC);
        InfoRepository infoRepository_appC = (InfoRepository)applicationContext.getBean("infoRepository");
        System.out.println(infoRepository_appC);
        InfoService infoService_appC = (InfoService)applicationContext.getBean("infoService");
        System.out.println(infoService_appC);

    }
}

8、InfoComponent.java

package com.demo.sshtest.annotation.component;

import org.springframework.stereotype.Component;

@Component
public class InfoComponent {

    public void component(){
        System.out.println("-------component--------");
    }
}

9、InfoController.java

package com.demo.sshtest.annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class InfoController {

    public void controller(){
        System.out.println("-------controller-------");
    }
}

10、InfoService.java

package com.demo.sshtest.annotation.service;

import org.springframework.stereotype.Service;

@Service
public class InfoService{

    public void service(){
        System.out.println("-------service-------");
    }
}

11、InfoRepository.java

package com.demo.sshtest.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository
public class InfoRepository implements InfoRepositoryImpl{

    public void respository(){
        System.out.println("-------respository-------");
    }
}

12、InfoRepositoryImpl.java

package com.demo.sshtest.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("infoRepository")
public interface InfoRepositoryImpl {

    void respository();
}

在InfoRepositoryImpl.java的@Repository中可以配置接口给那个类实现

13、项目目录
项目目录

14、demo
https://download.csdn.net/download/qq_22778717/10470238

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值