@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 注解的更多相关文章

  1. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  2. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  3. Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

    SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...

  4. controller,service,repository,component注解的使用对比

    项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...

  5. 分分享知识-快乐自己: @Component注解的使用

    @controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 @service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 @repositor ...

  6. Spring Boot@Component注解下的类无法@Autowired的问题

    title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...

  7. Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service

    最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...

  8. @Service ,@Controller,@Component注解

    首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...

  9. SpringBoot1.X /2.X使用@Component注解注入为java.lang.NullPointException 问题小结

    问题简述: 我们在使用Spring系的产品的时候总是想当然的以为所有指定的Bean都会让Spring来管理,其实不然,即使是Spring出品的产品也不全是的哦,以下是我遇到的一个使用@Componen ...

  10. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

随机推荐

  1. window 系统 cygwin swool 问题

    cygwin 终端乱码  端口占用 查看 tcp  端口: netstat -tno  或者 netstat -an | grep 端口 杀死进程号 : kill   进程号 --->  143 ...

  2. poj3417 Network 树上差分+LCA

    题目传送门 题目大意:给出一棵树,再给出m条非树边,先割掉一条树边,再割掉一条非树边,问有几种割法,使图变成两部分. 思路:每一条 非树边会和一部分的树边形成一个环,分三种情况: 对于那些没有形成环的 ...

  3. ZOJ - 2401 水DP

    最近会多做点巩固基础的题目 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  4. 关于jstl taglib的错误 Can not find the tag library descriptor for “http://java.sun.com/jstl/core”

    在查了N个帖子之后,决定记录一下关于jstl taglib的配置方法. 首先我遇到的错误是: Can not find the tag library descriptor for "htt ...

  5. F查询和Q查询

    F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的实例可以 ...

  6. 子查询及pymysql

    子查询 子查询指的是当一个查询语句被作为另一个查询语句的条件时,该查询语句就称之为子查询(内层查询) 可以将一个大问题 拆分几个小的问题 然后一步一步来查询 需求:财务不有哪些人 ),sex ),jo ...

  7. centos 7 ssh登录安全问题

    2018-10-11 1.ssh禁止root远程登录 修改ssh配置文件/etc/ssh/sshd_config vim /etc/ssh/sshd_config PermitRootLogin ye ...

  8. 剑指offer——面试题5:替换空格

    利用STL: #include"iostream" #include"stdio.h" #include"algorithm" using ...

  9. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  10. CSS气泡

    气泡状文本框,是一种很生动的网页设计手段. 它可以用来表示用户的发言. 也可以用来作为特定信息的提示符. DVD租借网站Netflix,还用它显示碟片的详细信息. ================== ...