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. 自动检查点(Automatic Checkpointing)

    自动检查点(Automatic Checkpointing)在oracle10g,支持自动检查点调优,这样可以提高系统可用性.自动检查点调优需要开启参数fast_start_mttr_target. ...

  2. mysql的binlog安全删除

    理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越来越多的binlog,磁盘被吃掉了不少.可以 ...

  3. 证明 logX < X 对所有 X > 0 成立

    题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss       练习1.5(a)  证明下列公式: logX < X 对所有 X > ...

  4. 删除NSMutableArray中的二维数组

    // 删除模型数据 [self.mutableArr[indexPath.section] removeObjectAtIndex:indexPath.row]; //删除UI(刷新数据,UI) [s ...

  5. Topcoder SRM 597

    妈蛋第一场tc就掉分,提交了第一个题的时候就注定悲剧要发生了,妈蛋没考虑0就直接%了,真的是人傻见识又少,第二题最后有了一点思路,没时间写了,可能也不是很准确,第三题想了小会儿效果为0! 然后第一题傻 ...

  6. C++Builder 解决绘图闪动问题

    使用双缓冲 Form->DoubleBuffered = true; Panel->DoubleBuffered = true;

  7. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  8. poj 算法 分类

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 最近AC题:2528   更新时间:2011.09.22  ...

  9. 算法训练 Anagrams问题

    http://lx.lanqiao.org/problem.page?gpid=T223 算法训练 Anagrams问题   时间限制:1.0s   内存限制:512.0MB      问题描述 An ...

  10. contentProvider

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...