Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring 自动检测和注册 Bean #109

Open
JasonWu73 opened this issue Jun 5, 2020 · 0 comments
Open

Spring 自动检测和注册 Bean #109

JasonWu73 opened this issue Jun 5, 2020 · 0 comments
Labels
参考 示例程序

Comments

@JasonWu73
Copy link
Owner

Spring 基于 Java 代码的 IoC 容器配置一文件中,我们是通过手动指定某个 Bean 加载到 IoC 容器中,那如果现在有几十个 Bean,难道我们还要每个指定吗?

Spring 为我们提供了自动检测并注册 Bean 的机制。以下是几种比较常见的组件扫描配置方式:

通过 XML 启用组件扫描

1、在 resources 目录中创建 spring-context.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 https://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="net.wuxianjie.demo.spring"/>

</beans>

2、然后修改主类 MySpring.java

package net.wuxianjie.demo.spring;

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

public class MySpring {

  public static void main(String[] args) throws Exception {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");

    UserService userService = applicationContext.getBean("userService", UserService.class);
    userService.userLogin("Jason", "123");
  }
}

通过 Java 注解启用组件扫描

1、创建配置类 SpringContextConfig.java

package net.wuxianjie.demo.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "net.wuxianjie.demo.spring")
public class SpringContextConfig {

}

2、然后修改主类 MySpring.java

package net.wuxianjie.demo.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MySpring {

  public static void main(String[] args) throws Exception {

    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringContextConfig.class);

    UserService userService = applicationContext.getBean("userService", UserService.class);
    userService.userLogin("Jason", "123");
  }
}

通过 scan(String…​) 启用组件扫描

此方法,我们只需要修改主类 MySpring.java 即可:

package net.wuxianjie.demo.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MySpring {

  public static void main(String[] args) throws Exception {

    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.scan("net.wuxianjie.demo.spring");
    applicationContext.refresh();

    UserService userService = applicationContext.getBean("userService", UserService.class);
    userService.userLogin("Jason", "123");
  }
}
@JasonWu73 JasonWu73 added 参考 示例程序 知识点 单知识点 手册 备忘速查 and removed 参考 示例程序 知识点 单知识点 手册 备忘速查 labels Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
参考 示例程序
Projects
None yet
Development

No branches or pull requests

1 participant