@Component 注解
@Component
a) 初始化的名字默认为类名首字母小写:UserService 在容器中默认为 userService
b) 可以指定初始化 bean 的名字: @Component(value="userService")
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt" /><!-- 指定包 --> </beans>
bean 配置:
UserDAOImpl
package com.bjsxt.dao.impl; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component //默认名字:userDAOImpl
public class UserDAOImpl implements UserDAO{ public void save(User u) {
System.out.println("a user saved!");
} }
UserService
package com.bjsxt.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component(value="userService")
public class UserService { private UserDAO userDAO; public void init(){
System.out.println("init");
} public UserService(){} public UserService(UserDAO userDAO){
super();
this.userDAO = userDAO;
} public void addUser(User u){
this.userDAO.save(u);
} public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="userDAOImpl")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} public void destroy(){
System.out.println("destroy");
} }
代码链接: http://pan.baidu.com/s/1i4GwEPn 密码: awdu
jar包链接: http://pan.baidu.com/s/1hs655SK 密码: 6h8x
@Component 注解的更多相关文章
- Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...
- Spring中@Component注解,@Controller注解详解
在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...
- Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解
SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...
- controller,service,repository,component注解的使用对比
项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...
- 分分享知识-快乐自己: @Component注解的使用
@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 @service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 @repositor ...
- Spring Boot@Component注解下的类无法@Autowired的问题
title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...
- Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service
最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...
- @Service ,@Controller,@Component注解
首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...
- SpringBoot1.X /2.X使用@Component注解注入为java.lang.NullPointException 问题小结
问题简述: 我们在使用Spring系的产品的时候总是想当然的以为所有指定的Bean都会让Spring来管理,其实不然,即使是Spring出品的产品也不全是的哦,以下是我遇到的一个使用@Componen ...
- spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)
Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...
随机推荐
- Android Toast语句应用
1.findViewById()函数使用 函数作用:通过id来找到前台界面的组件 2.Toast语句 (1)介绍 (2)用法 (3)代码示例 package com.lucky.test21; imp ...
- C++_标准模板库STL概念介绍4-算法
STL包含很多处理容器的非成员函数: sort() copy() find() random_shuffle() set_union() set_intersection() set_differen ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- [一首诗] Life and Death 生与死
Life and Death 生与死 I strove with none, 我和谁都不争, for none was worth my strife, 和谁争我都不屑: Nature I loved ...
- 补档 VS远程调试
先说概念 开发机:将编译好的程序部署到目标机器上执行.配置 VS 工程,建立与目标机的连接,开始远程调试. 目标机:负责执行目标程序.安装和运行远程工具 (Remote Debugger),等待来自开 ...
- UESTC - 1610 递推方程+矩阵快速幂
感觉像是HDU Keyboard的加强版,先推出3张牌时的所有组合,然后递推出n张牌 看到n=1e18时吓尿了 最后24那里还是推错了.. (5行1列 dp[1][n],dp[2][n],dp[3][ ...
- POJ - 2528 奇怪的测试数据
听说POJ内部测试数据有问题 我这份代码是WA的(UPD:第二份是AC代码),不过目前把discuss的数据试了一下没毛病 自己试了几组好像也没毛病? 感觉线段树部分的简单hash处理方法还是值得学习 ...
- 下厨房---map/字符串查询
题目描述 牛牛想尝试一些新的料理,每个料理需要一些不同的材料,问完成所有的料理需要准备多少种不同的材料. 输入描述: 每个输入包含 1 个测试用例.每个测试用例的第 i 行,表示完成第 i 件料理需要 ...
- Robot Framework变量的使用技巧
1.变量的使用 变量可以在命令行中设置,个别变量设置使用--variable (-v)选项,变量文件的选择使用--variablefile (-V)选项.通过命令行设置的变量是全局变量,对其所有执行的 ...
- PIE SDK SFIM融合
1.算法功能简介 SFIM 融合方法全称为基于平滑滤波的亮度变换.基本原理是将高分辨率影像通过低通滤波抑制其高频空间信息保留低频信息,再将原高分辨率影像与通过低通滤波的高分辨率影像进行比值运算,以抵消 ...