Spring框架bean的配置(3):基于注解的配置
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):基于注解的配置的更多相关文章
- Spring框架第四篇之基于注解的DI注入
一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring基于注解@Required配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题
解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题 Alternatively, create an own implementation of ...
随机推荐
- Java基础之访问文件与目录——测试文件或目录的路径(TryPath)
控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...
- windows远程控制ubuntu尝试--未成功
按照百度知道上的步骤一步一步操作,下载xrdp,一切很顺利. 直至出现了,如下的語言: connecting to sesman ip 尝试找到原因第一个原因 第二次的分析解释 至今还没找出原因
- Servlet 基础认识 使用
参考书<JSP Web 开发案例教程> 在jsp项目中的src文件下面创建HelloServletTest.java,代码如下,注意src的包名,我的包名是zhbit.com packag ...
- Python之urllib2
urllib2 - extensible library for opening URLs Note The urllib2 module has been split across several ...
- csuoj 1022: 菜鸟和大牛
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1022 1022: 菜鸟和大牛 Time Limit: 1 Sec Memory Limit: 1 ...
- Appium的理念
1.Appium的架构:C/S模式 Appium的核心是暴漏REST API的WebServer,appium接收来自客户端的连接请求,监听由客户端发起的命令,在移动设备上执行这些命令,这些命令的执行 ...
- JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面
将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...
- c 语言 结构体
一:结构体定义结构体类型变量 三种方式1st:先声明结构体类型,再定义该类型的变量struct student liming,zhangle;2nd:声明类型的同时定义变量struct student ...
- php curl 抓去远程页面内容
<?php/*** php curl抓取远程网页内容* edit by www.jbxue.com*/$curlPost = 'a=1&b=2';//模拟POST数据$ch = curl ...
- git使用记录(新手入门)
最近参与了公司的项目开发,最后要用git来把代码添加到远程库,但是没怎么接触过git,记录一下使用的流程 首先,当然是先下载git,这个略过不提,下载完之后,在你想要保存代码的目录下,用git ini ...