`
wangchen2009hao
  • 浏览: 8596 次
社区版块
存档分类
最新评论

Spring注解<context:component-scan base-package />使用

阅读更多

    小王:想要把一个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扫描器—spring组件扫描使用详解

    在Spring框架中,`&lt;context:component-scan/&gt;`元素是核心组件扫描的基石,它允许我们自动检测和注册...在实际项目中,结合使用`@Component`家族注解和`&lt;context:component-scan/&gt;`,能够构建出高效、灵活的Spring应用。

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    &lt;context:component-scan base-package="com.makelove88.**.dao,com.makelove88.**.service" /&gt; &lt;import resource="classpath*:*/applicationContext-*.xml" /&gt; &lt;/beans&gt; ``` 在这个修正后的配置中,我们添加了 ...

    spring组件扫描contextcomponent-scan使用详解.pdf

    Spring 组件扫描&lt;context:component-scan/&gt;使用详解 在 Spring 框架中,组件扫描是指通过注解和 XML 配置来自动检测和加载Bean的过程。下面将详细介绍&lt;context:component-scan/&gt;标签的使用方式和原理。 一、...

    Spring注解详解

    &lt;context:component-scan base-package="com.example"/&gt; ``` 这将扫描指定的类包和其递归子包中的所有类,并将其注册到 Spring 容器中。 Spring支持四种类型的过滤方式: 1. 注解过滤:使用 `@SomeAnnotation` ...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... &lt;context:component-scan base-package="com.haijian" /&gt;

    spring注解使用

    &lt;context:component-scan base-package="testspring.main"/&gt; &lt;/beans&gt; ``` 在上面的配置文件中,我们使用了 `&lt;context:annotation-config/&gt;` 和 `&lt;context:component-scan&gt;` 两个元素。`&lt;context:annotation-...

    集成springmvc spring hibernate的配置

    &lt;context:component-scan base-package="com.mvc.*"&gt; &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt; &lt;/context:component-scan&gt; ``` 2. **数据库连接配置...

    Spring注释 注入方式源码示例,Annotation

    &lt;context:component-scan base-package="Mode"&gt;&lt;/context:component-scan&gt; //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...

    spring mvc

    当在Spring配置文件中加入`&lt;context:component-scan base-package="leot.test"/&gt;`,Spring会扫描指定包(本例中为"leot.test")及其子包下的所有类,查找带有上述注解的类,并将其注册为Spring管理的Bean。...

    框架ssm整合

    &lt;context:component-scan base-package="com.example.service"/&gt; ``` #### 五、实现业务逻辑 根据需求,需要实现用户数据的CRUD操作。可以通过以下步骤来实现: 1. **实体类**:设计User类,包含用户名、密码等...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view....

    spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)

    要启用Spring的自动扫描功能,需要在配置文件中添加`&lt;context:component-scan&gt;`标签,并设置其`base-package`属性以指定需要扫描的包路径。例如,在给定的配置示例中,通过以下XML片段实现了这一功能: ```xml ...

    spring3.0依赖注入详解

    本文将深入探讨Spring 3.0中依赖注入的新特性,特别是如何使用`@Repository`、`@Service`、`@Controller`和`@Component`注解来标记类为Bean,以及如何利用`&lt;context:component-scan/&gt;`元素自动扫描和注册这些Bean。...

    Spring环境配置

    &lt;context:component-scan base-package="com.example.package"/&gt; &lt;!-- 开启AOP支持 --&gt; &lt;aop:aspectj-autoproxy/&gt; &lt;!-- 数据源配置 --&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource....

    SpringMVC+Hibernate全注解整合

    &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    spring-mvc 注解方式xml配置

    &lt;context:component-scan base-package="com.yourpackage" /&gt; &lt;!-- 指定包含Controller的包 --&gt; &lt;mvc:annotation-driven /&gt; &lt;/beans&gt; ``` `&lt;context:component-scan&gt;`注解扫描指定的包,查找带有注解的组件,...

    自己写网页spring框架搭建

    - 通过`&lt;context:component-scan base-package="controller"/&gt;`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图解析器以解析返回的视图名称为实际的视图资源。 ```xml &lt;beans xmlns=...

    SPring注解及页面跳转实例

    &lt;context:component-scan base-package="com.wdl.cn.controllers"/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/page/"/&gt; ...

    spring注解

    &lt;context:component-scan base-package="com.example"/&gt; ``` 这将扫描指定包及其递归子包中的所有类,并将它们注册到 Spring 容器中。 注解类型 Spring 提供了多种类型的注解,包括: * `@Controller`:标记一个...

Global site tag (gtag.js) - Google Analytics