We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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 基于 Java 代码的 IoC 容器配置一文件中,我们是通过手动指定某个 Bean 加载到 IoC 容器中,那如果现在有几十个 Bean,难道我们还要每个指定吗?
Spring 为我们提供了自动检测并注册 Bean 的机制。以下是几种比较常见的组件扫描配置方式:
1、在 resources 目录中创建 spring-context.xml:
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:
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"); } }
1、创建配置类 SpringContextConfig.java:
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 { }
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"); } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
像Spring 基于 Java 代码的 IoC 容器配置一文件中,我们是通过手动指定某个 Bean 加载到 IoC 容器中,那如果现在有几十个 Bean,难道我们还要每个指定吗?
Spring 为我们提供了自动检测并注册 Bean 的机制。以下是几种比较常见的组件扫描配置方式:
通过 XML 启用组件扫描
1、在
resources
目录中创建spring-context.xml
:2、然后修改主类
MySpring.java
:通过 Java 注解启用组件扫描
1、创建配置类
SpringContextConfig.java
:2、然后修改主类
MySpring.java
:通过
scan(String…)
启用组件扫描此方法,我们只需要修改主类
MySpring.java
即可:The text was updated successfully, but these errors were encountered: