17.笔记JAVA Spring框架学习————基于注解配置Bean之一

本文介绍Spring框架中如何利用注解配置Bean,包括@Component、@Respository、@Service及@Controller等注解的使用方法,并演示了如何进行组件扫描及注入。

17.笔记JAVA Spring框架学习————基于注解配置Bean之一

•       组件扫描(componentscanning):  Spring 能够从 classpath下自动扫描, 侦测和实例化具有特定注解的组件.

•       特定组件包括:

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

–      @Respository: 标识持久层组件

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

–      @Controller: 标识表现层组件

•       对于扫描到的组件, Spring有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

•       当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明<context:component-scan>

–      base-package 属性指定一个需要扫描的基类包Spring 容器将会扫描这个基类包里及其子包中的所有类.

–      当需要扫描多个包时, 可以使用逗号分隔.

–      如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:

–      <context:include-filter> 子节点表示要包含的目标类

–      <context:exclude-filter> 子节点表示要排除在外的目标类

–      <context:component-scan> 下可以拥有若干个<context:include-filter> 和 <context:exclude-filter> 子节点

–      <context:include-filter> 和<context:exclude-filter> 子节点支持多种类型的过滤表达式:

还需要导入 spring-aop-4.3.2.RELEASE.jar

创建一个package名字为annotation .generic

然后在package中创建如下类:

创建一个类UserService

package annotation.generic;

import org.springframework.stereotype.Service;

 

//若注解没有指定 bean id, 则类名第一个字母小写即为 bean id

@Service

publicclass UserService extendsBaseService<User>{

 

}

创建BaseService类如下:

package annotation.generic;

 

import org.springframework.beans.factory.annotation.Autowired;

 

publicclass BaseService<T> {

 

      @Autowired

      privateBaseDao<T> dao;

     

      publicvoid addNew(T entity){

            System.out.println("addNew by " + dao);

            dao.save(entity);

      }

     

}

创建RoleService

package annotation.generic;

 

import org.springframework.stereotype.Service;

 

@Service

publicclass RoleService extendsBaseService<Role>{

 

}

然后在app.xml文件中,配置如下:

                  <context:component-scanbase-package="annotation"></context:component-scan>

在main.java中代码如下:

package annotation.generic;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Main {

                 

                  publicstatic void main(String[] args) {

                                   

                                    ApplicationContextctx = new ClassPathXmlApplicationContext("app.xml");                                  

                                    UserServiceuserService = (UserService) ctx.getBean("userService");

                                    userService.addNew(newUser());                      

                                    RoleServiceroleService = (RoleService) ctx.getBean("roleService");

                                    roleService.addNew(newRole());

                  }                

}

执行如下:

addNewby annotation.generic.UserDao@3d0f8e03

Save:annotation.generic.User@6366ebe0

addNewby annotation.generic.RoleDao@44f75083

Save:annotation.generic.Role@2698dc7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值