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. 通用窗口类 Inventory Pro 2.1.2 Demo1(下)

    本篇想总结的是Inventory Pro中通用窗口的具体实现,但还是要强调下该插件的重点还是装备系统而不是通用窗口系统,所以这里提到的通用窗口类其实是通用装备窗口类(其实该插件中也有非装备窗口比如No ...

  2. Java知多少(19)访问修饰符(访问控制符)

    Java 通过修饰符来控制类.属性和方法的访问权限和其他功能,通常放在语句的最前端.例如: 1 public class className { 2 // body of class 3 } 4 pr ...

  3. leetcode-99 Recover_Binary_Search_Tree

    题目要求: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without ch ...

  4. PHP和JS实现多按钮提交表单

    JS: <html> <head> <script> function submitit1() //交由程序1处理 { document.myForm.action ...

  5. Java基础(6):foreach 方法遍历数组

    foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...

  6. c++必读

    下面的是学c++时要注意的.绝对经典.!!  1.把c++当成一门新的语言学习(和c没啥关系!真的.): 2.看<thinking in c++>,不要看<c++变成死相>:  ...

  7. bzoj4152 [AMPPZ2014]The Captain

    最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...

  8. MyEclipse启动失败

    日志的一部分: !SESSION 2014-09-24 11:47:03.156 -----------------------------------------------eclipse.buil ...

  9. 深入了解webservice_概念总结

    最近公司需要对java web端的第三方接口进行测试,使用WebService+TestNG实现,TsetNg是常用的自动化测试框架,这就不介绍了. WebService是一种跨编程语言和跨操作系统平 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON FillUp2

    zw版[转发·台湾nvp系列Delphi例程]HALCON FillUp2 procedure TForm1.Button1Click(Sender: TObject);var op : HOpera ...