<contest:component-scan base-package=":com.gong.spring.beans" resource-pattern="autowire/*.class"><context:include-filter>子节点表示要包含的目标类<context:exclude-filter>子节点表示要排除在外的目标类<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点;@H_404_0@一、所需环境@H_404_0@引入以下jar包,加入到build path中:@H_404_0@@H_404_0@两个连接数据库的,五个spring核心包,aop是额外的使用注解方式需要引入的包,一定要记得引入这个包。@H_404_0@基本目录如下:@H_404_0@@H_404_0@二、基本配置@[email protected]
package com.gong.spring.beans.annotation;import org.springframework.stereotype.Component;@Componentpublic class TestObject {}@[email protected]
com.gong.spring.beans.annotation.controller; org.springframework.stereotype.Controller;@Controller UserController { voID execute() { System.out.println("UserController的execute方法"); }}@[email protected]
com.gong.spring.beans.annotation.repository;interface UserRepository { save();}@[email protected]
org.springframework.stereotype.Repository;//可以指定使用时的名字@Repository(value="userRepository")class UserRepositoryImpl implements UserRepository{ @OverrIDe save() { Todo auto-generated method stub System.out.println("UserReposityImpl的save方法"); }}@[email protected]
com.gong.spring.beans.annotation.service; org.springframework.stereotype.Service;@Service UserService { add() { System.out.println("UserService中的add方法"); }}@[email protected]
org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.gong.spring.beans.annotation.controller.UserController; com.gong.spring.beans.annotation.repository.UserRepository; com.gong.spring.beans.annotation.service.UserService; Main { static main(String[] args) { 1.创建spring的IOC容器对象 ApplicationContext ctx = new ClasspathXmlApplicationContext("beans-annotation.xml"); 2.从容器中获取Bean实例 TestObject to = (TestObject) ctx.getBean("testObject"); System.out.println(to); UserController userController = (UserController) ctx.getBean("userController"); System.out.println(userController); UserService userService = (UserService) ctx.getBean("userService"); System.out.println(userService); UserRepository userRepository = (UserRepository) ctx.getBean("userRepository"); System.out.println(userRepository); } }@H_404_0@ beans-annotation.xml(需要引入context命名空间)
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 配置springIOC容器扫描的包 --> context:component-scan base-package="com.gong.spring.beans.annotation"></context:component-scan></beans>@H_404_0@输出:@H_404_0@@H_404_0@说明这些带有注解的类已经被spring所识别并被IOC容器所管理。需要注意的是,默认情况下获取bean的实例时,名字是类名,但首字母是小写。例如TestObject通过getBean("testObject")来获取,当然,也可以在注解时使用value属性来声明bean的名字。@H_404_0@三、子节点配置@H_404_0@再来看:
="com.gong.spring.beans.annotation" resource-pattern="repository/*.class">@H_404_0@指定扫描的模式之后,我们就只能访问到repository下面的组件了,即输出:@H_404_0@=@H_404_0@在<context:include-filter>和<context:exclude-filter>子节点支持多种类型的表达式:annotation:所有标注了XxxAnnotation的类;assignable:所有继承或扩展XxxService的类;aspectjregexcustom@H_404_0@(1)使用annotation的方式
> context:exclude-filter type="annotation" Expression="org.springframework.stereotype.Repository"/> >@H_404_0@不包含org.springframework.stereotype.Repository,即输出:@H_404_0@
use-default-filters="false"context:include-filter >@H_404_0@只包含org.springframework.stereotype.Repository,注意需要配置use-default-filters为false,即输出:@H_404_0@@H_404_0@(2)使用assignable方式
="assignable"="com.gong.spring.beans.annotation.repository.UserRepository">@H_404_0@不包含UserRepository接口及其实现类的。
>
@H_404_0@设置use-default-filters="false"。只包含UserRepository接口及其实现类的。 总结 以上是内存溢出为你收集整理的spring之通过注解方式配置Bean(一)全部内容,希望文章能够帮你解决spring之通过注解方式配置Bean(一)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)