Spring入门(7)-自动检测Bean

Spring入门(7)-自动检测Bean

本文介绍如何自动检测Bean。

0. 目录

  1. 使用component-scan自动扫描
  2. 为自动检测标注Bean

1. 使用component-scan自动扫描

<?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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    >
    <context:annotation-config/>
    <context:component-scan base-package="com.chzhao.springtest"/>
</beans>

注:<context:annotation-config/>也可以去掉

package com.chzhao.springtest;

public interface IPersonBll {
    void show();
}

package com.chzhao.springtest;

import org.springframework.stereotype.Service;

@Service
public class PersonBll implements IPersonBll {
    public void show() {
        System.out.println("show message");
    }
}
package com.chzhao.springtest;

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

@Service
public class App {

    @Autowired
    private IPersonBll personBll;

    public IPersonBll getPersonBll() {
        return personBll;
    }

    public void setPersonBll(IPersonBll personBll) {
        this.personBll = personBll;
    }

    public void showMsg() {
        this.personBll.show();
    }

}
package com.chzhao.springtest;

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

public class Main {
    public static void main(String[] args) {
        ApplicationContext act = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        
        App a = (App) act.getBean(App.class);
        a.showMsg();
    }
}

2. 为自动检测标注Bean

会自动检测如下注解:

  • @Component:通用的构造型注解,标识该类为Spring组件
  • @Controller:标识该类为Spring MVC controller
  • @Repository:标识该类为数据仓库
  • @Service:标识此类定义为服务

@Component可以标注任意自定义注解,同时也可以命名Bean的ID。

package com.chzhao.springtest;

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

@Component("app1")
public class App {

    @Autowired
    private IPersonBll personBll;

    public IPersonBll getPersonBll() {
        return personBll;
    }

    public void setPersonBll(IPersonBll personBll) {
        this.personBll = personBll;
    }

    public void showMsg() {
        this.personBll.show();
    }

}
package com.chzhao.springtest;

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

public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext act = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        
        App a = (App) act.getBean("app1");
        a.showMsg();
    }
}

转载于:https://www.cnblogs.com/wardensky/p/4199407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值