Spring初学之注解方式配置bean
直接看代码:
UserController.java
package spring.beans.annotation.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import spring.beans.annotation.service.UserService; @Controller
public class UserController { @Autowired
private UserService userService;
public void add() { System.out.println("UserController add...");
userService.add();
}
}
UserService.java
package spring.beans.annotation.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import spring.beans.annotation.repository.UserRepository;
import spring.beans.annotation.repository.UserRepositoryImpl; @Service
public class UserService { @Autowired
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;
public void add(){
System.out.println("UserService add...");
userRepository.add();
}
}
UserRepository.java
package spring.beans.annotation.repository;
public interface UserRepository {
public void add();
}
UserRepositoryImpl.java
package spring.beans.annotation.repository;
import org.springframework.stereotype.Repository;
@Repository//("userRepository")
//这里设置的value="userRepository",也可以用@Qualifier("userRepositoryImpl")写在装配它的地方
public class UserRepositoryImpl implements UserRepository {
@Override
public void add() {
System.out.println("UserRepositoryImpl add...");
}
}
xml:
<?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"> <!-- 扫描指定spring IOC容器的类 -->
<!--使用resource-pattern扫描特定的类 -->
<!--
<context:component-scan base-package="spring.beans.annotation"
resource-pattern="repository/*.class">
</context:component-scan>
--> <!-- 筛选扫描 -->
<context:component-scan base-package="spring.beans.annotation"
>
<!-- 扫描不包含指定类型(表达式)的类 -->
<!--
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- 扫描包含指定类型(表达式)的类(context下设置use-default-filters="false")配合使用 -->
<!-- <context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- 扫描除了该接口 或该接口实现类的类 -->
<!-- <context:exclude-filter type="assignable"
expression="spring.beans.annotation.repository.UserRepository"/>
-->
<!-- 只扫描包含该接口 或该接口实现类的类 (context下设置use-default-filters="false")配合使用-->
<!-- <context:include-filter type="assignable"
expression="spring.beans.annotation.repository.UserRepository"/>
-->
</context:component-scan> </beans>
Spring初学之注解方式配置bean的更多相关文章
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring之通过注解方式配置Bean(一)
(1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...
- Spring使用ioc注解方式配置bean
context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- spring笔记--通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
随机推荐
- 前端发起resultUrl请求,服务端收到后做逆向处理,校验sign后,执行originUrl逻辑
originUrl=http://test.com:8080/user/alipay_phone?uid=123&amount=21.3第0步:前后端约定32位密钥KEY第一步:对参数按照ke ...
- 【转】@javax.ws.rs Webservice注解
用于webservice. 1.路径 @javax.ws.rs.Path 标识要请求的资源类或资源方法的uri路径. 例,@Path("animal"),表示下一层路径是anima ...
- 0x02 MySQL SQL基本语句
有了mysql这个数据库软件,就可以将程序员从对数据的管理中解脱出来,专注于对程序逻辑的编写 mysql服务端软件即mysqld帮我们管理好文件夹以及文件,前提是作为使用者的我们,需要下载mysql的 ...
- Linux中命令查找顺序
第一优先级:用绝对路径或相对路径执行的命令第二优先级:别名指定的命令第三优先级:Bash内部命令第四优先级:$PATH环境变量定义的目录查找顺序中找到的第一个命令
- Linux用户相关文件之密码文件
1.文件地址: /etc/shadow ----------. 1 root root 842 10月 6 13:09 /etc/shadow 2.文件内容: xiaol_1:$6$NdCAnK3y$ ...
- ubuntu 安装Nodejs
ubuntu 安装Nodejs 1.在软件管理器里面安装nodejs2.由于版本很老,所以需要更新版本:先安装npm , sudo apt install npm然后用npm安装 n 命令,更新nod ...
- 模拟美式橄榄球比赛数据(R)
获得和清洗数据: 1.从网络上抓取数据 year<- url<-paste("http://sports.yahoo.com/nfl/stats/byteam?group=Off ...
- scrapy使用笔记
新建项目 在需要新建项目的目录下发指令 scrapy startproject MySpider 其中MySpider为工程的名字,会新建一个文件夹 进入工程目录 新建一个爬虫 scrapy gens ...
- 常用模块之hashlib,configparser,logging模块
常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- Hibernate深入浅出(九)持久层操作——数据保存&批量操作
数据保存: 1)session.save session.save方法用于实体对象到数据库的持久化操作.也就是说,session.save方法调用与实体对象所匹配的Insert SQL,将数据插入 ...