1.基于注解的配置:

@Component: 基本注解, 标识了一个受 Spring 管理的组件

@Respository: 标识持久层组件

@Service: 标识服务层(业务层)组件

@Controller: 标识表现层组件

将这些架包放入在工程目录下建立的lib文件夹里,并解压

commons-logging-1.1.1

spring-aop-4.0.0.RELEASE

spring-beans-4.0.0.RELEASE

spring-context-4.0.0.RELEASE

spring-core-4.0.0.RELEASE

spring-expression-4.0.0.RELEASE

---------------------------------------------------------------------------

建立接口:UserRepository

package com.atguigu.spring.beans.annotation.test;

public interface UserRepository {
void save();
}

建立类:UserRepositoryImpl继承于接口:UserRepository

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Repository;

@Repository("userRepository") //标识持久层组件
public class UserRepositoryImpl implements UserRepository { public void save() {
System.out.println("panpan123");
}
}

建立类:UserService

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Service;

@Service  //标识服务层(业务层)组件
public class UserService {
public void add(){
System.out.println("panpan456");
}
}

建立类:UserController

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Controller;

@Controller  //标识表现层组件
public class UserController {
public void test(){
System.out.println("panpan789");
}
}

上述类都是建立在包com.atguigu.spring.beans.annotation.test之下;

spring的xml配置文件:beansannotation.xml,在com.atguigu.spring.beans.annotation.test包或子包 下带有上述四个注解的,可以被IOC容器识别

<context:component-scan  base-package="com.atguigu.spring.beans.annotation.test" >
</context:component-scan>

加入resource-pattern="repository/*.class,是指识别repository注解的类;

<context:component-scan   base-package="com.atguigu.spring.beans.annotation.test"  resource-pattern="repository/*.class">
</context:component-scan>

注解过滤:指不包含 类型是 注解,导入的包(import后面的)是org.springframework.stereotype.Repository的类;

<context:component-scan
base-package="com.atguigu.spring.beans.annotation"
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

取消默认的use-default-filters="false",包含...

<context:component-scan
base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

类过滤:type="assignable",不包含全类名是com.atguigu.spring.beans.annotation.test.UserRepository

<context:component-scan
base-package="com.atguigu.spring.beans.annotation"
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.annotation.test.UserRepository"/>
</context:component-scan>

类过滤:type="assignable",取消默认的use-default-filters="false",只包含全类名为com.atguigu.spring.beans.annotation.test.UserRepositoryImpl类

<context:component-scan
base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.annotation.test.UserRepositoryImpl"/>
</context:component-scan>

建立Main类,进行测试:

package com.atguigu.spring.beans.annotation.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beansannotation.xml"); TestObject test=(TestObject) ctx.getBean("testObject");
System.out.println(test); UserController controller=(UserController) ctx.getBean("userController");
System.out.println(controller); UserService service=(UserService) ctx.getBean("userService");
System.out.println(service); UserRepositoryImpl repository=(UserRepositoryImpl) ctx.getBean("userRepository");
System.out.println(repository);
} }

Spring框架bean的配置(3):基于注解的配置的更多相关文章

  1. Spring框架第四篇之基于注解的DI注入

    一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...

  2. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

  3. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  4. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  5. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  6. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  7. Spring 基于注解的配置 简介

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  8. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  9. 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题

    解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题 Alternatively, create an own implementation of ...

随机推荐

  1. C# ?和??运算表达式

    1.单问号(?) 作用:用于给变量设初化的时候,给变量(int类型)赋为null值,而不是0. 例子: public int a; //默认值为0 public int ?b; //默认值为null ...

  2. 伪类写border, transform: scale3d() 及兼容性

    .top::before { content: ""; position: absolute; left: 0; width: 200%; height: 0; border-to ...

  3. linux:指令与档案的搜索

    linux下的五种搜索方法(参考自鸟哥linux私房菜基础篇): 一.find :功能很强大,直接搜寻整个硬碟的(速度不是很快,如果系统硬碟较旧的话)----特色:find后面可以接多个目录搜索,它本 ...

  4. 0428—Scrum团队成立及《构建之法》第六、七章读后感

    5.Scrum团队成立 5.1 团队名称:喳喳 团队目标:突破渣渣 团队口号:吱吱喳喳 团队照: 5.2 角色分配 产品负责人: 112冯婉莹 Scrum Master:109张鑫相 PM项目经理:1 ...

  5. !!常见的上穿突破M20方式——突破还是试探的判断

    1和2相似之处在于M5<M20, 最大的区别是M20和M5之间的间距在放大还是缩小,如果是放大,大盘会先试探一下.如果越缩越小,大盘必须选择方向 但是必须注意的是,即使是夹角缩小,选在方向不一定 ...

  6. php初探

    1.php中的连接符.可以连接多个字符串,相当于java中的+ 2.echo必须与后面的输出内容有至少一个空格 3.php编程中每个结尾都需要添加分号

  7. bzoj1834 [ZJOI2010]network 网络扩容

    第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...

  8. php扩展的基本安装

    phpize ./config --with-php-config=.. make&make install php.ini

  9. MDX Order排序

    Order基础用法: Numeric expression syntaxOrder(Set_Expression, Numeric_Expression [ , { ASC | DESC | BASC ...

  10. 手机端js模拟长按事件(代码仿照jQuery)

    代码编写: $.fn.longPress = function(fn) { var timeout = undefined; var $this = this; for(var i = 0;i< ...