小王:想要把一个bean交给Spring去管理,你想怎么做!
小李:简单啊,在Spring的配置文件里面配置<bean id="" class=""></bean>不就行了。
小王:不行啊,我们的项目太恶心了,用了一万个bean,写配置文件快累死了,密密麻麻的看不清楚
小李:额!这个也太奇葩了,怎么办,翻翻书吧,有没有好方法。
这是什么?自动装配?我擦,这么神奇?下面我们开启神奇的Spring自动装配之旅。
Spring自动装配,目的就是解放Spring的配置文件,我们来看看解放以后的配置文件
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <context:component-scan base-package="com"></context:component-scan> </beans>
小李:我擦,我得配置一万个bean呢,怎么现在只有一行了,靠谱吗!
小王:试试不就知道了。
简单的模拟数据库的bean
package com.dao; import org.springframework.stereotype.Repository; @Repository public class OracleDao { //模拟数据的插入操作,但是这里只打印一句话 public void insert() { System.out.println("哈哈,我往数据库插入了一条数据"); } }
测试类:
package test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.PersonService; public class TestPersonService { @Test public void test() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); OracleDao oracleDao = (OracleDao)applicationContext.getBean("oracleDao"); System.out.println(oracleDao); } }
打印结果是:com.dao.OracleDao@19ec4ed
不是空指针吗,我擦Spring真的生效了!!!!
但是@Repository是个什么东西。有问题吗,问360(为什么不是百度)硬广,还没有广告费
360说:Spring为了能够实现bean的自动扫描,提供几个注解@Component、@Controller、@Repository、@Service你只要把这几个注解任选一个放到你想让Spring管理的类的上面,Spring就会自动把这个类纳入到自己的管辖范围,就像例子一样。
等等,任选一个,为什么!
其实Spring也想通过注解给各个bean分层的,比如@Controller就是对应mvc的表现层,@Service对应的就是服务层,@Repository对应的持久层,@Component就是所有组件的统称,这样方便以后程序分层。
等等,这是注解吧,注解可是要开启对应的解析器的哦,比如依赖注入的时候使用注解就必须开启
<context:annotation-config/>,这个不用吗?
是不,不用,在声明<context:component-scan base-package="com"></context:component-scan>自动扫描的时候Spring已经自动帮你开启了注解的解析器,啊,神奇的Spring啊。
现在,你的配置文件清爽多了吧,不过问题又来了,菜鸟看不懂了怎么办?
o(︶︿︶)o 唉把这个博客给他看看吧。
小贴士:
- 自动装配的时候Spring默认给你的bean的取了名字,就是类名的第一个字母小写
- 如果你想把这个bean换成自己的名字(好吧,受不了你了)只需要在@Component等这几个注解后面价格小括号,像这样@Component(“ILOVEYOU”),下次你去Spring里面找ILOVEYOU的时候就能找到你这个bean了
- 默认Spring管理你的bean的作用域是单例,就是Spring容器启动的时候就把你的bean初始化一个放到他自己的仓库里(其实是一个map),以后你每次找他要他都从map里拿一个给你,想改怎么办,每次你给我一个新的,好吧,这样@Scope("prototype")每次给你一个新的
- 我想给我的bean加初始化方法和销毁方法行吗? 好的,@PostConstruct,放到初始化方法,@PreDestroy放到销毁方法,这样初始化完你的bean就会调用你的方法了
我靠,这么强大,服了没!
相关推荐
在Spring框架中,`<context:component-scan/>`元素是核心组件扫描的基石,它允许我们自动检测和注册...在实际项目中,结合使用`@Component`家族注解和`<context:component-scan/>`,能够构建出高效、灵活的Spring应用。
Spring 组件扫描<context:component-scan/>使用详解 在 Spring 框架中,组件扫描是指通过注解和 XML 配置来自动检测和加载Bean的过程。下面将详细介绍<context:component-scan/>标签的使用方式和原理。 一、...
<context:component-scan base-package="com.example"/> ``` 这将扫描指定的类包和其递归子包中的所有类,并将其注册到 Spring 容器中。 Spring支持四种类型的过滤方式: 1. 注解过滤:使用 `@SomeAnnotation` ...
//applicationContext.xml文件中添加 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... <context:component-scan base-package="com.haijian" />
<context:component-scan base-package="testspring.main"/> </beans> ``` 在上面的配置文件中,我们使用了 `<context:annotation-config/>` 和 `<context:component-scan>` 两个元素。`<context:annotation-...
<context:component-scan base-package="com.mvc.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> ``` 2. **数据库连接配置...
在上面的例子中,`<context:component-scan>` 标签指定了一个基础包 `com.example.package`,Spring 会在这个包及其子包下寻找所有使用了 @Component、@Repository、@Service 或 @Controller 等注解的类,并自动将...
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
当在Spring配置文件中加入`<context:component-scan base-package="leot.test"/>`,Spring会扫描指定包(本例中为"leot.test")及其子包下的所有类,查找带有上述注解的类,并将其注册为Spring管理的Bean。...
<context:component-scan base-package="com.example.service"/> ``` #### 五、实现业务逻辑 根据需求,需要实现用户数据的CRUD操作。可以通过以下步骤来实现: 1. **实体类**:设计User类,包含用户名、密码等...
<context:component-scan base-package="com.bbs"/> <!--注解支持--> <mvc:annotation-driven/> <!--视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view....
要启用Spring的自动扫描功能,需要在配置文件中添加`<context:component-scan>`标签,并设置其`base-package`属性以指定需要扫描的包路径。例如,在给定的配置示例中,通过以下XML片段实现了这一功能: ```xml ...
本文将深入探讨Spring 3.0中依赖注入的新特性,特别是如何使用`@Repository`、`@Service`、`@Controller`和`@Component`注解来标记类为Bean,以及如何利用`<context:component-scan/>`元素自动扫描和注册这些Bean。...
<context:component-scan base-package="com.example.package"/> <!-- 开启AOP支持 --> <aop:aspectj-autoproxy/> <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource....
<context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org....
<context:component-scan base-package="com.yourpackage" /> <!-- 指定包含Controller的包 --> <mvc:annotation-driven /> </beans> ``` `<context:component-scan>`注解扫描指定的包,查找带有注解的组件,...
- 通过`<context:component-scan base-package="controller"/>`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图解析器以解析返回的视图名称为实际的视图资源。 ```xml <beans xmlns=...
<context:component-scan base-package="com.makelove88.**.dao,com.makelove88.**.service" /> <import resource="classpath*:*/applicationContext-*.xml" /> </beans> ``` 在这个修正后的配置中,我们添加了 ...
<context:component-scan base-package="com.wdl.cn.controllers"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> ...
`<context:annotation-config/>` 和 `<context:component-scan base-package="需要实现注入的类所在包"/>` 是两个重要的 XML 配置元素,它们用于开启注解支持和指定扫描的包范围。 - `<context:annotation-config/>...